summaryrefslogtreecommitdiff
path: root/getting_started_with_symbolics/script.rst
blob: 2123ba0a3bc42bcd619b6473f96c771d52c1ccec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
.. Objectives
.. ----------

.. By the end of this tutorial, you will be able to

.. 1. Defining symbolic expressions in sage.  
.. # Using built-in constants and functions. 
.. # Performing Integration, differentiation using sage. 
.. # Defining matrices. 
.. # Defining Symbolic functions.  
.. # Simplifying and solving symbolic expressions and functions.


.. Prerequisites
.. -------------

..   1. getting started with sage notebook

     
.. Author              : Amit 
   Internal Reviewer   :  
   External Reviewer   :
   Language Reviewer   : Bhanukiran
   Checklist OK?       : <, if OK> [2010-10-05]

Symbolics with Sage
-------------------

.. L1

{{{ Show the  first slide containing title, name of the production
team along with the logo of MHRD }}}

.. R1

Hello friends and welcome to the tutorial on "Symbolics with Sage".

.. L2

{{{ Show objectives slide  }}}

.. R2

At the end of this tutorial, you will be able to,

 1. Define symbolic expressions in sage.  
 #. Use built-in constants and functions. 
 #. Perform Integration, differentiation using sage. 
 #. Define matrices. 
 #. Define Symbolic functions.  
 #. Simplify0and solve symbolic expressions and functions.

.. L3

{{{ Switch to the pre-requisite slide }}}

.. R3

Before beginning this tutorial,we would suggest you to complete the 
tutorial on "Getting started with sage notebook".

In addtion to a lot of other things, Sage can do Symbolic Math and 
we shall start with defining symbolic expressions in Sage. 

.. L4

{{{ Open the sage notebook }}}

.. R4

Have your Sage notebook opened. If not, pause the video and
start you Sage notebook right now. 

.. R5

On the sage notebook type

.. L5
::
   
    sin(y)

.. R6

It raises a name error saying that ``y`` is not defined. We need to
declare ``y`` as a symbol. We do it using the ``var`` function. 

.. L6
::

    var('y')

.. R7
   
Now if you type sin(y),Sage simply returns the expression.

.. L7
::

    sin(y)

.. R8

Sage treats ``sin(y)`` as a symbolic expression. We can use this to do
symbolic math using Sage's built-in constants and expressions.

Let us try out a few examples. 

.. L8
::
   
    var('x,alpha,y,beta') 
    x^2/alpha^2+y^2/beta^2

.. R9

We have defined 4 variables, ``x``, ``y``, ``alpha`` and ``beta`` and
have defined a symbolic expression using them.
 
Here is an expression in ``theta``  

.. L9
::
   
    var('theta')
    sin(theta)*sin(theta)+cos(theta)*cos(theta)

.. R10

Now that you know how to define symbolic expressions in Sage, here is
an exercise. 

Pause the video here, try out the following exercise and resume the video.

.. L10

.. L11

{{{ show slide showing exercise 1 }}}

.. R11

Define following expressions as symbolic expressions in Sage. 
   
   1. x^2+y^2
   #. y^2-4ax

.. L12

{{{continue from paused state}}}
{{{ show slide showing solution 1 }}}

.. R12

The solution is on your screen.
<pause for sometime,then continue>

.. R13

Sage also provides built-in constants which are commonly used in
mathematics, for instance pi, e, infinity. The function ``n`` gives
the numerical values of all these constants.

.. L13
:: 

    n(pi) 
    n(e) 
    n(oo)

.. R14
   
If you look into the documentation of function ``n`` by doing
n<tab>, You will see what all arguments it takes and what it returns.

.. L14
::

    n<Tab>

.. R15

It will be very helpful if you look at the documentation of all 
functions introduced in the course of this script.

Also we can define the number of digits we wish to have in the
constants. For this we have to pass an argument -- digits.

.. L15
::

    n(pi, digits = 10)

.. R16

Apart from the constants Sage also has a lot of built-in functions
like ``sin``, ``cos``, ``log``, ``factorial``, ``gamma``, ``exp``,
``arctan`` which stands for arctangent etc ...

Lets try some of them out on the Sage notebook.

.. L16
::
     
    sin(pi/2)
    arctan(oo)
    log(e,e)

.. R17

Pause the video here, try out the following exercise and resume the video.

.. L17

.. L18

{{{ show slide showing exercise 2 }}}

.. R18

Find the values of the following constants upto 6 digits precision
   
   1. pi^2
   #. euler_gamma^2


Find the value of the following.

   1. sin(pi/4)
   #. ln(23)  

.. L19

{{{continue from paused state}}}
{{{ show slide showing solution 2 }}}
 
.. R19

The solutions are on your screen.

  n(pi^2,digits=6)
  n(sin(pi/4))
  n(log(23,e))
 
<pause for sometime,then continue>

.. R20

Given that we have defined variables like x, y etc., we can define an
arbitrary function with desired name in the following way.

.. L20
::

    var('x') 
    function('f',x)

.. R21

Here f is the name of the function and x is the independent variable .
Now we can define f(x)  

.. L21
::

    f(x) = x/2 + sin(x)

.. R22

Evaluating this function f for the value x=pi returns pi/2.

.. L22
::

    f(pi)

.. R23

We can also define functions that are not continuous but defined
piecewise.  Let us define a function which is a parabola between 0
to 1 and a constant from 1 to 2 .We shall use the function ``Piecewise`` 
which returns a piecewise function from a list of pairs. 
Type the following 

.. L23
::
      
    var('x') 
    h(x)=x^2 
    g(x)=1 
    f=Piecewise([[(0,1),h(x)],[(1,2),g(x)]],x) 
    f

.. R24

We can also define functions convergent series and other series. 

We first define a function f(n) in the way discussed before.

.. L24
::

    var('n') 
    function('f', n)

.. R25

To sum the function for a range of discrete values of n, we use the
sage function sum.

For a convergent series , f(n)=1/n^2 we can say 

.. L25
::
   
    var('n') 
    function('f', n)
    f(n) = 1/n^2
    sum(f(n), n, 1, oo)

.. R26

Let us now try another series 

.. L26
::

    f(n) = (-1)^(n-1)*1/(2*n - 1)
    sum(f(n), n, 1, oo)

.. R27

This series converges to pi/4. 

Pause the video here, try out the following exercise and resume the video. 

.. L27

.. L28

{{{ show slide showing exercise 3 }}}

.. R28

Define the piecewise function

::
  
    f(x)=3x+2 when x is in the closed interval 0 to 4.  
    f(x)=4x^2 between 4 to 6.
   
Sum  of 1/(n^2-1) where n ranges from 1 to infinity. 

.. L29

{{{continue from paused state}}}
{{{ show slide showing solution 3 }}}

.. R29

The solution is on your screen

var('x')
h(x)=3*x+2
g(x)= 4*x^2
f = Piecewise([[(0,4),h(x)],[(4,6),g(x)]],x)
f

var('n')
f=1/(n^2-1)
sum(f(n), n, 1, oo)

<pause for sometime,then continue>

.. R30

Moving on let us see how to perform simple calculus operations 
using Sage

For example lets try an expression first 

.. L30
::

    diff(x**2+sin(x),x) 

.. R31

The ``diff`` function differentiates an expression or a function. It's
first argument is expression or function and second argument is the
independent variable.

We have already tried an expression now lets try a function 

.. L31
::

    f=exp(x^2)+arcsin(x) 
    diff(f(x),x)

.. R32

To get a higher order differential we need to add an extra third argument
for order 

.. L32
::
 
    diff(f(x),x,3)

.. R33

in this case it is 3.

Just like differentiation of expression you can also integrate them 

.. L33
::

    x = var('x') 
    s = integral(1/(1 + (tan(x))**2),x) 
    s

.. R34

Many a times we need to find factors of an expression, we can use the
"factor" function

.. L34
::

    y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2) 
    f = factor(y)

.. R35

One can simplify complicated expression by using the 
function ``simplify``. 

.. L35
::
    
    f.simplify_full()

.. R36

This simplifies the expression fully. We can also do simplification of
just the algebraic part and the trigonometric part 

.. L36
::

    f.simplify_exp() 
    f.simplify_trig()

.. R37
    
One can also find roots of an equation by using ``find_root`` function

.. L37
::

    phi = var('phi') 
    find_root(cos(phi)==sin(phi),0,pi/2)

.. R38

Let's substitute this solution into the equation and see we were
correct 

.. L38
::

    var('phi') 
    f(phi)=cos(phi)-sin(phi)
    root=find_root(f(phi)==0,0,pi/2) 
    f.substitute(phi=root)

.. R39

As we can see when we substitute the value the answer is almost = 0 showing 
the solution we got was correct.

Pause the video here, try out the following exercise and resume the video. 

.. L39

.. L40

{{{ show slide showing exercise 4 }}}

.. R40

Differentiate the following. 
      
      1. sin(x^3)+log(3x)  , degree=2
      #. x^5*log(x^7)      , degree=4 

Integrate the given expression 
      
      sin(x^2)+exp(x^3) 

Find x
      cos(x^2)-log(x)=0
      Does the equation have a root between 1,2. 

.. L41

{{{continue from paused state}}}
{{{ show slide showing solution 4 }}}

.. R41

The solution is on your screen

var('x')
f(x)= x^5*log(x^7)
diff(f(x),x,5)

var('x')
integral(x*sin(x^2),x)

var('x')
f=cos(x^2)-log(x)
find_root(f(x)==0,1,2)

<pause for sometime,then continue>

.. R42

Lets us now try some matrix algebra symbolically 

.. L42
::

    var('a,b,c,d') 
    A=matrix([[a,1,0],[0,b,0],[0,c,d]]) 
    A

.. R43

Now lets do some of the matrix operations on this matrix

.. L43
::
    
    A.det() 
    A.inverse()

.. R44

As we can see, we got the determinant and the inverse of the matrix 
respectively.

Pause the video here, try out the following exercise and resume the video.

.. L44

.. L45

{{{ show slide showing exercise 5 }}} 

.. R45

 Find the determinant and inverse of 

      A=[[x,0,1][y,1,0][z,0,y]]

.. L46

{{{continue from paused state}}}
{{{ show slide showing solution 5 }}}

.. R47

The solution is on your screen

var('x,y,z')
A=matrix([[x,0,1],[y,1,0],[z,0,y]])
A.det()
A.inverse()

<pause for sometime,then continue>

.. L48

{{{ Show the summary slide }}}

.. R48

This brings us to the end of this tutorial. In this tutorial, 
we have learnt to,

1. Define symbolic expression and functions using the method ``var``.
#. Use built-in constants like pi,e,oo and functions like 
   sum,sin,cos,log,exp and many more.  
#. Use <Tab> to see the documentation of a function. 
#. Do simple calculus using functions
   - diff()--to find a differential of a function
   - integral()--to integrate an expression
   - simplify--to simplify complicated expression. 
#. Substitute values in expressions using ``substitute`` function.
#. Create symbolic matrices and perform operations on them like--
   - det()--to find out the determinant of a matrix
   - inverse()--to find out the inverse of a matrix.

.. L49

{{{Show self assessment questions slide}}}

.. R49

Here are some self assessment questions for you to solve

1. How do you define a name 'y' as a symbol?


2. Get the value of pi upto precision 5 digits using sage?


3. Find third order differential function of

   f(x)=sin(x^2)+exp(x^3)
 
.. L50

{{{solution of self assessment questions on slide}}}

.. R50

And the answers,

1. We define a symbol using the function ``var``.In this case it will be
   ::

    var('y')

2. The value of pi upto precision 5 digits is given as,
   ::

    n(pi,5)

3. The third order differential function can be found out by adding the 
   third argument which states the order.The syntax will be,
   ::

    diff(f(x),x,3)

.. L51

{{{Show thank you slide}}}

.. R51

Hope you have enjoyed this tutorial and found it useful.
Thank You!