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