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