getting-started-with-symbolics/script.rst
author anand
Fri, 12 Nov 2010 00:40:47 +0530
changeset 491 ebfe3a675882
parent 458 9a1c5d134feb
child 492 de1998c3791e
permissions -rw-r--r--
checklist ok 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   :
491
ebfe3a675882 checklist ok for `getting started with lists`
anand
parents: 458
diff changeset
    23
   Language Reviewer   : Bhanukiran
376
3e947a3fa83e Added metadata for getting started with symbolics
amit
parents: 351
diff changeset
    24
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
3e947a3fa83e Added metadata for getting started with symbolics
amit
parents: 351
diff changeset
    25
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    26
Symbolics with Sage
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    27
-------------------
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    28
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    29
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
    30
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    31
{{{ Show welcome slide }}}
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    32
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    33
During the course of the tutorial we will learn
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    34
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    35
{{{ Show outline slide  }}}
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    36
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    37
* Defining symbolic expressions in Sage.  
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    38
* Using built-in constants and functions. 
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    39
* Performing Integration, differentiation using Sage. 
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    40
* Defining matrices. 
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    41
* Defining symbolic functions.  
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    42
* Simplifying and solving symbolic expressions and functions.
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    43
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    44
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
    45
start with defining symbolic expressions in Sage. 
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    46
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    47
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
    48
start you Sage notebook. 
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    49
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    50
On the sage notebook type::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    51
   
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    52
    sin(y)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    53
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    54
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
    55
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
    56
::
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    57
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    58
    var('y')
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    59
   
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    60
Now if you type::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    61
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    62
    sin(y)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    63
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    64
Sage simply returns the expression.
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    65
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    66
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
    67
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
    68
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    69
Let us try out a few examples. ::
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    70
   
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    71
   var('x,alpha,y,beta') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    72
   x^2/alpha^2+y^2/beta^2
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    73
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    74
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
    75
have defined a symbolic expression using them.
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    76
 
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    77
Here is an expression in ``theta``  ::
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    78
   
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    79
   var('theta')
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    80
   sin(theta)*sin(theta)+cos(theta)*cos(theta)
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    81
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    82
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
    83
an exercise. 
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    84
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    85
{{ show slide showing question 1 }}
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    86
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    87
%% %% 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
    88
   
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    89
   1. x^2+y^2
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    90
   #. y^2-4ax
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    91
  
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    92
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
    93
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    94
The solution is on your screen.
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    95
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    96
{{ show slide showing solution 1 }}
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    97
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
    98
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
    99
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
   100
the numerical values of all these constants.
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   101
:: 
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   102
    n(pi) 
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   103
    n(e) 
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   104
    n(oo)
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   105
   
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   106
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
   107
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   108
::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   109
   n(<Tab>
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   110
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   111
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
   112
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
   113
introduced in the course of this script.
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   114
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   115
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
   116
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
   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
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   120
   n(pi, digits = 10)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   121
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   122
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
   123
like ``sin``, ``cos``, ``log``, ``factorial``, ``gamma``, ``exp``,
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   124
``arcsin`` etc ...
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   125
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   126
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
   127
::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   128
     
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   129
   sin(pi/2)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   130
   
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   131
   arctan(oo)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   132
     
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   133
   log(e,e)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   134
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   135
Following are exercises that you must do. 
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   136
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   137
{{ show slide showing question 2 }}
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   138
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   139
%% %% 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
   140
      precision
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   141
   
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   142
   1. pi^2
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   143
   #. euler_gamma^2
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
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   146
%% %% Find the value of the following.
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   147
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   148
   1. sin(pi/4)
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   149
   #. ln(23)  
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   150
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   151
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
   152
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   153
The solutions are on your screen
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   154
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   155
{{ show slide showing solution 2 }}
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   156
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   157
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
   158
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
   159
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   160
       var('x') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   161
       function('f',x)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   162
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   163
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
   164
Now we can define f(x) to be ::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   165
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   166
     f(x) = x/2 + sin(x)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   167
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   168
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
   169
	   
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   170
	   f(pi)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   171
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   172
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
   173
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
   174
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
   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
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   178
      var('x') 
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   179
      h(x)=x^2 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   180
      g(x)=1 
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   181
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   182
      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
   183
      f
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   184
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   185
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
   186
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   187
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
   188
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   189
   var('n') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   190
   function('f', n)
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
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   193
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
   194
sage function sum.
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   195
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   196
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
   197
   
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   198
   var('n') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   199
   function('f', n)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   200
   f(n) = 1/n^2
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   201
   sum(f(n), n, 1, oo)
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
 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   204
Lets us now try another series ::
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
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   207
    f(n) = (-1)^(n-1)*1/(2*n - 1)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   208
    sum(f(n), n, 1, oo)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   209
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   210
This series converges to pi/4. 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   211
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   212
Following  are exercises that you must do. 
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   213
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   214
{{ show slide showing question 3 }}
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   215
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   216
%% %% Define the piecewise function. 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   217
   f(x)=3x+2 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   218
   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
   219
   f(x)=4x^2
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   220
   between 4 to 6. 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   221
   
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   222
%% %% 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
   223
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   224
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
   225
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   226
{{ show slide showing solution 3 }}
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   227
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   228
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
   229
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   230
For example lets try an expression first ::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   231
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   232
    diff(x**2+sin(x),x) 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   233
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   234
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
   235
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
   236
independent variable.
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   237
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   238
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
   239
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   240
   f=exp(x^2)+arcsin(x) 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   241
   diff(f(x),x)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   242
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   243
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
   244
for order ::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   245
 
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   246
   diff(f(x),x,3)
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   247
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   248
in this case it is 3.
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   249
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   250
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
   251
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   252
     x = var('x') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   253
     s = integral(1/(1 + (tan(x))**2),x) 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   254
     s
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   255
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   256
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
   257
"factor" function
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   258
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   259
::
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   260
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   261
    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
   262
    f = factor(y)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   263
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   264
One can simplify complicated expression ::
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   265
    
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   266
    f.simplify_full()
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   267
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   268
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
   269
just the algebraic part and the trigonometric part ::
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   270
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   271
    f.simplify_exp() 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   272
    f.simplify_trig()
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   273
    
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   274
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
   275
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   276
    phi = var('phi') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   277
    find_root(cos(phi)==sin(phi),0,pi/2)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   278
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   279
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
   280
correct ::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   281
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   282
     var('phi') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   283
     f(phi)=cos(phi)-sin(phi)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   284
     root=find_root(f(phi)==0,0,pi/2) 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   285
     f.substitute(phi=root)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   286
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   287
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
   288
the solution we got was correct.
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   289
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   290
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
   291
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   292
%% %% Differentiate the following. 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   293
      
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   294
      1. sin(x^3)+log(3x)  , degree=2
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   295
      #. x^5*log(x^7)      , degree=4 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   296
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   297
%% %% Integrate the given expression 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   298
      
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   299
      sin(x^2)+exp(x^3) 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   300
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   301
%% %% Find x
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   302
      cos(x^2)-log(x)=0
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   303
      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
   304
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   305
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
   306
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   307
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   308
Lets us now try some matrix algebra symbolically ::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   309
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   310
   var('a,b,c,d') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   311
   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
   312
   A
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   313
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   314
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
   315
::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   316
    A.det() 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   317
    A.inverse()
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   318
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   319
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   320
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
   321
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   322
%% %% Find the determinant and inverse of :
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   323
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   324
      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
   325
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   326
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
   327
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   328
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   329
{{{ Show the summary slide }}}
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   330
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   331
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
   332
how to
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   333
458
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   334
* define symbolic expression and functions
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   335
* use built-in constants and functions  
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   336
* use <Tab> to see the documentation of a function  
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   337
* do simple calculus
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   338
* substitute values in expressions using ``substitute`` function
9a1c5d134feb Reviewed getting started with symbolics.
Puneeth Chaganti <punchagan@fossee.in>
parents: 442
diff changeset
   339
* create symbolic matrices and perform operations on them
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   340