Difference between revisions of "Python/C2/Getting-started-with-symbolics/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 118: Line 118:
 
| Define following expressions as symbolic expressions in Sage.
 
| Define following expressions as symbolic expressions in Sage.
  
|0-
+
|-
|3:11
+
|03:11
 
|that is  x squared plus y squared
 
|that is  x squared plus y squared
  

Revision as of 15:32, 3 September 2014

Time Narration
00:02 Hello friends and welcome to the tutorial on "Symbolics with Sage".
00:07 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.
00:24 Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with sage notebook".
00:31 In addition to a lot of other things, Sage can do Symbolic Math and we shall start with defining symbolic expressions in Sage.
00:42 Have your Sage notebook opened.
00:44 If not, pause the video and start you Sage notebook.
00:49 On the sage notebook type sine y.
01:08 Then click shift enter.
01:12 It raises a name error saying that y is not defined.
01:14 We need to declare y as a symbol.
01:17 We do it using the var function.
01:19 So type var within brackets and single quotes y.
01:28 Now if you type sin within brackets y, simply returns the expression.
01:32 So type sine y.
01:37 Now, sage treats sin of y as a symbolic expression.
01:42 We can use this to do symbolic math using Sage's built-in constants and expressions.
01:47 Let us try out a few examples.
01:50 So let us type var within brackets and single quotes x comma alpha comma y comma beta
01:59 Then next line you can type x charat 2 by alpha charat 2 plus y charat 2 by beta charat 2
02:10 That is x squared by alpha squared plus y squared by beta squared.
02:17 We have defined 4 variables, x, y, alpha and beta and have defined a symbolic expression using them.
02:25 Here is an expression in theta
02:29 So you can type var within brackets and single quotes theta
02:38 then sine within brackets theta multiplied by sine within brackets theta plus cos within brackets theta multiplied by cos within brackets theta
02:55 Now that you know how to define symbolic expressions in Sage, here is an exercise.
03:01 Pause the video here, try out the following exercise and resume the video.
03:05 Define following expressions as symbolic expressions in Sage.
03:11 that is x squared plus y squared
03:13 and next one is. y squared minus 4 ax
03:18 The solution is on your screen.
03:25 that is var within brackets and single quotes x,y then x squared plus y squared that is x charat 2 plus y charat 2.
03:33 then next is var within brackets and single quotes a,x,y then y charat 2 minus 4 into a into x
03:49 Sage also provides built-in constants which are commonly used in mathematics, for instance pi, e, infinity.
03:56 The function n gives the numerical values of all these constants.
04:00 So you can type n within brackets pi then n within brackets e then n within brackets zero zero that is oo.
04:18 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.
04:26 So you can type n and hit tab.
04:30 It will be very helpful if you look at the documentation of all functions introduced in the course of this script.
04:36 Also we can define the number of digits we wish to have in the constants.
04:40 For this we have to pass an argument -- digits.
04:46 So you can type n within brackets pi comma space digits is equal to 10.
05:01 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 ...
05:16 So let us try some of them out on the Sage notebook.
05:21 so you can type sine within brackets pi by 2 then artan oo then log within brackets
05:44 so when you type artan , there is an error in arc so we have to type arctan.
05:54 Then type log e comma e
06:03 Pause the video here, try out the following exercise and resume the video.
06:06 Find the values of the following constants upto 6 digits precision
06:14 First option is pi charat 2
06:18 then euler underscore gamma charat 2
06:23 Find the value of the following.
06:26 1. sin of pi divided by 4
06:28 Next one is . ln of 23.
06:32 The solutions are on your screen.
06:36 that is n into within brackets pi squared comma digits equal to 6,next one is n into within brackets sin pi by 4 and then third one is n into within brackets log 23 comma e
07:05 Given that we have defined variables like x, y etc., we can define an arbitrary function with desired name in the following way.
07:14 So you can type var within brackets and single quotes x and then next line function within brackets and single quotes f comma x
07:33 Here f is the name of the function and x is the independent variable .
07:37 Now we can define f of x
07:40 that is f of x within brackets x is equal to x by 2 plus sin x.
07:53 Evaluating this function f for the value x=pi returns pi by 2.
08:01 So type f within brackets pi
08:07 so we will get the answer as 1 by 2 into pi.
08:12 We can also define functions that are not continuous but defined piecewise.
08:18 Let us define a function which is a parabola between 0 to 1 and a constant from 1 to 2 .
08:24 We shall use the function Piecewise which returns a piecewise function from a list of pairs.
08:31 We can type the following
08:35 var within brackets in single quotes x
08:41 then h of x is equal to x charat 2
08:52 then g of x is equal to 1
08:58 then next line we can type f is equal to piecewise within brackets 0 comma 1 then comma h then another bracket x then next square bracket it is 1,2, g of x comma x then type f
09:21 We can also define functions convergent series and other series.
09:26 We first define a function f(n) in the way discussed before.
09:29 So we can type var within brackets n in single quotes
09:39 then type function within brackets ' f ',n
09:53 To sum the function for a range of discrete values of n, we use the sage function sum.
10:03 For a convergent series , f(n)=1 by n raised to 2 we can say by typing

var('n')

function('f', n)

f(n) = 1/n^2

sum(f(n), n, 1, oo)

10:55 Let us now try another series

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

11:33 This series converges to pi by 4.
11:40 Pause the video here, try out the following exercise and resume the video.
11:46 Define the piecewise function
11:47 f of x is equal to 3x plus 2 when x is in the closed interval 0 to 4.
11:55 f of x is equal to 4x squared between 4 to 6.
12:03 Sum of 1 by within brackets n squared -1 where n ranges from 1 to infinity.
12:11 The solution is on your screen
12:13 var('x') ,h(x) = 3 into x plus 2 g(x) is equal to 4 into x squared , f = Piecewise within brackets again square brackets and square brackets again and within closing brackets 0,4,h(x),(4,6),g(x),x
12:40 Next step you have to type var('n') f = 1/(n squared minus 1) sum(f(n), n, 1, oo)
13:00 Moving on let us see how to perform simple calculus operations using Sage
13:05 For example lets try an expression first
13:07 So type diff then with in brackets x star star 2 plus sin of x coma x
13:18 The diff function differentiates an expression or a function.
13:27 It's first argument is expression or function and second argument is the independent variable.
13:33 We have already tried an expression now lets try a function
13:41 f = exp(x^2) + arcsin(x)

diff(f(x),x)

14:00 To get a higher order differential we need to add an extra third argument for order so you can type

diff(f(x),x,3)

14:35 in this case it is 3.
14:38 Just like differentiation of expression you can also integrate them

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

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

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

15:46 One can simplify complicated expression by using the function simplify.

f.simplify_full()

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

f.simplify_exp()

16:24 f.simplify_trig()
16:33 One can also find roots of an equation by using find_root function

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

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

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

17:55 As we can see when we substitute the value the answer is almost = 0 showing the solution we got was correct.
18:04 Pause the video here, try out the following exercise and resume the video.
18:10 Differentiate the following.
18:12 1. sin(x cubed) plus log(3x) , degree=2
18:24 2. x raised to 5 into log x raised to 7 , degree=4
18:32 Integrate the given expression
18:33 x start sin into x square
18:44 Find x
18:45 cos(x squared)-log(x)=0
18:50 Does the equation have a root between 1,2.
18:55 The solution is on your screen
18:56 For the first one we have to type var('x') f(x)= x raised to 5 into log of x raised to 7 diff(f(x),x,5)
19:15 Next line we have to type var('x')then second line integral(x*sin(x^2),x)
19:33 For the third one we have to type var('x') then f=cos(x^2)-log(x) find_root(f(x)==0,1,2)
19:53 So let us now try some matrix algebra symbolically

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

20:29 Now lets do some of the matrix operations on this matrix

A.det() A.inverse()

20:46 As we can see, we got the determinant and the inverse of the matrix respectively.
20:50 So pause the video here, try out the following exercise and resume the video.
20:57 Find the determinant and inverse of
20:59 A = within brackets and again brackets x,0,1 then again brackets y,1,0 again bracket z,0,y
21:18 The solution is on your screen
21:20 var('x,y,z') A = matrix([[x,0,1],[y,1,0],[z,0,y]])then third line you have to type A dot det function and next line you have to type A dot inverse function
21:44 This brings us to the end of this tutorial.
21:48 In this tutorial, we have learnt to,
21:49 Define symbolic expression and functions using the method var.
21:53 Then use built-in constants like pi,e,oo and functions like sum,sin,cos,log,exp and many more.
22:00 Then use <Tab> to see the documentation of a function.
22:03 4. Do simple calculus using functions - diff()--to find a differential of a function - integral()--to integrate an expression - simplify--to simplify complicated expression.
22:16 5. Substitute values in expressions using substitute function.
22:19 Then 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.
22:29 Here are some self assessment questions for you to solve
22:32 1. How do you define a name 'y' as a symbol?
22:37 2. Get the value of pi upto precision 5 digits using sage?
22:41 3. Find third order differential function of
f(x) = sin(x^2)+exp(x^3)
22:50 So, the answers,
22:53 1. We define a symbol using the function var.
22:57 In this case it will be
var('y')
23:02 2. The value of pi upto precision 5 digits is given as,
n(pi,5)
23:11 3. The third order differential function can be found out by adding the third argument which states the order.
23:18 The syntax will be,
diff(f(x),x,3)
23:24 Hope you have enjoyed this tutorial and found it useful.

Contributors and Content Editors

Gaurav, Jyotisolanki, Minal, PoojaMoolya, Pravin1389