<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC2%2FGetting-started-with-symbolics%2FEnglish</id>
		<title>Python/C2/Getting-started-with-symbolics/English - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC2%2FGetting-started-with-symbolics%2FEnglish"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C2/Getting-started-with-symbolics/English&amp;action=history"/>
		<updated>2026-04-08T20:06:59Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=Python/C2/Getting-started-with-symbolics/English&amp;diff=464&amp;oldid=prev</id>
		<title>Chandrika: Created page with '{| border=1 !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 &quot;…'</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C2/Getting-started-with-symbolics/English&amp;diff=464&amp;oldid=prev"/>
				<updated>2012-11-29T06:01:11Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;#039;{| border=1 !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 &amp;quot;…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{| border=1&lt;br /&gt;
!Visual Cue&lt;br /&gt;
!Narration&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 1 &lt;br /&gt;
&lt;br /&gt;
Containing title, name of the production team along with the logo of MHRD &lt;br /&gt;
| Hello friends and welcome to the tutorial on &amp;quot;Symbolics with Sage&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 2&lt;br /&gt;
&lt;br /&gt;
Learning objectives &lt;br /&gt;
| At the end of this tutorial, you will be able to,&lt;br /&gt;
&lt;br /&gt;
# Define symbolic expressions in sage.&lt;br /&gt;
# Use built-in constants and functions.&lt;br /&gt;
# Perform Integration, differentiation using sage.&lt;br /&gt;
# Define matrices.&lt;br /&gt;
# Define Symbolic functions.&lt;br /&gt;
# Simplify and solve symbolic expressions and functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 3 &lt;br /&gt;
&lt;br /&gt;
Pre-requisite slide &lt;br /&gt;
| Before beginning this tutorial,we would suggest you to complete the tutorial on &amp;quot;Getting started with sage notebook&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
In addition to a lot of other things, Sage can do Symbolic Math and we shall start with defining symbolic expressions in Sage.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Open the sage notebook &lt;br /&gt;
| Have your Sage notebook opened. If not, pause the video and start you Sage notebook.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  sin(y)&lt;br /&gt;
| On the sage notebook type&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  var('y')&lt;br /&gt;
| It raises a name error saying that &amp;lt;tt&amp;gt;y&amp;lt;/tt&amp;gt; is not defined. We need to declare &amp;lt;tt&amp;gt;y&amp;lt;/tt&amp;gt; as a symbol. We do it using the &amp;lt;tt&amp;gt;var&amp;lt;/tt&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  sin(y)&lt;br /&gt;
| Now if you type sin(y),Sage simply returns the expression.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  var('x,alpha,y,beta')&lt;br /&gt;
 x^2/alpha^2+y^2/beta^2&lt;br /&gt;
| Sage treats &amp;lt;tt&amp;gt;sin(y)&amp;lt;/tt&amp;gt; as a symbolic expression. We can use this to do symbolic math using Sage's built-in constants and expressions.&lt;br /&gt;
&lt;br /&gt;
Let us try out a few examples.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  var('theta')&lt;br /&gt;
 sin(theta)*sin(theta)+cos(theta)*cos(theta)&lt;br /&gt;
| We have defined 4 variables, &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;y&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;alpha&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;beta&amp;lt;/tt&amp;gt; and have defined a symbolic expression using them.&lt;br /&gt;
&lt;br /&gt;
Here is an expression in &amp;lt;tt&amp;gt;theta&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Now that you know how to define symbolic expressions in Sage, here is an exercise.&lt;br /&gt;
&lt;br /&gt;
Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 4 &lt;br /&gt;
&lt;br /&gt;
Assignment 1 &lt;br /&gt;
| Define following expressions as symbolic expressions in Sage.&lt;br /&gt;
&lt;br /&gt;
# x^2+y^2&lt;br /&gt;
# y^2-4ax&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state&lt;br /&gt;
&lt;br /&gt;
Show Slide 5&lt;br /&gt;
&lt;br /&gt;
Solution 1 &lt;br /&gt;
| The solution is on your screen.&lt;br /&gt;
&lt;br /&gt;
var('x,y') x^2+y^2 var('a,x,y') y^2-4*a*x&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&amp;lt;pause for sometime,then continue&amp;gt;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  n(pi)&lt;br /&gt;
 n(e)&lt;br /&gt;
 n(oo)&lt;br /&gt;
| Sage also provides built-in constants which are commonly used in mathematics, for instance pi, e, infinity. The function &amp;lt;tt&amp;gt;n&amp;lt;/tt&amp;gt; gives the numerical values of all these constants.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;n&amp;lt;Tab&amp;gt;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| If you look into the documentation of function &amp;lt;tt&amp;gt;n&amp;lt;/tt&amp;gt;&amp;lt;nowiki&amp;gt; by doing n&amp;lt;tab&amp;gt;, You will see what all arguments it takes and what it returns.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  n(pi, digits = 10)&lt;br /&gt;
| 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.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  sin(pi/2)&lt;br /&gt;
 arctan(oo)&lt;br /&gt;
 log(e,e)&lt;br /&gt;
| Apart from the constants Sage also has a lot of built-in functions like &amp;lt;tt&amp;gt;sin&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;cos&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;factorial&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;gamma&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;exp&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;arctan&amp;lt;/tt&amp;gt; which stands for arctangent etc ...&lt;br /&gt;
&lt;br /&gt;
Lets try some of them out on the Sage notebook.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 6&lt;br /&gt;
&lt;br /&gt;
Assignment 2 &lt;br /&gt;
| Find the values of the following constants upto 6 digits precision&lt;br /&gt;
&lt;br /&gt;
# pi^2&lt;br /&gt;
# euler_gamma^2&lt;br /&gt;
&lt;br /&gt;
Find the value of the following.&lt;br /&gt;
&lt;br /&gt;
# sin(pi/4)&lt;br /&gt;
# ln(23)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state&lt;br /&gt;
&lt;br /&gt;
Show Slide 7&lt;br /&gt;
&lt;br /&gt;
Solution 2 &lt;br /&gt;
| The solutions are on your screen.&lt;br /&gt;
&lt;br /&gt;
n(pi^2,digits=6) n(sin(pi/4)) n(log(23,e))&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&amp;lt;pause for sometime,then continue&amp;gt;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  var('x')&lt;br /&gt;
 function('f',x)&lt;br /&gt;
| Given that we have defined variables like x, y etc., we can define an arbitrary function with desired name in the following way.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f(x) = x/2 + sin(x)&lt;br /&gt;
| Here f is the name of the function and x is the independent variable . Now we can define f(x)&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f(pi)&lt;br /&gt;
| Evaluating this function f for the value x=pi returns pi/2.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  var('x')&lt;br /&gt;
 h(x)=x^2&lt;br /&gt;
 g(x)=1&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;f=Piecewise([[(0,1),h(x)],[(1,2),g(x)]],x)&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 f&lt;br /&gt;
| 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 &amp;lt;tt&amp;gt;Piecewise&amp;lt;/tt&amp;gt; which returns a piecewise function from a list of pairs. Type the following&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  var('n')&lt;br /&gt;
 function('f', n)&lt;br /&gt;
| We can also define functions convergent series and other series. We first define a function f(n) in the way discussed before.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  var('n')&lt;br /&gt;
 function('f', n)&lt;br /&gt;
 f(n) = 1/n^2&lt;br /&gt;
 sum(f(n), n, 1, oo)&lt;br /&gt;
| 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&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f(n) = (-1)^(n-1)*1/(2*n - 1)&lt;br /&gt;
 sum(f(n), n, 1, oo)&lt;br /&gt;
| Let us now try another series&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| This series converges to pi/4. Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 8 &lt;br /&gt;
&lt;br /&gt;
Assignment 3 &lt;br /&gt;
| Define the piecewise function&lt;br /&gt;
&lt;br /&gt;
 f(x)=3x+2 when x is in the closed interval 0 to 4.&lt;br /&gt;
 f(x)=4x^2 between 4 to 6.&lt;br /&gt;
&lt;br /&gt;
Sum of 1/(n^2-1) where n ranges from 1 to infinity.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state&lt;br /&gt;
&lt;br /&gt;
Show Slide 9&lt;br /&gt;
&lt;br /&gt;
Solution 3 &lt;br /&gt;
| The solution is on your screen&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;var('x') h(x) = 3*x+2 g(x) = 4*x^2 f = Piecewise([[(0,4),h(x)],[(4,6),g(x)]],x) f&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var('n') f = 1/(n^2-1) sum(f(n), n, 1, oo)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&amp;lt;pause for sometime,then continue&amp;gt;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  diff(x**2+sin(x),x)&lt;br /&gt;
| Moving on let us see how to perform simple calculus operations using Sage For example lets try an expression first&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f = exp(x^2) + arcsin(x)&lt;br /&gt;
 diff(f(x),x)&lt;br /&gt;
| The &amp;lt;tt&amp;gt;diff&amp;lt;/tt&amp;gt; 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&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  diff(f(x),x,3)&lt;br /&gt;
| To get a higher order differential we need to add an extra third argument for order&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  x = var('x')&lt;br /&gt;
 s = integral(1/(1 + (tan(x))**2),x)&lt;br /&gt;
 s&lt;br /&gt;
| in this case it is 3. Just like differentiation of expression you can also integrate them&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2)&lt;br /&gt;
 f = factor(y)&lt;br /&gt;
| Many a times we need to find factors of an expression, we can use the &amp;quot;factor&amp;quot; function&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f.simplify_full()&lt;br /&gt;
| One can simplify complicated expression by using the function &amp;lt;tt&amp;gt;simplify&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f.simplify_exp()&lt;br /&gt;
 f.simplify_trig()&lt;br /&gt;
| This simplifies the expression fully. We can also do simplification of just the algebraic part and the trigonometric part&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  phi = var('phi')&lt;br /&gt;
 find_root(cos(phi) == sin(phi),0,pi/2)&lt;br /&gt;
| One can also find roots of an equation by using &amp;lt;tt&amp;gt;find_root&amp;lt;/tt&amp;gt; function&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  var('phi')&lt;br /&gt;
 f(phi) = cos(phi)-sin(phi)&lt;br /&gt;
 root = find_root(f(phi) == 0,0,pi/2)&lt;br /&gt;
 f.substitute(phi=root)&lt;br /&gt;
| Let's substitute this solution into the equation and see we were correct&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| 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.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 10&lt;br /&gt;
&lt;br /&gt;
Assignment 4 &lt;br /&gt;
| Differentiate the following.&lt;br /&gt;
&lt;br /&gt;
# sin(x^3)+log(3x) , degree=2&lt;br /&gt;
# x^5*log(x^7) , degree=4&lt;br /&gt;
&lt;br /&gt;
Integrate the given expression&lt;br /&gt;
&lt;br /&gt;
sin(x^2)+exp(x^3)&lt;br /&gt;
&lt;br /&gt;
'''Find x'''&lt;br /&gt;
&lt;br /&gt;
cos(x^2)-log(x)=0 Does the equation have a root between 1,2.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state&lt;br /&gt;
&lt;br /&gt;
Show Slide 11&lt;br /&gt;
&lt;br /&gt;
Solution 4 &lt;br /&gt;
| The solution is on your screen&lt;br /&gt;
&lt;br /&gt;
var('x') f(x)= x^5*log(x^7) diff(f(x),x,5)&lt;br /&gt;
&lt;br /&gt;
var('x') integral(x*sin(x^2),x)&lt;br /&gt;
&lt;br /&gt;
var('x') f=cos(x^2)-log(x) find_root(f(x)==0,1,2)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&amp;lt;pause for sometime,then continue&amp;gt;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  var('a,b,c,d')&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;A=matrix([[a,1,0],[0,b,0],[0,c,d]])&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 A&lt;br /&gt;
| Lets us now try some matrix algebra symbolically&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  A.det()&lt;br /&gt;
 A.inverse()&lt;br /&gt;
| Now lets do some of the matrix operations on this matrix&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| 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.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 12&lt;br /&gt;
&lt;br /&gt;
Assignment 5 &lt;br /&gt;
| Find the determinant and inverse of&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;A = [[x,0,1][y,1,0][z,0,y]]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state&lt;br /&gt;
&lt;br /&gt;
Show Slide 13&lt;br /&gt;
&lt;br /&gt;
Solution 5 &lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| The solution is on your screen&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;var('x,y,z') A = matrix([[x,0,1],[y,1,0],[z,0,y]]) A.det() A.inverse()&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&amp;lt;pause for sometime,then continue&amp;gt;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 14&lt;br /&gt;
&lt;br /&gt;
Summary slide &lt;br /&gt;
| This brings us to the end of this tutorial. In this tutorial, we have learnt to,&lt;br /&gt;
&lt;br /&gt;
# Define symbolic expression and functions using the method &amp;lt;tt&amp;gt;var&amp;lt;/tt&amp;gt;.&lt;br /&gt;
# Use built-in constants like pi,e,oo and functions like sum,sin,cos,log,exp and many more.&lt;br /&gt;
# &amp;lt;nowiki&amp;gt;Use &amp;lt;Tab&amp;gt; to see the documentation of a function.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
# Do simple calculus using functions - diff()--to find a differential of a function - integral()--to integrate an expression - simplify--to simplify complicated expression.&lt;br /&gt;
# Substitute values in expressions using &amp;lt;tt&amp;gt;substitute&amp;lt;/tt&amp;gt; function.&lt;br /&gt;
# 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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 15 &lt;br /&gt;
&lt;br /&gt;
Self assessment questions slide &lt;br /&gt;
| Here are some self assessment questions for you to solve&lt;br /&gt;
&lt;br /&gt;
# How do you define a name 'y' as a symbol?&lt;br /&gt;
# Get the value of pi upto precision 5 digits using sage?&lt;br /&gt;
# Find third order differential function of&amp;lt;br/&amp;gt; f(x) = sin(x^2)+exp(x^3)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 16 &lt;br /&gt;
&lt;br /&gt;
Solution of self assessment questions on slide &lt;br /&gt;
| And the answers,&lt;br /&gt;
&lt;br /&gt;
# We define a symbol using the function &amp;lt;tt&amp;gt;var&amp;lt;/tt&amp;gt;.In this case it will be&amp;lt;br/&amp;gt;  var('y')&lt;br /&gt;
# The value of pi upto precision 5 digits is given as,&amp;lt;br/&amp;gt;  n(pi,5)&lt;br /&gt;
# The third order differential function can be found out by adding the third argument which states the order.The syntax will be,&amp;lt;br/&amp;gt;  diff(f(x),x,3)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 17&lt;br /&gt;
&lt;br /&gt;
Acknowledgment slide &lt;br /&gt;
| Hope you have enjoyed this tutorial and found it useful. Thank You!&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Chandrika</name></author>	</entry>

	</feed>