#symbolics.rst#
author Puneeth Chaganti <punchagan@fossee.in>
Thu, 11 Nov 2010 10:37:26 +0530
changeset 465 78d20cd87c7e
parent 418 8a42b4203f6d
permissions -rw-r--r--
Added questions to writing python scripts.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
418
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
     1
Symbolics with Sage
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
     2
-------------------
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
     3
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
     4
Hello friends and welcome to this tutorial on symbolics with sage.
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
     5
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
     6
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
     7
.. #[Madhu: Sounds more or less like an ad!]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
     8
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
     9
{{{ Part of Notebook with title }}}
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    10
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    11
.. #[Madhu: Please make your instructions, instructional. While
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    12
     recording if I have to read this, think what you are actually
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    13
     meaning it will take a lot of time]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    14
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    15
We would be using simple mathematical functions on the sage notebook
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    16
for this tutorial.
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    17
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    18
.. #[Madhu: What is this line doing here. I don't see much use of it]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    19
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    20
During the course of the tutorial we will learn
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    21
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    22
{{{ Part of Notebook with outline }}}
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    23
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    24
To define symbolic expressions in sage.  Use built-in costants and
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    25
function. Integration, differentiation using sage. Defining
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    26
matrices. Defining Symbolic functions. Simplifying and solving
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    27
symbolic expressions and functions.
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    28
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    29
.. #[Nishanth]: The formatting is all messed up
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    30
                First fix the formatting and compile the rst
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    31
                The I shall review
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    32
.. #[Madhu: Please make the above items full english sentences, not
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    33
     the slides like points. The person recording should be able to
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    34
     read your script as is. It can read something like "we will learn
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    35
     how to define symbolic expressions in Sage, using built-in ..."]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    36
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    37
Using sage we can perform mathematical operations on symbols.
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    38
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    39
.. #[Madhu: Same mistake with period symbols! Please get the
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    40
     punctuation right. Also you may have to rephrase the above
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    41
     sentence as "We can use Sage to perform sybmolic mathematical
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    42
     operations" or such]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    43
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    44
On the sage notebook type::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    45
   
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    46
    sin(y)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    47
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    48
It raises a name error saying that y is not defined. But in sage we
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    49
can declare y as a symbol using var function.
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    50
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    51
.. #[Madhu: But is not required]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    52
::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    53
    var('y')
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    54
   
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    55
Now if you type::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    56
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    57
    sin(y)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    58
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    59
    sage simply returns the expression .
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    60
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    61
.. #[Madhu: Why is this line indented? Also full stop. When will you
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    62
     learn? Yes we can correct you. But corrections are for you to
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    63
     learn. If you don't learn from your mistakes, I don't know what
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    64
     to say]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    65
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    66
thus now sage treats sin(y) as a symbolic expression . You can use
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    67
this to do a lot of symbolic maths using sage's built-in constants and
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    68
expressions .
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    69
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    70
.. #[Madhu: "Thus now"? It sounds like Dus and Nou, i.e 10 and 9 in
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    71
     Hindi! Full stop again. "a lot" doesn't mean anything until you
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    72
     quantify it or give examples.]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    73
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    74
Try out
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    75
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    76
.. #[Madhu: "So let us try" sounds better]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    77
 ::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    78
   
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    79
   var('x,alpha,y,beta') x^2/alpha^2+y^2/beta^2
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    80
 
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    81
Similarly , we can define many algebraic and trigonometric expressions
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    82
using sage .
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    83
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    84
.. #[Madhu: comma again. Show some more examples?]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    85
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    86
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    87
Sage also provides a few built-in constants which are commonly used in
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    88
mathematics .
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    89
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    90
example : pi,e,oo , Function n gives the numerical values of all these
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    91
    constants.
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    92
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    93
.. #[Madhu: This doesn't sound like scripts. How will I read this
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    94
     while recording. Also if I were recording I would have read your
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    95
     third constant as Oh-Oh i.e. double O. It took me at least 30
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    96
     seconds to figure out it is infinity]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    97
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    98
For instance::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
    99
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   100
   n(e)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   101
   
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   102
   2.71828182845905
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   103
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   104
gives numerical value of e.
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   105
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   106
If you look into the documentation of n by doing
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   107
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   108
.. #[Madhu: "documentation of the function "n"?]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   109
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   110
::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   111
   n(<Tab>
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   112
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   113
You will see what all arguments it can take etc .. It will be very
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   114
helpful if you look at the documentation of all functions introduced
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   115
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   116
.. #[Madhu: What does etc .. mean in a script?]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   117
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   118
Also we can define the no of digits we wish to use in the numerical
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   119
value . For this we have to pass an argument digits.  Type
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   120
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   121
.. #[Madhu: "no of digits"? Also "We wish to obtain" than "we wish to
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   122
     use"?]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   123
::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   124
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   125
   n(pi, digits = 10)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   126
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   127
Apart from the constants sage also has a lot of builtin functions like
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   128
sin,cos,sinh,cosh,log,factorial,gamma,exp,arcsin,arccos,arctan etc ...
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   129
lets try some out on the sage notebook.
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   130
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   131
.. #[Madhu: Here "a lot" makes sense]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   132
::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   133
     
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   134
   sin(pi/2)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   135
   
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   136
   arctan(oo)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   137
     
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   138
   log(e,e)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   139
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   140
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   141
Given that we have defined variables like x,y etc .. , We can define
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   142
an arbitrary function with desired name in the following way.::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   143
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   144
       var('x') function(<tab> {{{ Just to show the documentation
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   145
       extend this line }}} function('f',x)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   146
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   147
.. #[Madhu: What will the person recording show in the documentation
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   148
     without a script for it? Please don't assume recorder can cook up
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   149
     things while recording. It is impractical]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   150
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   151
Here f is the name of the function and x is the independent variable .
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   152
Now we can define f(x) to be ::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   153
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   154
     f(x) = x/2 + sin(x)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   155
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   156
Evaluating this function f for the value x=pi returns pi/2.::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   157
	   
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   158
	   f(pi)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   159
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   160
We can also define functions that are not continuous but defined
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   161
piecewise.  We will be using a function which is a parabola between 0
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   162
to 1 and a constant from 1 to 2 .  type the following as given on the
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   163
screen
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   164
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   165
.. #[Madhu: Instead of "We will be using ..." how about "Let us define
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   166
     a function ..."]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   167
::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   168
      
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   169
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   170
      var('x') h(x)=x^2 g(x)=1 f=Piecewise(<Tab> {{{ Just to show the
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   171
      documentation extend this line }}}
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   172
      f=Piecewise([[(0,1),h(x)],[(1,2),g(x)]],x) f
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   173
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   174
Checking f at 0.4, 1.4 and 3 :: f(0.4) f(1.4) f(3)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   175
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   176
.. #[Madhu: Again this doesn't sound like a script]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   177
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   178
for f(3) it raises a value not defined in domain error .
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   179
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   180
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   181
Apart from operations on expressions and functions one can also use
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   182
them for series .
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   183
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   184
.. #[Madhu: I am not able to understand this line. "Use them as
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   185
.. series". Use what as series?]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   186
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   187
We first define a function f(n) in the way discussed above.::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   188
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   189
   var('n') function('f', n)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   190
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   191
.. #[Madhu: Shouldn't this be on 2 separate lines?]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   192
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   193
To sum the function for a range of discrete values of n, we use the
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   194
sage function sum.
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   195
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   196
For a convergent series , f(n)=1/n^2 we can say ::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   197
   
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   198
   var('n') function('f', n)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   199
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   200
   f(n) = 1/n^2
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   201
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   202
   sum(f(n), n, 1, oo)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   203
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   204
For the famous Madhava series :: var('n') function('f', n)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   205
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   206
.. #[Madhu: What is this? your double colon says it must be code block
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   207
     but where is the indentation and other things. How will the
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   208
     recorder know about it?]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   209
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   210
    f(n) = (-1)^(n-1)*1/(2*n - 1)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   211
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   212
This series converges to pi/4. It was used by ancient Indians to
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   213
interpret pi.
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   214
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   215
.. #[Madhu: I am losing the context. Please add something to bring
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   216
     this thing to the context]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   217
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   218
For a divergent series, sum would raise a an error 'Sum is
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   219
divergent' :: 
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   220
	
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   221
	var('n') 
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   222
	function('f', n) 
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   223
	f(n) = 1/n sum(f(n), n,1, oo)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   224
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   225
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   226
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   227
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   228
We can perform simple calculus operation using sage
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   229
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   230
.. #[Madhu: When you switch to irrelevant topics make sure you use
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   231
    some connectors in English like "Moving on let us see how to
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   232
    perform simple calculus operations using Sage" or something like
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   233
    that]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   234
For example lets try an expression first ::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   235
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   236
    diff(x**2+sin(x),x) 2x+cos(x)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   237
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   238
The diff function differentiates an expression or a function . Its
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   239
first argument is expression or function and second argument is the
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   240
independent variable .
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   241
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   242
.. #[Madhu: Full stop, Full stop, Full stop]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   243
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   244
We have already tried an expression now lets try a function ::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   245
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   246
   f=exp(x^2)+arcsin(x) diff(f(x),x)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   247
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   248
To get a higher order differentiation we need to add an extra argument
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   249
for order ::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   250
 
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   251
   diff(<tab> diff(f(x),x,3)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   252
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   253
.. #[Madhu: Please try to be more explicit saying third argument]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   254
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   255
in this case it is 3.
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   256
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   257
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   258
Just like differentiation of expression you can also integrate them ::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   259
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   260
     x = var('x') s = integral(1/(1 + (tan(x))**2),x) s
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   261
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   262
.. #[Madhu: Two separate lines.]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   263
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   264
To find the factors of an expression use the "factor" function
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   265
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   266
.. #[Madhu: See the diff]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   267
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   268
::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   269
    factor(<tab> y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2) f =
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   270
    factor(y)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   271
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   272
One can also simplify complicated expression using sage ::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   273
    f.simplify_full()
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   274
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   275
This simplifies the expression fully . You can also do simplification
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   276
of just the algebraic part and the trigonometric part ::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   277
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   278
    f.simplify_exp() f.simplify_trig()
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   279
    
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   280
.. #[Madhu: Separate lines?]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   281
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   282
One can also find roots of an equation by using find_root function::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   283
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   284
    phi = var('phi') find_root(cos(phi)==sin(phi),0,pi/2)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   285
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   286
.. #[Madhu: Separate lines?]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   287
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   288
Lets substitute this solution into the equation and see we were
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   289
correct ::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   290
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   291
     var('phi') f(phi)=cos(phi)-sin(phi)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   292
     root=find_root(f(phi)==0,0,pi/2) f.substitute(phi=root)
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   293
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   294
.. #[Madhu: Separate lines?]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   295
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   296
as we can see the solution is almost equal to zero .
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   297
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   298
.. #[Madhu: So what?]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   299
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   300
We can also define symbolic matrices ::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   301
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   302
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   303
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   304
   var('a,b,c,d') A=matrix([[a,1,0],[0,b,0],[0,c,d]]) A
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   305
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   306
.. #[Madhu: Why don't you break the lines?]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   307
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   308
Now lets do some of the matrix operations on this matrix
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   309
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   310
.. #[Madhu: Why don't you break the lines? Also how do you connect
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   311
     this up? Use some transformation keywords in English]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   312
::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   313
    A.det() A.inverse()
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   314
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   315
.. #[Madhu: Why don't you break the lines?]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   316
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   317
You can do ::
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   318
    
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   319
    A.<Tab>
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   320
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   321
To see what all operations are available
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   322
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   323
.. #[Madhu: Sounds very abrupt]
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   324
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   325
{{{ Part of the notebook with summary }}}
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   326
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   327
So in this tutorial we learnt how to
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   328
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   329
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   330
We learnt about defining symbolic expression and functions .  
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   331
And some built-in constants and functions .  
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   332
Getting value of built-in constants using n function.  
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   333
Using Tab to see the documentation.  
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   334
Also we learnt how to sum a series using sum function.  
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   335
diff() and integrate() for calculus operations .  
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   336
Finding roots , factors and simplifying expression using find_root(), 
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   337
factor() , simplify_full, simplify_exp , simplify_trig .
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   338
Substituting values in expression using substitute function.
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   339
And finally creating symbolic matrices and performing operation on them .
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   340
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   341
.. #[Madhu: See what Nishanth is doing. He has written this as
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   342
     points. So easy to read out while recording. You may want to
8a42b4203f6d "Merging heads"
Amit Sethi
parents:
diff changeset
   343
     reorganize like that]