symbolics.rst
changeset 148 60a4616dbf55
child 187 3b912b3fdcbf
equal deleted inserted replaced
147:fe7aad827c9c 148:60a4616dbf55
       
     1 Symbolics with Sage
       
     2 -------------------
       
     3 
       
     4 This tutorial on using Sage for symbolic calculation is brought to you
       
     5 by Fossee group.
       
     6 
       
     7 {{{ Part of Notebook with title }}}
       
     8 
       
     9 We would be using simple mathematical functions on the sage notebook
       
    10 for this tutorial .
       
    11 
       
    12 During the course of the tutorial we will learn
       
    13 
       
    14 
       
    15 {{{ Part of Notebook with outline }}}
       
    16 
       
    17 To define symbolic expressions in sage .  Use built-in costants and
       
    18 function.  Integration , differentiation using sage .  Defining
       
    19 matrices.  Defining Symbolic functions .  Simplifying and solving
       
    20 symbolic expressions and functions
       
    21     
       
    22 
       
    23 
       
    24 Using sage we can perform mathematical operations on symbols .
       
    25 
       
    26 On the sage notebook type::
       
    27    
       
    28     sin(y)
       
    29 
       
    30 It raises a name error saying that y is not defined . But in sage we
       
    31 can declare y as a symbol using var function. ::
       
    32    
       
    33     var('y')
       
    34    
       
    35 Now if you type::
       
    36 
       
    37     sin(y)
       
    38 
       
    39      sage simply returns the expression .
       
    40 
       
    41 thus now sage treats sin(y) as a symbolic expression . You can use
       
    42 this to do a lot of symbolic maths using sage's built-in constants and
       
    43 expressions .
       
    44 
       
    45 Try out ::
       
    46    
       
    47    var('x,alpha,y,beta') x^2/alpha^2+y^2/beta^2
       
    48  
       
    49 Similarly , we can define many algebraic and trigonometric expressions
       
    50 using sage .
       
    51 
       
    52 
       
    53 
       
    54 Sage also provides a few built-in constants which are commonly used in
       
    55 mathematics .
       
    56 
       
    57 example : pi,e,oo , Function n gives the numerical values of all these
       
    58     constants.
       
    59 
       
    60 For instance::
       
    61 
       
    62    n(e)
       
    63    
       
    64    2.71828182845905
       
    65 
       
    66 gives numerical value of e.
       
    67 
       
    68 If you look into the documentation of n by doing ::
       
    69 
       
    70    n(<Tab>
       
    71 
       
    72 You will see what all arguments it can take etc .. It will be very
       
    73 helpful if you look at the documentation of all functions introduced
       
    74       
       
    75 
       
    76 Also we can define the no of digits we wish to use in the numerical
       
    77 value . For this we have to pass an argument digits.  Type::
       
    78   
       
    79    n(pi, digits = 10)
       
    80 
       
    81 Apart from the constants sage also has a lot of builtin functions like
       
    82 sin,cos,sinh,cosh,log,factorial,gamma,exp,arcsin,arccos,arctan etc ...
       
    83 lets try some out on the sage notebook. ::
       
    84      
       
    85    sin(pi/2)
       
    86    
       
    87    arctan(oo)
       
    88      
       
    89    log(e,e)
       
    90 
       
    91 
       
    92 Given that we have defined variables like x,y etc .. , We can define
       
    93 an arbitrary function with desired name in the following way.::
       
    94 
       
    95        var('x') function(<tab> {{{ Just to show the documentation
       
    96        extend this line }}} function('f',x)
       
    97 
       
    98 Here f is the name of the function and x is the independent variable .
       
    99 Now we can define f(x) to be ::
       
   100 
       
   101      f(x) = x/2 + sin(x)
       
   102 
       
   103 Evaluating this function f for the value x=pi returns pi/2.::
       
   104 	   
       
   105 	   f(pi)
       
   106 
       
   107 We can also define function that are not continuous but defined
       
   108 piecewise.  We will be using a function which is a parabola between 0
       
   109 to 1 and a constant from 1 to 2 .  type the following as given on the
       
   110 screen::
       
   111       
       
   112 
       
   113       var('x') h(x)=x^2 g(x)=1 f=Piecewise(<Tab> {{{ Just to show the
       
   114       documentation extend this line }}}
       
   115       f=Piecewise([[(0,1),h(x)],[(1,2),g(x)]],x) f
       
   116 
       
   117 Checking f at 0.4, 1.4 and 3 :: f(0.4) f(1.4) f(3)
       
   118 
       
   119 for f(3) it raises a value not defined in domain error .
       
   120 
       
   121 
       
   122 Apart from operations on expressions and functions one can also use
       
   123 them for series .
       
   124 
       
   125 We first define a function f(n) in the way discussed above.::
       
   126 
       
   127    var('n') function('f', n)
       
   128 
       
   129 
       
   130 To sum the function for a range of discrete values of n, we use the
       
   131 sage function sum.
       
   132 
       
   133  For a convergent series , f(n)=1/n^2 we can say ::
       
   134    
       
   135    var('n') function('f', n)
       
   136 
       
   137    f(n) = 1/n^2
       
   138 
       
   139    sum(f(n), n, 1, oo)
       
   140 
       
   141 For the famous Madhava series :: var('n') function('f', n)
       
   142 
       
   143     f(n) = (-1)^(n-1)*1/(2*n - 1)
       
   144 
       
   145 This series converges to pi/4. It was used by ancient Indians to
       
   146 interpret pi.
       
   147 
       
   148 For a divergent series, sum would raise a an error 'Sum is
       
   149 divergent' :: 
       
   150 	
       
   151 	var('n') 
       
   152 	function('f', n) 
       
   153 	f(n) = 1/n sum(f(n), n,1, oo)
       
   154 
       
   155 
       
   156 
       
   157 
       
   158 We can perform simple calculus operation using sage
       
   159 
       
   160 For example lets try an expression first ::
       
   161 
       
   162     diff(x**2+sin(x),x) 2x+cos(x)
       
   163 
       
   164 The diff function differentiates an expression or a function . Its
       
   165 first argument is expression or function and second argument is the
       
   166 independent variable .
       
   167 
       
   168 We have already tried an expression now lets try a function ::
       
   169 
       
   170    f=exp(x^2)+arcsin(x) diff(f(x),x)
       
   171 
       
   172 To get a higher order differentiation we need to add an extra argument
       
   173 for order ::
       
   174  
       
   175    diff(<tab> diff(f(x),x,3)
       
   176 
       
   177 
       
   178 in this case it is 3.
       
   179 
       
   180 
       
   181 Just like differentiation of expression you can also integrate them ::
       
   182 
       
   183      x = var('x') s = integral(1/(1 + (tan(x))**2),x) s
       
   184 
       
   185 
       
   186 
       
   187 To find factors of an expression use the function factor
       
   188 
       
   189     factor(<tab> y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2) f =
       
   190     factor(y)
       
   191 
       
   192 One can also simplify complicated expression using sage ::
       
   193     f.simplify_full()
       
   194 
       
   195 This simplifies the expression fully . You can also do simplification
       
   196 of just the algebraic part and the trigonometric part ::
       
   197 
       
   198     f.simplify_exp() f.simplify_trig()
       
   199     
       
   200 
       
   201 One can also find roots of an equation by using find_root function::
       
   202 
       
   203     phi = var('phi') find_root(cos(phi)==sin(phi),0,pi/2)
       
   204 
       
   205 Lets substitute this solution into the equation and see we were
       
   206 correct ::
       
   207 
       
   208      var('phi') f(phi)=cos(phi)-sin(phi)
       
   209      root=find_root(f(phi)==0,0,pi/2) f.substitute(phi=root)
       
   210 
       
   211 
       
   212 as we can see the solution is almost equal to zero .
       
   213 
       
   214 
       
   215 We can also define symbolic matrices ::
       
   216 
       
   217 
       
   218 
       
   219    var('a,b,c,d') A=matrix([[a,1,0],[0,b,0],[0,c,d]]) A
       
   220 
       
   221 
       
   222 Now lets do some of the matrix operations on this matrix ::
       
   223 
       
   224 
       
   225     A.det() A.inverse()
       
   226 
       
   227 You can do ::
       
   228     
       
   229     A.<Tab>
       
   230 
       
   231 To see what all operations are available
       
   232 
       
   233 
       
   234 {{{ Part of the notebook with summary }}}
       
   235 
       
   236 So in this tutorial we learnt how to
       
   237 
       
   238 
       
   239 We learnt about defining symbolic expression and functions .  
       
   240 And some built-in constants and functions .  
       
   241 Getting value of built-in constants using n function.  
       
   242 Using Tab to see the documentation.  
       
   243 Also we learnt how to sum a series using sum function.  
       
   244 diff() and integrate() for calculus operations .  
       
   245 Finding roots , factors and simplifying expression using find_root(), 
       
   246 factor() , simplify_full, simplify_exp , simplify_trig .
       
   247 Substituting values in expression using substitute function.
       
   248 And finally creating symbolic matrices and performing operation on them .