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