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