Python/C2/Getting-started-with-symbolics/English

From Script | Spoken-Tutorial
Revision as of 11:31, 29 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
Show Slide 1

Containing title, name of the production team along with the logo of MHRD

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

Learning objectives

At the end of this tutorial, you will be able to,
  1. Define symbolic expressions in sage.
  2. Use built-in constants and functions.
  3. Perform Integration, differentiation using sage.
  4. Define matrices.
  5. Define Symbolic functions.
  6. Simplify and solve symbolic expressions and functions.


Show Slide 3

Pre-requisite slide

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

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

Open the sage notebook Have your Sage notebook opened. If not, pause the video and start you Sage notebook.
sin(y) On the sage notebook type
var('y') 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.
sin(y) Now if you type sin(y),Sage simply returns the expression.
var('x,alpha,y,beta')
x^2/alpha^2+y^2/beta^2
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.

var('theta')
sin(theta)*sin(theta)+cos(theta)*cos(theta)
We have defined 4 variables, x, y, alpha and beta and have defined a symbolic expression using them.

Here is an expression in theta

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.

Show Slide 4

Assignment 1

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


Continue from paused state

Show Slide 5

Solution 1

The solution is on your screen.

var('x,y') x^2+y^2 var('a,x,y') y^2-4*a*x

<pause for sometime,then continue>

n(pi)
n(e)
n(oo)
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.
n<Tab> 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.
n(pi, digits = 10) 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.
sin(pi/2)
arctan(oo)
log(e,e)
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.

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

Assignment 2

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

Find the value of the following.

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


Continue from paused state

Show Slide 7

Solution 2

The solutions are on your screen.

n(pi^2,digits=6) n(sin(pi/4)) n(log(23,e))

<pause for sometime,then continue>

var('x')
function('f',x)
Given that we have defined variables like x, y etc., we can define an arbitrary function with desired name in the following way.
f(x) = x/2 + sin(x) Here f is the name of the function and x is the independent variable . Now we can define f(x)
f(pi) Evaluating this function f for the value x=pi returns pi/2.
var('x')
h(x)=x^2
g(x)=1
f=Piecewise([[(0,1),h(x)],[(1,2),g(x)]],x)
f
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
var('n')
function('f', n)
We can also define functions convergent series and other series. We first define a function f(n) in the way discussed before.
var('n')
function('f', n)
f(n) = 1/n^2
sum(f(n), n, 1, oo)
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
f(n) = (-1)^(n-1)*1/(2*n - 1)
sum(f(n), n, 1, oo)
Let us now try another series
This series converges to pi/4. Pause the video here, try out the following exercise and resume the video.
Show Slide 8

Assignment 3

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.

Continue from paused state

Show Slide 9

Solution 3

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>

diff(x**2+sin(x),x) Moving on let us see how to perform simple calculus operations using Sage For example lets try an expression first
f = exp(x^2) + arcsin(x)
diff(f(x),x)
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
diff(f(x),x,3) To get a higher order differential we need to add an extra third argument for order
x = var('x')
s = integral(1/(1 + (tan(x))**2),x)
s
in this case it is 3. Just like differentiation of expression you can also integrate them
y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2)
f = factor(y)
Many a times we need to find factors of an expression, we can use the "factor" function
f.simplify_full() One can simplify complicated expression by using the function simplify.
f.simplify_exp()
f.simplify_trig()
This simplifies the expression fully. We can also do simplification of just the algebraic part and the trigonometric part
phi = var('phi')
find_root(cos(phi) == sin(phi),0,pi/2)
One can also find roots of an equation by using find_root function
var('phi')
f(phi) = cos(phi)-sin(phi)
root = find_root(f(phi) == 0,0,pi/2)
f.substitute(phi=root)
Let's substitute this solution into the equation and see we were correct
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.
Show Slide 10

Assignment 4

Differentiate the following.
  1. sin(x^3)+log(3x) , degree=2
  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.

Continue from paused state

Show Slide 11

Solution 4

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>

var('a,b,c,d')
A=matrix([[a,1,0],[0,b,0],[0,c,d]])
A
Lets us now try some matrix algebra symbolically
A.det()
A.inverse()
Now lets do some of the matrix operations on this matrix
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.
Show Slide 12

Assignment 5

Find the determinant and inverse of

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

Continue from paused state

Show Slide 13

Solution 5

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>

Show Slide 14

Summary slide

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.
  2. Use built-in constants like pi,e,oo and functions like sum,sin,cos,log,exp and many more.
  3. Use <Tab> to see the documentation of a function.
  4. Do simple calculus using functions - diff()--to find a differential of a function - integral()--to integrate an expression - simplify--to simplify complicated expression.
  5. Substitute values in expressions using substitute function.
  6. 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.


Show Slide 15

Self assessment questions slide

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)


Show Slide 16

Solution of self assessment questions on slide

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)


Show Slide 17

Acknowledgment slide

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

Contributors and Content Editors

Chandrika