author | Puneeth Chaganti <punchagan@fossee.in> |
Wed, 06 Oct 2010 19:32:18 +0530 | |
changeset 240 | f4c36aa38a84 |
parent 205 | d95288e57cfc |
permissions | -rw-r--r-- |
148 | 1 |
Symbolics with Sage |
2 |
------------------- |
|
3 |
||
4 |
This tutorial on using Sage for symbolic calculation is brought to you |
|
5 |
by Fossee group. |
|
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 | 9 |
{{{ Part of Notebook with title }}} |
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 | 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 | 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 | 20 |
During the course of the tutorial we will learn |
21 |
||
22 |
{{{ Part of Notebook with outline }}} |
|
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 | 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 | 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 | 43 |
|
44 |
On the sage notebook type:: |
|
45 |
||
46 |
sin(y) |
|
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 | 53 |
var('y') |
54 |
||
55 |
Now if you type:: |
|
56 |
||
57 |
sin(y) |
|
58 |
||
59 |
sage simply returns the expression . |
|
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 | 66 |
thus now sage treats sin(y) as a symbolic expression . You can use |
67 |
this to do a lot of symbolic maths using sage's built-in constants and |
|
68 |
expressions . |
|
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 | 78 |
|
79 |
var('x,alpha,y,beta') x^2/alpha^2+y^2/beta^2 |
|
80 |
||
81 |
Similarly , we can define many algebraic and trigonometric expressions |
|
82 |
using sage . |
|
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 | 85 |
|
86 |
||
87 |
Sage also provides a few built-in constants which are commonly used in |
|
88 |
mathematics . |
|
89 |
||
90 |
example : pi,e,oo , Function n gives the numerical values of all these |
|
91 |
constants. |
|
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 | 98 |
For instance:: |
99 |
||
100 |
n(e) |
|
101 |
||
102 |
2.71828182845905 |
|
103 |
||
104 |
gives numerical value of e. |
|
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 | 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 | 111 |
n(<Tab> |
112 |
||
113 |
You will see what all arguments it can take etc .. It will be very |
|
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 | 117 |
|
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 | 125 |
n(pi, digits = 10) |
126 |
||
127 |
Apart from the constants sage also has a lot of builtin functions like |
|
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 | 133 |
|
134 |
sin(pi/2) |
|
135 |
||
136 |
arctan(oo) |
|
137 |
||
138 |
log(e,e) |
|
139 |
||
140 |
||
141 |
Given that we have defined variables like x,y etc .. , We can define |
|
142 |
an arbitrary function with desired name in the following way.:: |
|
143 |
||
144 |
var('x') function(<tab> {{{ Just to show the documentation |
|
145 |
extend this line }}} function('f',x) |
|
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 | 151 |
Here f is the name of the function and x is the independent variable . |
152 |
Now we can define f(x) to be :: |
|
153 |
||
154 |
f(x) = x/2 + sin(x) |
|
155 |
||
156 |
Evaluating this function f for the value x=pi returns pi/2.:: |
|
157 |
||
158 |
f(pi) |
|
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 | 161 |
piecewise. We will be using a function which is a parabola between 0 |
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 | 168 |
|
169 |
||
170 |
var('x') h(x)=x^2 g(x)=1 f=Piecewise(<Tab> {{{ Just to show the |
|
171 |
documentation extend this line }}} |
|
172 |
f=Piecewise([[(0,1),h(x)],[(1,2),g(x)]],x) f |
|
173 |
||
174 |
Checking f at 0.4, 1.4 and 3 :: f(0.4) f(1.4) f(3) |
|
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 | 178 |
for f(3) it raises a value not defined in domain error . |
179 |
||
180 |
||
181 |
Apart from operations on expressions and functions one can also use |
|
182 |
them for series . |
|
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 | 187 |
We first define a function f(n) in the way discussed above.:: |
188 |
||
189 |
var('n') function('f', n) |
|
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 | 192 |
|
193 |
To sum the function for a range of discrete values of n, we use the |
|
194 |
sage function sum. |
|
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 | 197 |
|
198 |
var('n') function('f', n) |
|
199 |
||
200 |
f(n) = 1/n^2 |
|
201 |
||
202 |
sum(f(n), n, 1, oo) |
|
203 |
||
204 |
For the famous Madhava series :: var('n') function('f', n) |
|
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 | 210 |
f(n) = (-1)^(n-1)*1/(2*n - 1) |
211 |
||
212 |
This series converges to pi/4. It was used by ancient Indians to |
|
213 |
interpret pi. |
|
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 | 218 |
For a divergent series, sum would raise a an error 'Sum is |
219 |
divergent' :: |
|
220 |
||
221 |
var('n') |
|
222 |
function('f', n) |
|
223 |
f(n) = 1/n sum(f(n), n,1, oo) |
|
224 |
||
225 |
||
226 |
||
227 |
||
228 |
We can perform simple calculus operation using sage |
|
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 | 234 |
For example lets try an expression first :: |
235 |
||
236 |
diff(x**2+sin(x),x) 2x+cos(x) |
|
237 |
||
238 |
The diff function differentiates an expression or a function . Its |
|
239 |
first argument is expression or function and second argument is the |
|
240 |
independent variable . |
|
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 | 244 |
We have already tried an expression now lets try a function :: |
245 |
||
246 |
f=exp(x^2)+arcsin(x) diff(f(x),x) |
|
247 |
||
248 |
To get a higher order differentiation we need to add an extra argument |
|
249 |
for order :: |
|
250 |
||
251 |
diff(<tab> diff(f(x),x,3) |
|
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 | 254 |
|
255 |
in this case it is 3. |
|
256 |
||
257 |
||
258 |
Just like differentiation of expression you can also integrate them :: |
|
259 |
||
260 |
x = var('x') s = integral(1/(1 + (tan(x))**2),x) s |
|
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 | 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 | 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 | 267 |
|
205
d95288e57cfc
Review of Amit' Symbolics in Sage.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
187
diff
changeset
|
268 |
:: |
148 | 269 |
factor(<tab> y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2) f = |
270 |
factor(y) |
|
271 |
||
272 |
One can also simplify complicated expression using sage :: |
|
273 |
f.simplify_full() |
|
274 |
||
275 |
This simplifies the expression fully . You can also do simplification |
|
276 |
of just the algebraic part and the trigonometric part :: |
|
277 |
||
278 |
f.simplify_exp() f.simplify_trig() |
|
279 |
||
205
d95288e57cfc
Review of Amit' Symbolics in Sage.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
187
diff
changeset
|
280 |
.. #[Madhu: Separate lines?] |
148 | 281 |
|
282 |
One can also find roots of an equation by using find_root function:: |
|
283 |
||
284 |
phi = var('phi') find_root(cos(phi)==sin(phi),0,pi/2) |
|
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 | 288 |
Lets substitute this solution into the equation and see we were |
289 |
correct :: |
|
290 |
||
291 |
var('phi') f(phi)=cos(phi)-sin(phi) |
|
292 |
root=find_root(f(phi)==0,0,pi/2) f.substitute(phi=root) |
|
293 |
||
205
d95288e57cfc
Review of Amit' Symbolics in Sage.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
187
diff
changeset
|
294 |
.. #[Madhu: Separate lines?] |
148 | 295 |
|
296 |
as we can see the solution is almost equal to zero . |
|
297 |
||
205
d95288e57cfc
Review of Amit' Symbolics in Sage.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
187
diff
changeset
|
298 |
.. #[Madhu: So what?] |
148 | 299 |
|
300 |
We can also define symbolic matrices :: |
|
301 |
||
302 |
||
303 |
||
304 |
var('a,b,c,d') A=matrix([[a,1,0],[0,b,0],[0,c,d]]) A |
|
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 | 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 | 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 | 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 | 316 |
|
317 |
You can do :: |
|
318 |
||
319 |
A.<Tab> |
|
320 |
||
321 |
To see what all operations are available |
|
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 | 324 |
|
325 |
{{{ Part of the notebook with summary }}} |
|
326 |
||
327 |
So in this tutorial we learnt how to |
|
328 |
||
329 |
||
330 |
We learnt about defining symbolic expression and functions . |
|
331 |
And some built-in constants and functions . |
|
332 |
Getting value of built-in constants using n function. |
|
333 |
Using Tab to see the documentation. |
|
334 |
Also we learnt how to sum a series using sum function. |
|
335 |
diff() and integrate() for calculus operations . |
|
336 |
Finding roots , factors and simplifying expression using find_root(), |
|
337 |
factor() , simplify_full, simplify_exp , simplify_trig . |
|
338 |
Substituting values in expression using substitute function. |
|
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] |