getting-started-with-symbolics/script.rst
author anand
Thu, 11 Nov 2010 00:03:57 +0530
changeset 472 fcdec2d28c9a
parent 442 a9b71932cbfa
child 458 9a1c5d134feb
permissions -rw-r--r--
Checklist OK for `The other kinds of plots`
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
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    28
Hello friends and welcome to the tutorial on symbolics with sage.
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
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    33
.. #[Madhu: What is this line doing here. I don't see much use of it]
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
During the course of the tutorial we will learn
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    36
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    37
{{{ Show outline slide  }}}
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    38
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    39
* Defining symbolic expressions in sage.  
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    40
* Using built-in constants and functions. 
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    41
* Performing Integration, differentiation using sage. 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    42
* Defining matrices. 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    43
* Defining Symbolic functions.  
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    44
* Simplifying and solving symbolic expressions and functions.
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    45
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    46
We can use Sage for symbolic maths. 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    47
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    48
On the sage notebook type::
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
    sin(y)
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
It raises a name error saying that y is not defined. But in sage we
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    53
can declare y as a symbol using var function.
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    54
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    55
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
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    63
sage simply returns the expression.
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    64
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    65
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    66
Thus sage treats sin(y) as a symbolic expression . We can use
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    67
this to do  symbolic maths using sage's built-in constants and
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    68
expressions..
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
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    71
So let us try ::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    72
   
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    73
   var('x,alpha,y,beta') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    74
   x^2/alpha^2+y^2/beta^2
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    75
 
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    76
taking another example ::
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
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    81
Similarly, we can define many algebraic and trigonometric expressions using sage .
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    82
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    83
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    84
Following is an exercise that you must do. 
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
    85
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    86
%% %%  Define following expressions as symbolic expressions
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    87
in sage?
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
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    96
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    97
Sage also provides a few built-in constants which are commonly used in mathematics .
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    98
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
    99
example : pi,e,infinity , Function n gives the numerical values of all these constants.
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   100
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   101
{{{ Type n(pi) n(e) n(oo) On the sage notebook }}}
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   102
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   103
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   104
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   105
If you look into the documentation of function "n" by doing
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
.. #[Madhu: "documentation of the function "n"?]
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
::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   110
   n(<Tab>
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   111
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   112
You will see what all arguments it takes and what it returns. It will be very
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   113
helpful if you look at the documentation of all functions introduced through
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   114
this script.
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   115
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
Also we can define the no. of digits we wish to use in the numerical
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   119
value . For this we have to pass an argument digits.  Type
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   120
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   121
.. #[Madhu: "no of digits"? Also "We wish to obtain" than "we wish to
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   122
     use"?]
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   123
::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   124
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   125
   n(pi, digits = 10)
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
Apart from the constants sage also has a lot of builtin functions like
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   128
sin,cos,log,factorial,gamma,exp,arcsin etc ...
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   129
lets try some of them out on the sage notebook.
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
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
     
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   134
   sin(pi/2)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   135
   
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   136
   arctan(oo)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   137
     
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   138
   log(e,e)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   139
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   140
Following is are exercises that you must do. 
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
%% %% Find the values of the following constants upto 6 digits  precision 
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
   1. pi^2
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   145
   #. euler_gamma^2
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
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   148
%% %% Find the value of the following.
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   149
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   150
   1. sin(pi/4)
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   151
   #. ln(23)  
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   152
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   153
Please, pause the video here. Do the exercises and then continue. 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   154
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   155
The solutions are on your screen.
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   156
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   157
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
Given that we have defined variables like x,y etc .. , We can define
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   160
an arbitrary function with desired name in the following way.::
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
       var('x') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   163
       function('f',x)
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
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   166
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
   167
Now we can define f(x) to be ::
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(x) = x/2 + sin(x)
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
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
   172
	   
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   173
	   f(pi)
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
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
   176
piecewise.  Let us define a function which is a parabola between 0
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   177
to 1 and a constant from 1 to 2 .  Type the following as given on the
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   178
screen
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   179
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   180
::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   181
      
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   182
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   183
      var('x') 
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   184
      h(x)=x^2 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   185
      g(x)=1 
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   186
      f=Piecewise(<Tab>
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
{{{ Show the documentation of Piecewise }}} 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   189
    
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   190
::
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   191
      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
   192
      f
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   193
351
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
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
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   198
We can also define functions which are series 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   199
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   200
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   201
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
   202
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   203
   var('n') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   204
   function('f', n)
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
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
   208
sage function sum.
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
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
   211
   
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   212
   var('n') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   213
   function('f', n)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   214
   f(n) = 1/n^2
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   215
   sum(f(n), n, 1, oo)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   216
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   217
 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   218
Lets us now try another series ::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   219
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   220
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   221
    f(n) = (-1)^(n-1)*1/(2*n - 1)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   222
    sum(f(n), n, 1, oo)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   223
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   224
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   225
This series converges to pi/4. 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   226
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   227
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   228
Following  are exercises that you must do. 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   229
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   230
%% %% Define the piecewise function. 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   231
   f(x)=3x+2 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   232
   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
   233
   f(x)=4x^2
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   234
   between 4 to 6. 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   235
   
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   236
%% %% 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
   237
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   238
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
   239
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   240
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
   241
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   242
For example lets try an expression first ::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   243
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   244
    diff(x**2+sin(x),x) 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   245
    2x+cos(x)
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
The diff function differentiates an expression or a function. Its
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   248
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
   249
independent variable.
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
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
   252
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   253
   f=exp(x^2)+arcsin(x) 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   254
   diff(f(x),x)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   255
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   256
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
   257
for order ::
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
   diff(<tab> diff(f(x),x,3)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   260
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   261
in this case it is 3.
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   262
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   263
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   264
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
   265
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   266
     x = var('x') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   267
     s = integral(1/(1 + (tan(x))**2),x) 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   268
     s
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
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   271
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   272
Many a times we need to find factors of an expression ,we can use the "factor" function
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   273
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
    factor(<tab> 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   276
    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
   277
    f = factor(y)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   278
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   279
One can  simplify complicated expression ::
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
    f.simplify_full()
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   282
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   283
This simplifies the expression fully . We can also do simplification
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   284
of just the algebraic part and the trigonometric part ::
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
    f.simplify_exp() 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   287
    f.simplify_trig()
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   288
    
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   289
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   290
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   291
One can also find roots of an equation by using find_root function::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   292
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   293
    phi = var('phi') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   294
    find_root(cos(phi)==sin(phi),0,pi/2)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   295
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   296
Lets substitute this solution into the equation and see we were
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   297
correct ::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   298
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   299
     var('phi') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   300
     f(phi)=cos(phi)-sin(phi)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   301
     root=find_root(f(phi)==0,0,pi/2) 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   302
     f.substitute(phi=root)
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   303
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   304
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
   305
the solution we got was correct.
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   306
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   307
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
   308
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   309
%% %% Differentiate the following. 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   310
      
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   311
      1. sin(x^3)+log(3x)  , degree=2
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   312
      #. x^5*log(x^7)      , degree=4 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   313
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   314
%% %% Integrate the given expression 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   315
      
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   316
      sin(x^2)+exp(x^3) 
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   317
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   318
%% %% Find x
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   319
      cos(x^2)-log(x)=0
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   320
      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
   321
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   322
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
   323
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   324
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   325
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   326
Lets us now try some matrix algebra symbolically ::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   327
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   328
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   329
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   330
   var('a,b,c,d') 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   331
   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
   332
   A
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   333
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   334
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
   335
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   336
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   337
::
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   338
    A.det() 
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   339
    A.inverse()
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   340
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   341
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   342
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
   343
442
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   344
%% %% Find the determinant and inverse of :
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   345
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   346
      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
   347
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   348
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
   349
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   350
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   351
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   352
a9b71932cbfa Added exercises and slides to getting started with symbolics
Amit Sethi
parents: 376
diff changeset
   353
{{{ Show the summary slide }}}
351
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   354
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   355
So in this tutorial we learnt how to
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   356
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   357
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   358
* We learnt about defining symbolic expression and functions.  
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   359
* Using built-in constants and functions.  
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   360
* Using <Tab>  to see the documentation of a function.  
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   361
* Simple calculus operations .  
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   362
* Substituting values in expression using substitute function.
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   363
* Creating symbolic matrices and performing operation on them .
054117c9dd59 changed the name of symbolics to getting started with symbolics
amit
parents:
diff changeset
   364