getting-started-with-symbolics/script.rst
changeset 458 9a1c5d134feb
parent 442 a9b71932cbfa
child 491 ebfe3a675882
equal deleted inserted replaced
446:2ce824b5adf4 458:9a1c5d134feb
    23    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    23    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    24 
    24 
    25 Symbolics with Sage
    25 Symbolics with Sage
    26 -------------------
    26 -------------------
    27 
    27 
    28 Hello friends and welcome to the tutorial on symbolics with sage.
    28 Hello friends and welcome to the tutorial on Symbolics with Sage.
    29 
    29 
    30 {{{ Show welcome slide }}}
    30 {{{ Show welcome slide }}}
    31 
    31 
    32 
       
    33 .. #[Madhu: What is this line doing here. I don't see much use of it]
       
    34 
       
    35 During the course of the tutorial we will learn
    32 During the course of the tutorial we will learn
    36 
    33 
    37 {{{ Show outline slide  }}}
    34 {{{ Show outline slide  }}}
    38 
    35 
    39 * Defining symbolic expressions in sage.  
    36 * Defining symbolic expressions in Sage.  
    40 * Using built-in constants and functions. 
    37 * Using built-in constants and functions. 
    41 * Performing Integration, differentiation using sage. 
    38 * Performing Integration, differentiation using Sage. 
    42 * Defining matrices. 
    39 * Defining matrices. 
    43 * Defining Symbolic functions.  
    40 * Defining symbolic functions.  
    44 * Simplifying and solving symbolic expressions and functions.
    41 * Simplifying and solving symbolic expressions and functions.
    45 
    42 
    46 We can use Sage for symbolic maths. 
    43 Amongst a lot of other things, Sage can do Symbolic Math and we shall
       
    44 start with defining symbolic expressions in Sage. 
       
    45 
       
    46 Hope you have your Sage notebook open. If not, pause the video and
       
    47 start you Sage notebook. 
    47 
    48 
    48 On the sage notebook type::
    49 On the sage notebook type::
    49    
    50    
    50     sin(y)
    51     sin(y)
    51 
    52 
    52 It raises a name error saying that y is not defined. But in sage we
    53 It raises a name error saying that ``y`` is not defined. We need to
    53 can declare y as a symbol using var function.
    54 declare ``y`` as a symbol. We do it using the ``var`` function. 
    54 
    55 ::
    55 
    56 
    56 ::
       
    57     var('y')
    57     var('y')
    58    
    58    
    59 Now if you type::
    59 Now if you type::
    60 
    60 
    61     sin(y)
    61     sin(y)
    62 
    62 
    63 sage simply returns the expression.
    63 Sage simply returns the expression.
    64 
    64 
    65 
    65 Sage treats ``sin(y)`` as a symbolic expression. We can use this to do
    66 Thus sage treats sin(y) as a symbolic expression . We can use
    66 symbolic maths using Sage's built-in constants and expressions.
    67 this to do  symbolic maths using sage's built-in constants and
    67 
    68 expressions..
    68 Let us try out a few examples. ::
    69 
       
    70 
       
    71 So let us try ::
       
    72    
    69    
    73    var('x,alpha,y,beta') 
    70    var('x,alpha,y,beta') 
    74    x^2/alpha^2+y^2/beta^2
    71    x^2/alpha^2+y^2/beta^2
       
    72 
       
    73 We have defined 4 variables, ``x``, ``y``, ``alpha`` and ``beta`` and
       
    74 have defined a symbolic expression using them.
    75  
    75  
    76 taking another example ::
    76 Here is an expression in ``theta``  ::
    77    
    77    
    78    var('theta')
    78    var('theta')
    79    sin(theta)*sin(theta)+cos(theta)*cos(theta)
    79    sin(theta)*sin(theta)+cos(theta)*cos(theta)
    80 
    80 
    81 Similarly, we can define many algebraic and trigonometric expressions using sage .
    81 Now that you know how to define symbolic expressions in Sage, here is
    82 
    82 an exercise. 
    83 
    83 
    84 Following is an exercise that you must do. 
    84 {{ show slide showing question 1 }}
    85 
    85 
    86 %% %%  Define following expressions as symbolic expressions
    86 %% %% Define following expressions as symbolic expressions in Sage. 
    87 in sage?
       
    88    
    87    
    89    1. x^2+y^2
    88    1. x^2+y^2
    90    #. y^2-4ax
    89    #. y^2-4ax
    91   
    90   
    92 Please, pause the video here. Do the exercise and then continue. 
    91 Please, pause the video here. Do the exercise and then continue. 
    93 
    92 
    94 The solution is on your screen.
    93 The solution is on your screen.
    95 
    94 
    96 
    95 {{ show slide showing solution 1 }}
    97 Sage also provides a few built-in constants which are commonly used in mathematics .
    96 
    98 
    97 Sage also provides built-in constants which are commonly used in
    99 example : pi,e,infinity , Function n gives the numerical values of all these constants.
    98 mathematics, for instance pi, e, infinity. The function ``n`` gives
   100 
    99 the numerical values of all these constants.
   101 {{{ Type n(pi) n(e) n(oo) On the sage notebook }}}
   100 :: 
   102 
   101     n(pi) 
   103 
   102     n(e) 
   104 
   103     n(oo)
   105 If you look into the documentation of function "n" by doing
   104    
   106 
   105 If you look into the documentation of function ``n`` by doing
   107 .. #[Madhu: "documentation of the function "n"?]
       
   108 
   106 
   109 ::
   107 ::
   110    n(<Tab>
   108    n(<Tab>
   111 
   109 
   112 You will see what all arguments it takes and what it returns. It will be very
   110 You will see what all arguments it takes and what it returns. It will
   113 helpful if you look at the documentation of all functions introduced through
   111 be very helpful if you look at the documentation of all functions
   114 this script.
   112 introduced in the course of this script.
   115 
   113 
   116 
   114 Also we can define the number of digits we wish to have in the
   117 
   115 constants. For this we have to pass an argument -- digits.  Type
   118 Also we can define the no. of digits we wish to use in the numerical
   116 
   119 value . For this we have to pass an argument digits.  Type
       
   120 
       
   121 .. #[Madhu: "no of digits"? Also "We wish to obtain" than "we wish to
       
   122      use"?]
       
   123 ::
   117 ::
   124 
   118 
   125    n(pi, digits = 10)
   119    n(pi, digits = 10)
   126 
   120 
   127 Apart from the constants sage also has a lot of builtin functions like
   121 Apart from the constants Sage also has a lot of built-in functions
   128 sin,cos,log,factorial,gamma,exp,arcsin etc ...
   122 like ``sin``, ``cos``, ``log``, ``factorial``, ``gamma``, ``exp``,
   129 lets try some of them out on the sage notebook.
   123 ``arcsin`` etc ...
   130 
   124 
   131 
   125 Lets try some of them out on the Sage notebook.
   132 ::
   126 ::
   133      
   127      
   134    sin(pi/2)
   128    sin(pi/2)
   135    
   129    
   136    arctan(oo)
   130    arctan(oo)
   137      
   131      
   138    log(e,e)
   132    log(e,e)
   139 
   133 
   140 Following is are exercises that you must do. 
   134 Following are exercises that you must do. 
   141 
   135 
   142 %% %% Find the values of the following constants upto 6 digits  precision 
   136 {{ show slide showing question 2 }}
       
   137 
       
   138 %% %% Find the values of the following constants upto 6 digits
       
   139       precision
   143    
   140    
   144    1. pi^2
   141    1. pi^2
   145    #. euler_gamma^2
   142    #. euler_gamma^2
   146 
   143 
   147 
   144 
   148 %% %% Find the value of the following.
   145 %% %% Find the value of the following.
   149 
   146 
   150    1. sin(pi/4)
   147    1. sin(pi/4)
   151    #. ln(23)  
   148    #. ln(23)  
   152 
   149 
   153 Please, pause the video here. Do the exercises and then continue. 
   150 Please, pause the video here. Do the exercises and then continue.
   154 
   151 
   155 The solutions are on your screen.
   152 The solutions are on your screen
   156 
   153 
   157 
   154 {{ show slide showing solution 2 }}
   158 
   155 
   159 Given that we have defined variables like x,y etc .. , We can define
   156 Given that we have defined variables like x, y etc., we can define an
   160 an arbitrary function with desired name in the following way.::
   157 arbitrary function with desired name in the following way.::
   161 
   158 
   162        var('x') 
   159        var('x') 
   163        function('f',x)
   160        function('f',x)
   164 
       
   165 
   161 
   166 Here f is the name of the function and x is the independent variable .
   162 Here f is the name of the function and x is the independent variable .
   167 Now we can define f(x) to be ::
   163 Now we can define f(x) to be ::
   168 
   164 
   169      f(x) = x/2 + sin(x)
   165      f(x) = x/2 + sin(x)
   172 	   
   168 	   
   173 	   f(pi)
   169 	   f(pi)
   174 
   170 
   175 We can also define functions that are not continuous but defined
   171 We can also define functions that are not continuous but defined
   176 piecewise.  Let us define a function which is a parabola between 0
   172 piecewise.  Let us define a function which is a parabola between 0
   177 to 1 and a constant from 1 to 2 .  Type the following as given on the
   173 to 1 and a constant from 1 to 2 .  Type the following 
   178 screen
       
   179 
       
   180 ::
   174 ::
   181       
   175       
   182 
   176 
   183       var('x') 
   177       var('x') 
   184       h(x)=x^2 
   178       h(x)=x^2 
   185       g(x)=1 
   179       g(x)=1 
   186       f=Piecewise(<Tab>
   180 
   187 
       
   188 {{{ Show the documentation of Piecewise }}} 
       
   189     
       
   190 ::
       
   191       f=Piecewise([[(0,1),h(x)],[(1,2),g(x)]],x) 
   181       f=Piecewise([[(0,1),h(x)],[(1,2),g(x)]],x) 
   192       f
   182       f
   193 
   183 
   194 
   184 We can also define functions convergent series and other series. 
   195 
       
   196 
       
   197 
       
   198 We can also define functions which are series 
       
   199 
       
   200 
   185 
   201 We first define a function f(n) in the way discussed above.::
   186 We first define a function f(n) in the way discussed above.::
   202 
   187 
   203    var('n') 
   188    var('n') 
   204    function('f', n)
   189    function('f', n)
   219 
   204 
   220 
   205 
   221     f(n) = (-1)^(n-1)*1/(2*n - 1)
   206     f(n) = (-1)^(n-1)*1/(2*n - 1)
   222     sum(f(n), n, 1, oo)
   207     sum(f(n), n, 1, oo)
   223 
   208 
   224 
       
   225 This series converges to pi/4. 
   209 This series converges to pi/4. 
   226 
   210 
   227 
       
   228 Following  are exercises that you must do. 
   211 Following  are exercises that you must do. 
       
   212 
       
   213 {{ show slide showing question 3 }}
   229 
   214 
   230 %% %% Define the piecewise function. 
   215 %% %% Define the piecewise function. 
   231    f(x)=3x+2 
   216    f(x)=3x+2 
   232    when x is in the closed interval 0 to 4.
   217    when x is in the closed interval 0 to 4.
   233    f(x)=4x^2
   218    f(x)=4x^2
   235    
   220    
   236 %% %% Sum  of 1/(n^2-1) where n ranges from 1 to infinity. 
   221 %% %% Sum  of 1/(n^2-1) where n ranges from 1 to infinity. 
   237 
   222 
   238 Please, pause the video here. Do the exercise(s) and then continue. 
   223 Please, pause the video here. Do the exercise(s) and then continue. 
   239 
   224 
       
   225 {{ show slide showing solution 3 }}
       
   226 
   240 Moving on let us see how to perform simple calculus operations using Sage
   227 Moving on let us see how to perform simple calculus operations using Sage
   241 
   228 
   242 For example lets try an expression first ::
   229 For example lets try an expression first ::
   243 
   230 
   244     diff(x**2+sin(x),x) 
   231     diff(x**2+sin(x),x) 
   245     2x+cos(x)
   232 
   246 
   233 The diff function differentiates an expression or a function. It's
   247 The diff function differentiates an expression or a function. Its
       
   248 first argument is expression or function and second argument is the
   234 first argument is expression or function and second argument is the
   249 independent variable.
   235 independent variable.
   250 
   236 
   251 We have already tried an expression now lets try a function ::
   237 We have already tried an expression now lets try a function ::
   252 
   238 
   254    diff(f(x),x)
   240    diff(f(x),x)
   255 
   241 
   256 To get a higher order differential we need to add an extra third argument
   242 To get a higher order differential we need to add an extra third argument
   257 for order ::
   243 for order ::
   258  
   244  
   259    diff(<tab> diff(f(x),x,3)
   245    diff(f(x),x,3)
   260 
   246 
   261 in this case it is 3.
   247 in this case it is 3.
   262 
       
   263 
   248 
   264 Just like differentiation of expression you can also integrate them ::
   249 Just like differentiation of expression you can also integrate them ::
   265 
   250 
   266      x = var('x') 
   251      x = var('x') 
   267      s = integral(1/(1 + (tan(x))**2),x) 
   252      s = integral(1/(1 + (tan(x))**2),x) 
   268      s
   253      s
   269 
   254 
   270 
   255 Many a times we need to find factors of an expression, we can use the
   271 
   256 "factor" function
   272 Many a times we need to find factors of an expression ,we can use the "factor" function
   257 
   273 
   258 ::
   274 ::
   259 
   275     factor(<tab> 
       
   276     y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2) 
   260     y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2) 
   277     f = factor(y)
   261     f = factor(y)
   278 
   262 
   279 One can  simplify complicated expression ::
   263 One can simplify complicated expression ::
   280     
   264     
   281     f.simplify_full()
   265     f.simplify_full()
   282 
   266 
   283 This simplifies the expression fully . We can also do simplification
   267 This simplifies the expression fully. We can also do simplification of
   284 of just the algebraic part and the trigonometric part ::
   268 just the algebraic part and the trigonometric part ::
   285 
   269 
   286     f.simplify_exp() 
   270     f.simplify_exp() 
   287     f.simplify_trig()
   271     f.simplify_trig()
   288     
   272     
   289 
   273 One can also find roots of an equation by using ``find_root`` function::
   290 
       
   291 One can also find roots of an equation by using find_root function::
       
   292 
   274 
   293     phi = var('phi') 
   275     phi = var('phi') 
   294     find_root(cos(phi)==sin(phi),0,pi/2)
   276     find_root(cos(phi)==sin(phi),0,pi/2)
   295 
   277 
   296 Lets substitute this solution into the equation and see we were
   278 Let's substitute this solution into the equation and see we were
   297 correct ::
   279 correct ::
   298 
   280 
   299      var('phi') 
   281      var('phi') 
   300      f(phi)=cos(phi)-sin(phi)
   282      f(phi)=cos(phi)-sin(phi)
   301      root=find_root(f(phi)==0,0,pi/2) 
   283      root=find_root(f(phi)==0,0,pi/2) 
   320       Does the equation have a root between 1,2. 
   302       Does the equation have a root between 1,2. 
   321 
   303 
   322 Please, pause the video here. Do the exercises and then continue. 
   304 Please, pause the video here. Do the exercises and then continue. 
   323 
   305 
   324 
   306 
   325 
       
   326 Lets us now try some matrix algebra symbolically ::
   307 Lets us now try some matrix algebra symbolically ::
   327 
       
   328 
       
   329 
   308 
   330    var('a,b,c,d') 
   309    var('a,b,c,d') 
   331    A=matrix([[a,1,0],[0,b,0],[0,c,d]]) 
   310    A=matrix([[a,1,0],[0,b,0],[0,c,d]]) 
   332    A
   311    A
   333 
   312 
   334 Now lets do some of the matrix operations on this matrix
   313 Now lets do some of the matrix operations on this matrix
   335 
       
   336 
       
   337 ::
   314 ::
   338     A.det() 
   315     A.det() 
   339     A.inverse()
   316     A.inverse()
   340 
   317 
   341 
   318 
   346       A=[[x,0,1][y,1,0][z,0,y]]
   323       A=[[x,0,1][y,1,0][z,0,y]]
   347 
   324 
   348 Please, pause the video here. Do the exercise(s) and then continue. 
   325 Please, pause the video here. Do the exercise(s) and then continue. 
   349 
   326 
   350 
   327 
   351 
       
   352 
       
   353 {{{ Show the summary slide }}}
   328 {{{ Show the summary slide }}}
   354 
   329 
   355 So in this tutorial we learnt how to
   330 That brings us to the end of this tutorial. In this tutorial we learnt
   356 
   331 how to
   357 
   332 
   358 * We learnt about defining symbolic expression and functions.  
   333 * define symbolic expression and functions
   359 * Using built-in constants and functions.  
   334 * use built-in constants and functions  
   360 * Using <Tab>  to see the documentation of a function.  
   335 * use <Tab> to see the documentation of a function  
   361 * Simple calculus operations .  
   336 * do simple calculus
   362 * Substituting values in expression using substitute function.
   337 * substitute values in expressions using ``substitute`` function
   363 * Creating symbolic matrices and performing operation on them .
   338 * create symbolic matrices and perform operations on them
   364 
   339