23 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
23 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
24 |
24 |
25 Symbolics with Sage |
25 Symbolics with Sage |
26 ------------------- |
26 ------------------- |
27 |
27 |
28 Hello friends and welcome to the tutorial on symbolics with sage. |
28 Hello friends and welcome to the tutorial on Symbolics with Sage. |
29 |
29 |
30 {{{ Show welcome slide }}} |
30 {{{ Show welcome slide }}} |
31 |
31 |
32 |
|
33 .. #[Madhu: What is this line doing here. I don't see much use of it] |
|
34 |
|
35 During the course of the tutorial we will learn |
32 During the course of the tutorial we will learn |
36 |
33 |
37 {{{ Show outline slide }}} |
34 {{{ Show outline slide }}} |
38 |
35 |
39 * Defining symbolic expressions in sage. |
36 * Defining symbolic expressions in Sage. |
40 * Using built-in constants and functions. |
37 * Using built-in constants and functions. |
41 * Performing Integration, differentiation using sage. |
38 * Performing Integration, differentiation using Sage. |
42 * Defining matrices. |
39 * Defining matrices. |
43 * Defining Symbolic functions. |
40 * Defining symbolic functions. |
44 * Simplifying and solving symbolic expressions and functions. |
41 * Simplifying and solving symbolic expressions and functions. |
45 |
42 |
46 We can use Sage for symbolic maths. |
43 Amongst a lot of other things, Sage can do Symbolic Math and we shall |
|
44 start with defining symbolic expressions in Sage. |
|
45 |
|
46 Hope you have your Sage notebook open. If not, pause the video and |
|
47 start you Sage notebook. |
47 |
48 |
48 On the sage notebook type:: |
49 On the sage notebook type:: |
49 |
50 |
50 sin(y) |
51 sin(y) |
51 |
52 |
52 It raises a name error saying that y is not defined. But in sage we |
53 It raises a name error saying that ``y`` is not defined. We need to |
53 can declare y as a symbol using var function. |
54 declare ``y`` as a symbol. We do it using the ``var`` function. |
54 |
55 :: |
55 |
56 |
56 :: |
|
57 var('y') |
57 var('y') |
58 |
58 |
59 Now if you type:: |
59 Now if you type:: |
60 |
60 |
61 sin(y) |
61 sin(y) |
62 |
62 |
63 sage simply returns the expression. |
63 Sage simply returns the expression. |
64 |
64 |
65 |
65 Sage treats ``sin(y)`` as a symbolic expression. We can use this to do |
66 Thus sage treats sin(y) as a symbolic expression . We can use |
66 symbolic maths using Sage's built-in constants and expressions. |
67 this to do symbolic maths using sage's built-in constants and |
67 |
68 expressions.. |
68 Let us try out a few examples. :: |
69 |
|
70 |
|
71 So let us try :: |
|
72 |
69 |
73 var('x,alpha,y,beta') |
70 var('x,alpha,y,beta') |
74 x^2/alpha^2+y^2/beta^2 |
71 x^2/alpha^2+y^2/beta^2 |
|
72 |
|
73 We have defined 4 variables, ``x``, ``y``, ``alpha`` and ``beta`` and |
|
74 have defined a symbolic expression using them. |
75 |
75 |
76 taking another example :: |
76 Here is an expression in ``theta`` :: |
77 |
77 |
78 var('theta') |
78 var('theta') |
79 sin(theta)*sin(theta)+cos(theta)*cos(theta) |
79 sin(theta)*sin(theta)+cos(theta)*cos(theta) |
80 |
80 |
81 Similarly, we can define many algebraic and trigonometric expressions using sage . |
81 Now that you know how to define symbolic expressions in Sage, here is |
82 |
82 an exercise. |
83 |
83 |
84 Following is an exercise that you must do. |
84 {{ show slide showing question 1 }} |
85 |
85 |
86 %% %% Define following expressions as symbolic expressions |
86 %% %% Define following expressions as symbolic expressions in Sage. |
87 in sage? |
|
88 |
87 |
89 1. x^2+y^2 |
88 1. x^2+y^2 |
90 #. y^2-4ax |
89 #. y^2-4ax |
91 |
90 |
92 Please, pause the video here. Do the exercise and then continue. |
91 Please, pause the video here. Do the exercise and then continue. |
93 |
92 |
94 The solution is on your screen. |
93 The solution is on your screen. |
95 |
94 |
96 |
95 {{ show slide showing solution 1 }} |
97 Sage also provides a few built-in constants which are commonly used in mathematics . |
96 |
98 |
97 Sage also provides built-in constants which are commonly used in |
99 example : pi,e,infinity , Function n gives the numerical values of all these constants. |
98 mathematics, for instance pi, e, infinity. The function ``n`` gives |
100 |
99 the numerical values of all these constants. |
101 {{{ Type n(pi) n(e) n(oo) On the sage notebook }}} |
100 :: |
102 |
101 n(pi) |
103 |
102 n(e) |
104 |
103 n(oo) |
105 If you look into the documentation of function "n" by doing |
104 |
106 |
105 If you look into the documentation of function ``n`` by doing |
107 .. #[Madhu: "documentation of the function "n"?] |
|
108 |
106 |
109 :: |
107 :: |
110 n(<Tab> |
108 n(<Tab> |
111 |
109 |
112 You will see what all arguments it takes and what it returns. It will be very |
110 You will see what all arguments it takes and what it returns. It will |
113 helpful if you look at the documentation of all functions introduced through |
111 be very helpful if you look at the documentation of all functions |
114 this script. |
112 introduced in the course of this script. |
115 |
113 |
116 |
114 Also we can define the number of digits we wish to have in the |
117 |
115 constants. For this we have to pass an argument -- digits. Type |
118 Also we can define the no. of digits we wish to use in the numerical |
116 |
119 value . For this we have to pass an argument digits. Type |
|
120 |
|
121 .. #[Madhu: "no of digits"? Also "We wish to obtain" than "we wish to |
|
122 use"?] |
|
123 :: |
117 :: |
124 |
118 |
125 n(pi, digits = 10) |
119 n(pi, digits = 10) |
126 |
120 |
127 Apart from the constants sage also has a lot of builtin functions like |
121 Apart from the constants Sage also has a lot of built-in functions |
128 sin,cos,log,factorial,gamma,exp,arcsin etc ... |
122 like ``sin``, ``cos``, ``log``, ``factorial``, ``gamma``, ``exp``, |
129 lets try some of them out on the sage notebook. |
123 ``arcsin`` etc ... |
130 |
124 |
131 |
125 Lets try some of them out on the Sage notebook. |
132 :: |
126 :: |
133 |
127 |
134 sin(pi/2) |
128 sin(pi/2) |
135 |
129 |
136 arctan(oo) |
130 arctan(oo) |
137 |
131 |
138 log(e,e) |
132 log(e,e) |
139 |
133 |
140 Following is are exercises that you must do. |
134 Following are exercises that you must do. |
141 |
135 |
142 %% %% Find the values of the following constants upto 6 digits precision |
136 {{ show slide showing question 2 }} |
|
137 |
|
138 %% %% Find the values of the following constants upto 6 digits |
|
139 precision |
143 |
140 |
144 1. pi^2 |
141 1. pi^2 |
145 #. euler_gamma^2 |
142 #. euler_gamma^2 |
146 |
143 |
147 |
144 |
148 %% %% Find the value of the following. |
145 %% %% Find the value of the following. |
149 |
146 |
150 1. sin(pi/4) |
147 1. sin(pi/4) |
151 #. ln(23) |
148 #. ln(23) |
152 |
149 |
153 Please, pause the video here. Do the exercises and then continue. |
150 Please, pause the video here. Do the exercises and then continue. |
154 |
151 |
155 The solutions are on your screen. |
152 The solutions are on your screen |
156 |
153 |
157 |
154 {{ show slide showing solution 2 }} |
158 |
155 |
159 Given that we have defined variables like x,y etc .. , We can define |
156 Given that we have defined variables like x, y etc., we can define an |
160 an arbitrary function with desired name in the following way.:: |
157 arbitrary function with desired name in the following way.:: |
161 |
158 |
162 var('x') |
159 var('x') |
163 function('f',x) |
160 function('f',x) |
164 |
|
165 |
161 |
166 Here f is the name of the function and x is the independent variable . |
162 Here f is the name of the function and x is the independent variable . |
167 Now we can define f(x) to be :: |
163 Now we can define f(x) to be :: |
168 |
164 |
169 f(x) = x/2 + sin(x) |
165 f(x) = x/2 + sin(x) |
254 diff(f(x),x) |
240 diff(f(x),x) |
255 |
241 |
256 To get a higher order differential we need to add an extra third argument |
242 To get a higher order differential we need to add an extra third argument |
257 for order :: |
243 for order :: |
258 |
244 |
259 diff(<tab> diff(f(x),x,3) |
245 diff(f(x),x,3) |
260 |
246 |
261 in this case it is 3. |
247 in this case it is 3. |
262 |
|
263 |
248 |
264 Just like differentiation of expression you can also integrate them :: |
249 Just like differentiation of expression you can also integrate them :: |
265 |
250 |
266 x = var('x') |
251 x = var('x') |
267 s = integral(1/(1 + (tan(x))**2),x) |
252 s = integral(1/(1 + (tan(x))**2),x) |
268 s |
253 s |
269 |
254 |
270 |
255 Many a times we need to find factors of an expression, we can use the |
271 |
256 "factor" function |
272 Many a times we need to find factors of an expression ,we can use the "factor" function |
257 |
273 |
258 :: |
274 :: |
259 |
275 factor(<tab> |
|
276 y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2) |
260 y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2) |
277 f = factor(y) |
261 f = factor(y) |
278 |
262 |
279 One can simplify complicated expression :: |
263 One can simplify complicated expression :: |
280 |
264 |
281 f.simplify_full() |
265 f.simplify_full() |
282 |
266 |
283 This simplifies the expression fully . We can also do simplification |
267 This simplifies the expression fully. We can also do simplification of |
284 of just the algebraic part and the trigonometric part :: |
268 just the algebraic part and the trigonometric part :: |
285 |
269 |
286 f.simplify_exp() |
270 f.simplify_exp() |
287 f.simplify_trig() |
271 f.simplify_trig() |
288 |
272 |
289 |
273 One can also find roots of an equation by using ``find_root`` function:: |
290 |
|
291 One can also find roots of an equation by using find_root function:: |
|
292 |
274 |
293 phi = var('phi') |
275 phi = var('phi') |
294 find_root(cos(phi)==sin(phi),0,pi/2) |
276 find_root(cos(phi)==sin(phi),0,pi/2) |
295 |
277 |
296 Lets substitute this solution into the equation and see we were |
278 Let's substitute this solution into the equation and see we were |
297 correct :: |
279 correct :: |
298 |
280 |
299 var('phi') |
281 var('phi') |
300 f(phi)=cos(phi)-sin(phi) |
282 f(phi)=cos(phi)-sin(phi) |
301 root=find_root(f(phi)==0,0,pi/2) |
283 root=find_root(f(phi)==0,0,pi/2) |
346 A=[[x,0,1][y,1,0][z,0,y]] |
323 A=[[x,0,1][y,1,0][z,0,y]] |
347 |
324 |
348 Please, pause the video here. Do the exercise(s) and then continue. |
325 Please, pause the video here. Do the exercise(s) and then continue. |
349 |
326 |
350 |
327 |
351 |
|
352 |
|
353 {{{ Show the summary slide }}} |
328 {{{ Show the summary slide }}} |
354 |
329 |
355 So in this tutorial we learnt how to |
330 That brings us to the end of this tutorial. In this tutorial we learnt |
356 |
331 how to |
357 |
332 |
358 * We learnt about defining symbolic expression and functions. |
333 * define symbolic expression and functions |
359 * Using built-in constants and functions. |
334 * use built-in constants and functions |
360 * Using <Tab> to see the documentation of a function. |
335 * use <Tab> to see the documentation of a function |
361 * Simple calculus operations . |
336 * do simple calculus |
362 * Substituting values in expression using substitute function. |
337 * substitute values in expressions using ``substitute`` function |
363 * Creating symbolic matrices and performing operation on them . |
338 * create symbolic matrices and perform operations on them |
364 |
339 |