plotting_using_sage/script.rst
author Anoop Jacob Thomas<anoop@fossee.in>
Tue, 12 Oct 2010 16:26:36 +0530
changeset 308 0a0a91fb3a0d
parent 255 75fd106303dc
child 332 b702c10e5919
permissions -rw-r--r--
added slides for other-type-of-plots.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
255
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     1
.. Objectives
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     2
.. ----------
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     3
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     4
.. A - Students and teachers from Science and engineering backgrounds
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     5
   B -
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     6
   C - 
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     7
   D - 
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     8
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     9
.. Prerequisites
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    10
.. -------------
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    11
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    12
..   1. Getting started with lists
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    13
     
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    14
.. Author              : Nishanth Amuluru
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    15
   Internal Reviewer   : 
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    16
   External Reviewer   :
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    17
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    18
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    19
Script
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    20
------
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    21
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    22
Hello friends, welcome to the tutorial on "Plotting using SAGE".
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    23
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    24
{{{ Show the outline slide }}}
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    25
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    26
In this tutorial we shall look at 
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    27
 
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    28
 * 2D plotting in SAGE
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    29
 * 3D plotting in SAGE
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    30
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    31
We shall first create a symbolic variable ``x``
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    32
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    33
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    34
    x = var('x')
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    35
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    36
We shall plot the function ``sin(x) - cos(x) ^ 2`` in the range (-5, 5).
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    37
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    38
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    39
    plot(sin(x) - cos(x) ^ 2, (x, -5, 5))
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    40
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    41
As we can see, the plot is shown.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    42
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    43
``plot`` command takes the symbolic function as the first argument and the
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    44
range as the second argument.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    45
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    46
{{{ Pause here and try out the following exercises }}}
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    47
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    48
%% 1 %% Define a variable ``y`` and plot the function ``y^2 + 5y - 7`` in the
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    49
        range (-3, 3)
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    50
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    51
{{{ continue from paused state }}}
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    52
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    53
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    54
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    55
    y = var('y')
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    56
    plot(y^2 + 5*y -7, (y, -3, 3))
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    57
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    58
We have seen that plot command plots the given function on a linear range.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    59
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    60
What if the x and y values are functions of another variable.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    61
For instance, lets plot the trajectory of a projectile.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    62
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    63
A projectile was thrown at 50 m/s^2 and at an angle of 45 degrees from the 
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    64
ground. We shall plot the trajectory of the particle for 5 seconds.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    65
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    66
These types of plots can be drawn using the parametric_plot function.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    67
We first define the time variable.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    68
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    69
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    70
    t = var('t')
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    71
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    72
Then we define the x and y as functions of t.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    73
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    74
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    75
    f_x = 50 * cos(pi/4)
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    76
    f_y = 50 * sin(pi/4) * t - 1/2 * 9.81 * t^2 )
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    77
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    78
We then call the ``parametric_plot`` function as
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    79
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    80
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    81
    parametric_plot((f_x, f_y), (t, 0, 5))
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    82
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    83
And we can see the trajectory of the projectile.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    84
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    85
The ``parametric_plot`` funciton takes a tuple of two functions as the first
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    86
argument and the range over which the independent variable varies as the second
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    87
argument.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    88
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    89
{{{ Pause here and try out the following exercises }}}
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    90
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    91
%% 2 %% A particle is thrown into the air at 10 m/s^2 and at angle of 60 degrees
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    92
        from the top of a 100 m tower. Plot the trajectory of the particle.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    93
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    94
{{{ continue from paused state }}}
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    95
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    96
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    97
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    98
    t = var('t')
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    99
    f_x = 10 * cos(pi/3) * t
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   100
    f_y = 100 + 10 * sin(pi/3) * t - 1/2 * 9.81 * t^2
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   101
    parametric_plot((f_x, f_y), (t,0,5))
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   102
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   103
Now we shall look at how to plot a set of points.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   104
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   105
We have the ``line`` function to acheive this.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   106
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   107
We shall plot sin(x) at few points and join them.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   108
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   109
First we need the set of points.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   110
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   111
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   112
    points = [ (x, sin(x)) for x in srange(-2*float(pi), 2*float(pi), 0.75) ]
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   113
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   114
``srange`` takes a start, a stop and a step argument and returns a list of
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   115
point. We generate list of tuples in which the first value is ``x`` and second
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   116
is ``sin(x)``.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   117
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   118
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   119
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   120
    line(points)
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   121
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   122
plots the points and joins them with a line.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   123
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   124
{{{ Pause here and try out the following exercises }}}
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   125
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   126
%% 3 %% Plot the cosine function using line function.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   127
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   128
{{{ continue from paused state }}}
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   129
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   130
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   131
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   132
    points = [ (x, cos(x)) for x in srange(-2*float(pi), 2*float(pi), 0.75) ]
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   133
    line(points)
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   134
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   135
The ``line`` function behaves like the plot command in matplotlib. The
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   136
difference is that ``plot`` command takes two sequences while line command
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   137
expects a sequence of co-ordinates.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   138
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   139
As we can see, the axes limits are set by SAGE. Often we would want to set them
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   140
ourselves. Moreover, the plot is shown here since the last command that is
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   141
executed produces a plot. 
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   142
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   143
Let us try this example
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   144
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   145
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   146
    plot(cos(x), (x,0,2*pi))
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   147
    # Does the plot show up??
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   148
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   149
As we can see here, the plot is not shown since the last command does not
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   150
produce a plot.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   151
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   152
The actual way of showing a plot is to use the ``show`` command.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   153
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   154
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   155
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   156
    p1 = plot(cos(x), (x,0,2*pi))
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   157
    show(p1)
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   158
    # What happens now??
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   159
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   160
As we can see the plot is shown since we used it with ``show`` command.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   161
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   162
``show`` command is also used set the axes limits.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   163
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   164
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   165
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   166
    p1 = plot(cos(x), (x,0,2*pi))
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   167
    show(p1, xmin=0, xmax=2*pi, ymin=-1.2, ymax=1.2)
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   168
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   169
As we can see, we just have to pass the right keyword arguments to the ``show``
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   170
command to set the axes limits.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   171
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   172
{{{ Pause here and try out the following exercises }}}
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   173
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   174
%% 4 %% Plot the cosine function in the range (-2pi, 2pi) and set the x-axis
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   175
        limits to (-5, 5) and y-axis limits to (-2, 2) respectively.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   176
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   177
{{{ continue from paused state }}}
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   178
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   179
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   180
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   181
    p1 = plot(cos(x), (x, 0, 2*pi))
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   182
    show(p1, xmin=-5, xmax=5, ymin=-2, ymax=2)
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   183
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   184
The ``show`` command can also be used to show multiple plots.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   185
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   186
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   187
    p1 = plot(cos(x), (x, 0, 2*pi))
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   188
    p2 = plot(sin(x), (x, 0, 2*pi))
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   189
    show(p1+p2)
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   190
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   191
As we can see, we can add the plots and use them in the ``show`` command.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   192
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   193
{{{ Pause here and try out the following exercises }}}
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   194
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   195
%% 5 %% Plot sin(x) and sin(2*x) in the range (0, 2pi)
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   196
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   197
{{{ continue from paused state }}}
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   198
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   199
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   200
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   201
    p1 = plot(sin(x), (x, 0, 2*pi))
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   202
    p2 = plot(sin(2*x), (x, 0, 2*pi))
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   203
    show(p1+p2)
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   204
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   205
Now we shall look at 3D plotting in SAGE.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   206
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   207
We have the ``plot3d`` function that takes a function in terms of two 
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   208
independent variables and the range over which they vary.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   209
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   210
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   211
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   212
    x, y = var('x y')
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   213
    plot3d(x^2 + y^2, (x, 0, 2), (y, 0, 2))
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   214
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   215
We get a 3D plot which can be rotated and zoomed using the mouse.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   216
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   217
{{{ Pause here and try out the following exercises }}}
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   218
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   219
%% 6 %% Plot the function sin(x)^2 + cos(y)^2 for x in range (0,2) and y in
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   220
        range (-2, 2)
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   221
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   222
{{{ continue from paused state }}}
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   223
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   224
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   225
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   226
    x, y = var("x y")
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   227
    plot3d( sin(x)^2 + cos(y)^2, (x, 0, 2), (y, -2, 2))
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   228
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   229
``parametric_plot3d`` function plots the surface in which x, y and z are
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   230
functions of another variable.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   231
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   232
::
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   233
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   234
   u, v = var("u v")
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   235
   f_x = u
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   236
   f_y = v
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   237
   f_z = u^2 + v^2
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   238
   parametric_plot3d((f_x, f_y, f_z), (u, 0, 2), (v, 0, 2))
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   239
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   240
{{{ Show summary slide }}}
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   241
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   242
This brings us to the end of the tutorial.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   243
we have learnt
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   244
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   245
 * How to draw 2D plots using plot comand
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   246
 * How to use the parametric_plot and line functions
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   247
 * How to use show command for multiple plots and setting axes limits
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   248
 * How to draw 3D plots
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   249
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   250
{{{ Show the "sponsored by FOSSEE" slide }}}
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   251
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   252
#[Nishanth]: Will add this line after all of us fix on one.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   253
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   254
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   255
Hope you have enjoyed and found it useful.
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   256
Thankyou
75fd106303dc Added the script to plotting_using_sage
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   257