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