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