0% found this document useful (0 votes)
265 views20 pages

LAB 1 To LAB 10 - Jupyter Notebook

This document contains Python code for plotting various parametric curves and implicit functions using matplotlib. It includes programs to plot exponential, trigonometric, polynomial functions as well as parametric curves like circle, cycloid, cardioid, rose curves. There are also programs for finding radius of curvature and angle between two polar curves. The document contains examples of plotting implicit curves like circle, cissus, lemniscate through plot_implicit function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
265 views20 pages

LAB 1 To LAB 10 - Jupyter Notebook

This document contains Python code for plotting various parametric curves and implicit functions using matplotlib. It includes programs to plot exponential, trigonometric, polynomial functions as well as parametric curves like circle, cycloid, cardioid, rose curves. There are also programs for finding radius of curvature and angle between two polar curves. The document contains examples of plotting implicit curves like circle, cissus, lemniscate through plot_implicit function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

In [ ]: 1 #LAB 1

In [ ]: 1 #Program to plot curve connecting the points using plot and scatter function
2 ​
3 from pylab import * #Library which comprise of numpy and matplotlib
4 ​
5 x=[2,3,4,5,7] # x - values
6 y=[4,2,3,7,10] # y - values
7 ​
8 if len(x)!=len(y):
9 print("number of values of x and y doesnot match")
10 else:
11 scatter(x,y) #plot points in the plane
12 plot(x,y) #plot curve connecting the points
13 grid()
14 xlabel("x-axis")
15 ylabel("y-axis")
16 title("Scatter grph")
17 xlim(0,10)
18 ylim(0,10)
19 axvline(color="red")
20 axhline(color="red")
21 show()

In [ ]: 1 #Program to plot exponential function


2 ​
3 from pylab import * #Library which comprise of numpy and matplotlib
4 ​
5 x = arange(-5,5,0.001) # x takes the values between -10 and 10 with a
6 #step length of 0.001
7 y = exp(x) # Exponential function
8 plot(x,y) # plotting the points
9 title(" Exponential curve ") # giving a title to the graph
10 grid()
11 xlabel("x-axis")
12 ylabel("y-axis")
13 xlim(-10,10)
14 axvline(color="red")
15 axhline(color="red")
16 show()

In [ ]: 1 #Program to plot sin(x) and cos(x) together


2 ​
3 from pylab import * #Library which comprise of numpy and matplotlib
4 ​
5 x = linspace(-2*pi,2*pi,100) # x takes the values between -10 and 10 with
6 #a step length of 0.001
7 ​
8 plot(x,sin(x),x,cos(x)) # plotting the points
9 title(" Exponential curve ") # giving a title to the graph
10 grid()
11 xlabel("x-axis")
12 ylabel("y-axis")
13 title("Scatter grph")
14 xlim(-2*pi,2*pi)
15 axvline(color="red")
16 axhline(color="red")
17 show()
In [ ]: 1 #Program to plot functions x, x^2 and X^3 for same values of x
2 from pylab import *
3 ​
4 x= linspace (0 , 2 , 100 )
5 plot(x,x,label='linear ') # Plot of y=x a linear curve
6 plot(x,x**2,label='quadratic ') # Plot of y=x^2 a quadric curve
7 plot(x,x**3,label='cubic ') # Plot of y=x^3 a cubic curve
8 xlabel('x label ') # Add an x- label to the axes .
9 ylabel('y label ') # Add a y- label to the axes .
10 title(" Simple Plot ") # Add a title to the axes .
11 legend(loc="upper right") # Add a legend
12 axvline(color="red")
13 axhline(color="red")
14 ylim(0,6)
15 grid()
16 show() # to show the complete graph

In [ ]: 1 #Program to plot parametric curve circle x=r*cos(theta) and y=r sin(theta)


2 from pylab import *
3 #x=arange(-3,3,0.001)
4 theta=linspace(0,2*pi,1000)
5 x=3*cos(theta)
6 y=3*sin(theta)
7 plot(x,y)
8 axis("square")
9 title("$x=3 cos(\\theta) & y=3 sin(\\theta)$")
10 axvline(color="red")
11 axhline(color="red")
12 grid()
13 show()

In [ ]: 1 #Program to plot parametric curve cycloid x=r*(theta-sin(theta)) and


2 #y=r (1-cos(theta))
3 ​
4 from pylab import *
5 ​
6 theta=linspace(0,2*pi,1000)
7 x=3*(theta-sin(theta))
8 y=3*(1-cos(theta))
9 plot(x,y)
10 title("$x=3 (\\theta-sin(\\theta)) & y=3 (1-cos(\\theta))$")
11 axvline(color="red")
12 axhline(color="red")
13 grid()
14 show()

In [ ]: 1 # Plot cardioid r=5(1+cos theta )


2 from pylab import *
3 theta=linspace(0,2*pi,1000)
4 r1=5+5*cos( theta )
5 polar(theta,r1,'r')
6 title("$r=a(1+cos(\\theta))$")
7 show()

In [ ]: 1 #Program to pLot cardiod r=a(1+cos(theta)) take a =2


2 theta=linspace(0,2*pi,1000)
3 r=2*(1+cos(theta))
4 polar(theta,r)
5 title("cardiod: $r=a(1+cos(\\theta))$")
6 axvline(color="red")
7 show()
In [ ]: 1 #Program to pLot cardiod r=a(1+cos(theta)) &r=a(1-cos(theta))
2 theta=linspace(0,2*pi,1000)
3 r1=2*(1+cos(theta))
4 r2=2*(1-cos(theta))
5 polar(theta,r1,theta,r2)
6 title("cardiods: $r=a(1+cos(\\theta))$ & $r=a(1-cos(\\theta))$")
7 axvline(color="red")
8 show()

In [ ]: 1 #Program to plot four leaved rose r=2|cos(2*theta)|


2 from pylab import *
3 theta=linspace(0,2*pi,1000)
4 r=2*abs(cos(2*theta))
5 polar(theta,r)
6 title("$r=2|cos(2*\\theta))|$")
7 axvline(color="red")
8 show()

In [ ]: 1 #Program to plot circle r=5


2 from pylab import *
3 theta=linspace(0,2*pi,1000)
4 r=[5 for i in theta]
5 polar(theta,r,color="red")
6 title("$circle: r= constant $")
7 axvline(color="green")
8 show()

In [ ]: 1 #Program to plot implicit function, Cissiod: y^2(a-x)=x^3; a=2


2 from pylab import *
3 from sympy import *
4 x,y=symbols('x,y')
5 plot_implicit(Eq(y**2*(2-x),x**2*(2+x)),xlabel="",ylabel="",
6 title="Strophoid:$y^2(2-x)=x^2(2+x)$")
7 show()

In [ ]: 1 #Program to plot implicit function, Circle: x^2 + y^2 =a^2; a=2


2 from pylab import *
3 from sympy import *
4 x,y=symbols('x,y')
5 p1=plot_implicit(Eq(x**2 + y**2,4),xlabel=False,ylabel=False,
6 title='Circle : $x^2+y^2=4$ ',aspect_ratio=(2.,2.)) # a= 2
7 show()

In [ ]: 1 #Program to plot Cissiod - an implicit function


2 from sympy import *
3 from pylab import *
4
5 x,y=symbols('x y')
6 p2=plot_implicit(Eq ((y**2)*(3-x),x**3),xlabel=False,ylabel=False,
7 title='Cissiod : $y^2 (a-x)=x^3, a> 0$ ') # a= 2
8 show()

In [ ]: 1 #Program to plot Lemniscate - an implicit function


2 from sympy import *
3 from pylab import *
4
5 x,y=symbols('x y')
6 ​
7 p2=plot_implicit(Eq(4*(y**2),(x**2)*(4-x**2)),xlabel=False,ylabel=False,
8 title='Leminscate : $a^2 y^2 = x^2 (a^2-x^2), a> 0$ ') # a= 2
9 show()
In [ ]: 1 #Program to plot Folium of De-cartes - an implicit function
2 from sympy import *
3 from pylab import *
4
5 x,y=symbols('x y')
6 ​
7 p=plot_implicit(Eq(x ** 3+y ** 3,3*2*x*y),xlabel=False,ylabel=False,
8 title='Folium of De-cartes : $x^3 + y^3 = 3 a x y, a> 0$ ') # a =
9 show()

In [ ]: 1 #LAB 2

In [ ]: 1 #Program to find angle between two polar curves r=4(1+cos(t)) and


2 #r=5(1-cos(t))
3 ​
4 from sympy import *
5 t=Symbol('t')
6 r1=4*(1+cos(t))
7 r2=5*(1-cos(t))
8 dr1=diff(r1,t)
9 dr2=diff(r2,t)
10 ​
11 r1dr1=r1/dr1
12 r2dr2=r2/dr2
13 ​
14 θ=solve(r1-r2,t)
15 ​
16 print("The point of intersection is",θ)
17 ​
18 r1dr1=r1dr1.subs({t:θ[1]})
19 r2dr2=r2dr2.subs({t:θ[1]})
20 ​
21 p1=atan(r1dr1)
22 p2=atan(r2dr2)
23 p=abs(p1-p2)
24 print("Angle between the curves is %.2f"%p,"radians or %.2f"%deg(p),"degree")

In [ ]: 1 #Find the angle between the curves r = 4 cost and r = 5 sin t.


2 from sympy import *
3 t=Symbol('t')
4 r1=4*(cos(t))
5 r2=5*(sin(t))
6 dr1=diff(r1,t)
7 dr2=diff(r2,t)
8 ​
9 t1=r1/dr1
10 t2=r2/dr2
11 θ=solve(r1-r2,t)
12 print("The point of intersection is",θ)
13 w1=t1.subs({t:float(θ[0])})
14 w2=t2.subs({t:float(θ[0])})
15 y1=atan(w1)
16 y2=atan(w2)
17 w=abs(y1-y2)
18 print(f'Angle between curves is: %0.2f'%(w),"radians or %0.2f"%deg(w),"degree")
In [ ]: 1 #program to find the radius of curvature in polar form
2 #curve is r=4(1+cos(t)) at the point t=pi/2
3 ​
4 from sympy import *
5 t=Symbol("t")
6 ​
7 r=4*(1+cos(t))
8 r1=diff(r,t)
9 r2=diff(r1,t)
10 ​
11 ρ=(r**2+r1**2)**(3/2)/(r**2+2*r1**2-r*r2)
12 print("ρ=")
13 display(ρ)
14 ​
15 rho=rho.subs({t:pi/2})
16 print("radius of curvatre at t=pi/2 is %2.2f"%rho,"units")
17 print("curvatre at t=pi/2 is %2.2f"%(1/rho),"units")

In [ ]: 1 #program to find the radius of curvature in polar form


2 # curve is r=a sin(nt) at the point t=pi/2 and n=1
3 ​
4 from sympy import *
5 a,n,t=symbols("a,n,t")
6 ​
7 r=a*sin(n*t)
8 r1=diff(r,t)
9 r2=diff(r1,t)
10 ​
11 ρ=(r**2+r1**2)**(3/2)/(r**2+2*r1**2-r*r2)
12 print("ρ=")
13 display(ρ)
14 ​
15 rho=rho.subs({t:pi/2,n:1})
16 print("radius of curvatre at t=pi/2 is")
17 ​
18 display(rho)
19 print("curvatre at t=pi/2 is ")
20 display(1/rho)

In [ ]: 1 #Find radius of curvature in parametric form for x = acos(t), y = asin(t)


2 #with a=5 at t=pi/2
3 ​
4 from sympy import *
5 t,a=symbols("t,a") # define all symbols required
6 ​
7 y=a*sin(t) # input the parametric equation
8 x=a*cos(t)
9 y1=diff(y,t)
10 y2=diff(y1,t)
11 x1=diff(x,t)
12 x2=diff(x1,t)
13 ​
14 rho=(x1**2+y1**2)**(3/2)/(y1*x2-x1*y2)
15 print('Radius of curvature is ')
16 display(ratsimp(rho))
17 ​
18 rho=rho.subs({t:pi/2,a:5})
19 ​
20 print('\n\nRadius of curvature at a and t %.2f'%simplify(rho))
21 ​
22 curvature=1/rho
23 print (f'\n\n Curvature at ({a},{t}) is ',float(curvature))
In [ ]: 1 #LAB 3

In [ ]: 1 #program to find first and second order partial derivatives of


2 #u=exp(x) (x cosy - y siny)
3 ​
4 from sympy import *
5 x, y =symbols("x,y")
6 ​
7 u=exp(x)*(x*cos(y)-y*sin(y))
8 ux=simplify(diff(u,x))
9 print("ux=")
10 display(ux)
11 ​
12 uy=simplify(diff(u,y))
13 print("uy=")
14 display(uy)
15 ​
16 uxx=simplify(diff(ux,x)) #diff(u,x,x) or diff(u,x,2)
17 print("uxx=")
18 display(uxx)
19 ​
20 uyy=simplify(diff(uy,y)) #diff(u,y,y)
21 print("uyy=")
22 display(uyy)
23 ​
24 uxy=simplify(diff(ux,y)) #diff(u,x,y)
25 print("uxy=")
26 display(uxy)
27 ​
28 uyx=simplify(diff(uy,x)) #diff(u,y,x)
29 print("uyx=")
30 display(uyx)
31 print("From above derivatives we can notice that uxy=uyx")

In [ ]: 1 #program to show that Ux+Uy+Uz=3/(x+y+z), if U=log(x^3+y^3+z^3-3xyz)


2 ​
3 from sympy import *
4 x, y, z =symbols("x,y,z")
5 ​
6 U=log(x**3+y**3+z**3-3*x*y*z)
7 Ux=simplify(diff(U,x))
8 print("Ux=")
9 display(Ux)
10 ​
11 Uy=simplify(diff(U,y))
12 print("Uy=")
13 display(Uy)
14 ​
15 Uz=simplify(diff(U,z))
16 print("Uz=")
17 display(Uz)
18 ​
19 print("Ux+Uy+Uz=")
20 display(simplify(Ux+Uy+Uz))
In [ ]: 1 #program to show a^2 Uxx - Ut = 0, if U=A t^(-1/2) exp(-x^2/(4 a^2 t))
2 ​
3 from sympy import *
4 A, a, t, x = symbols("A,a,t,x")
5 ​
6 U=A*t**(-1/2)*exp(-x**2/(4*a**2*t))
7 Uxx=simplify(diff(U,x,x))
8 #print("Ux=")
9 display(Eq(Derivative(U,x,2),Uxx))
10 ​
11 Ut=simplify(diff(U,t))
12 print("Ut=")
13 display(Ut)
14 ​
15 print("a^2 Uxx-Ut=")
16 display(simplify(a**2*Uxx-Ut))

In [ ]: 1 #Program to find the jacobian,


2 #if U=x y / z, V = y z / x and W = z x / y
3 ​
4 from sympy import *
5 x, y, z =symbols("x,y,z")
6 ​
7 U=x*y/z
8 V=y*z/x
9 W=z*x/y
10 ​
11 Ux=simplify(diff(U,x))
12 Uy=simplify(diff(U,y))
13 Uz=simplify(diff(U,z))
14 ​
15 Vx=simplify(diff(V,x))
16 Vy=simplify(diff(V,y))
17 Vz=simplify(diff(V,z))
18 ​
19 Wx=simplify(diff(W,x))
20 Wy=simplify(diff(W,y))
21 Wz=simplify(diff(W,z))
22 ​
23 List=[[Ux,Uy,Uz],[Vx,Vy,Vz],[Wx,Wy,Wz]]
24 M=Matrix(List)
25 J=simplify(det(M))
26 ​
27 display(Eq(Determinant(M),J))
In [ ]: 1 #Program to find the jacobian,
2 #if u=ρ cos(ϕ) sin(θ) v=ρ cos(ϕ) cos(θ) w=ρ sin(ϕ)
3 from sympy import *
4 ρ, ϕ, θ =symbols("ρ,ϕ,θ")
5 ​
6 U=ρ*cos(ϕ)*sin(θ)
7 V=ρ*cos(ϕ)*cos(θ)
8 W=ρ*sin(ϕ)
9 ​
10 Uρ=simplify(diff(U,ρ))
11 Uϕ=simplify(diff(U,ϕ))
12 Uθ=simplify(diff(U,θ))
13 ​
14 Vρ=simplify(diff(V,ρ))
15 Vϕ=simplify(diff(V,ϕ))
16 Vθ=simplify(diff(V,θ))
17 ​
18 Wρ=simplify(diff(W,ρ))
19 Wϕ=simplify(diff(W,ϕ))
20 Wθ=simplify(diff(W,θ))
21 ​
22 List=[[Uρ,Uϕ,Uθ],[Vρ,Vϕ,Vθ],[Wρ,Wϕ,Wθ]]
23 M=Matrix(List)
24 J=simplify(det(M))
25 ​
26 display(Eq(Determinant(M),J))

In [ ]: 1 #LAB 4
In [ ]: 1 # Maxima and Minima of function with 2 Variables
2 #f(x, y) = x^4 + y^4 - 2x^2 − 2y^2 + 4xy.
3 from sympy import *
4 x,y=symbols('x,y')
5 ​
6 f=(x**4)+(y**4)-(2*x**2)+(4*x*y)-(2*y**2)
7 fx = diff(f,x)
8 fy = diff(f,y)
9 ​
10 points=solve([Eq(fx,0),Eq(fy,0)],[x,y])
11 ​
12 a1=[]
13 b1=[]
14 if type(points)!=list:
15 a1.append(points[x])
16 b1.append(points[y])
17 print(points)
18 print(a1,b1)
19 else:
20 for i in range(len(points)):
21 (a,b)=points[i]
22 if im(a)==0 and im(b)==0:
23 a1.append(a)
24 b1.append(b)
25 print(a1,b1)
26
27 fxx=diff(fx,x)
28 fxy=diff(fx,y)
29 fyy=diff(fy,y)
30 ​
31 for i in range(len(a1)):
32 A=fxx.subs({x:a1[i],y:b1[i]})
33 B=fxy.subs({x:a1[i],y:b1[i]})
34 C=fyy.subs({x:a1[i],y:b1[i]})
35 print('A=',A,'B=',B,'C=',C)
36
37 delta=A*C-B**2
38 print("AC-B^2=",delta)
39
40 if delta>0 and A>0:
41 print('(%0.2f,%0.2f)'%(a1[i],b1[i]),'is the minimum point')
42 elif delta>0 and A<0:
43 print('(%0.2f,%0.2f)'%(a1[i],b1[i]),'is the maximum point')
44 elif delta<0:
45 print('(%0.2f,%0.2f)'%(a1[i],b1[i]),'is the saddle point')
46 elif delta==0:
47 print("At (%0.2f,%0.2f)"%(a1[i],b1[i]),' Further investigation is required
In [ ]: 1 # Programs to find MacLaurin series of f(x)=exp(x)
2 from sympy import *
3 x=Symbol("x")
4 y=exp(x)
5 ys=y.subs({x:0})
6 n=int(input("Number of terms you want: "))
7 for i in range(1,n):
8 ys+=x**i*diff(y,x,i).subs({x:0})/factorial(i)
9 display("Maclaurin's series is", ys)
10 ​
11 ys=lambdify(x,ys)
12 ​
13 from pylab import *
14 x=arange(0,5,0.001)
15 plot(x,exp(x),color="red")
16 plot(x,ys(x),color="green")
17 axvline()
18 axhline()
19 show()

In [ ]: 1 #Programs to find MacLaurin series of f(x)=sin(x)


2 from sympy import *
3 x=Symbol("x")
4 y=sin(x)
5 ys=y.subs({x:0})
6 n=int(input("Number of terms you want: "))
7 for i in range(1,n):
8 ys+=x**i*diff(y,x,i).subs({x:0})/factorial(i)
9 display(ys)
10 ys=lambdify(x,ys)
11 ​
12 from pylab import *
13 x=arange(-pi,pi,0.001)
14 plot(x,sin(x),color="red")
15 plot(x,ys(x),color="green")
16 axvline()
17 axhline()
18 show()

In [ ]: 1 #Program to find limit of the function sin(x) / x as x tends to 0


2 ​
3 from sympy import *
4 ​
5 x=Symbol("x")
6 ​
7 f=sin(x)/x
8 ​
9 l=limit(f,x,0)
10 display(Eq(Limit(f,x,0),l))

In [ ]: 1 #Program to find limit of the function (1+1/x)^x as x tends to inf


2 ​
3 from sympy import *
4 from math import *
5 ​
6 x=Symbol("x")
7 ​
8 f=(1+1/x)**x
9 ​
10 l=limit(f,x,oo) # l=limit(f,x,-inf) also works
11 display(Eq(Limit(f,x,oo),l))
In [ ]: 1 #LAB 5

In [ ]: 1 #Program to solve dy/dx = x


2 ​
3 from sympy import *
4 x=Symbol("x")
5 ​
6 y=Function("y")(x)
7 ​
8 DE=Derivative(y,x)-x
9 display(Eq(DE,0))
10 ​
11 GS=dsolve(DE,y)
12 display(GS)

In [ ]: 1 #Program to solve dy/dx +y tan(x) - y^3 sec(x)=0


2 ​
3 from sympy import *
4 x=Symbol("x")
5 ​
6 y=Function("y")(x)
7 ​
8 DE=Derivative(y,x)+y*tan(x)-y**3*sec(x)
9 display(Eq(DE,0))
10 ​
11 GS=dsolve(DE,y)
12 for i in range(len(GS)):
13 display(GS[i])

In [ ]: 1 #Program to solve x^3 dy/dx - x^2 y + y^4 cos(x)=0


2 ​
3 from sympy import *
4 x=Symbol("x")
5 ​
6 y=Function("y")(x)
7 ​
8 DE=x**3*Derivative(y,x)-x**2*y+y**4*cos(x)
9 display(Eq(DE,0))
10 ​
11 GS=dsolve(DE,y)
12 for i in range(len(GS)):
13 display(GS[i])
In [ ]: 1 #Program to solve dP/dt = r P(0)=2 and take r = 3
2 ​
3 from sympy import *
4 r,t=symbols("r,t")
5 ​
6 P=Function("P")(t)
7 ​
8 C1=Symbol("C1")
9 ​
10 DE=Derivative(P,t)-3
11 display(Eq(DE,0))
12 ​
13 GS=dsolve(DE,P)
14 print("General solution is: ")
15 display(GS)
16 ​
17 e1=solve(GS.subs({P:2,t:0}),C1)
18 ​
19 PS=GS.subs({C1:e1[0]})
20 print("Particular solution is: ")
21 display(PS)
22 ​
23 P1=PS.rhs
24 P1=lambdify(t,P1)
25 ​
26 from pylab import *
27 t=arange(0,10,0.01)
28 plot(t,P1(t))
29 axvline()
30 axhline()
31 grid()
32 show()
In [ ]: 1 #Newton' Law of cooling
2 #Program to solve dθ/dt = -k*(θ-θm) with θ(0)=100 and θ(10)=80
3 #take θm = 25 (normally temperature of the surrounding)
4 ​
5 from sympy import *
6 t=Symbol("t")
7 k=Symbol("k",real=True)
8 θ=Function("θ")(t)
9 ​
10 C1=Symbol("C1")
11 ​
12 DE=Derivative(θ,t)+k*(θ-25)
13 display(Eq(DE,0))
14 ​
15 GS=dsolve(DE,θ)
16 print("General solution is: ")
17 display(GS)
18 ​
19 e1=solve(GS.subs({θ:100,t:0}),C1)
20 ​
21 PS=GS.subs({C1:e1[0]})
22 print("Particular solution is: ")
23 display(PS)
24 ​
25 e2=solve(PS.subs({θ:80,t:10}),k)
26 ​
27 PS=PS.subs({k:e2[0]})
28 print("Particular solution is: ")
29 display(PS)
30 ​
31 P1=PS.rhs
32 P1=lambdify(t,P1)
33 ​
34 from pylab import *
35 t=arange(0,100,0.01)
36 plot(t,P1(t))
37 axvline()
38 axhline()
39 grid()
40 show()
In [ ]: 1 #Growth of Virus affected files in a system or Bacteira in a culture test
2 ​
3 #Consider the following problem
4 ​
5 # If number of virus affected files in a system is proportional to the number
6 #of virus affected files P(t) at time t. Determine the number of virus affected
7 #files at time t, if the number virus affected files in the system is P0
8 #to begin with and after t=1 hour the number virus affected files found
9 #to be (3/2)P0. Assume P0=20
10 # OR
11 # If the rate of growth of bacteria is proportional to the number of bacteria P(t)
12 #present at time t.Determine the number of bacteria at time t, if culture
13 #initially has P0 number of bacteria and afetr t=1 hour
14 #the number of bacteria measured to be (3/2)P0. Assume P0=20
15 ​
16 #Following is the Mathematical model for the above situation
17 ​
18 # Program to Solve : dP(t)/dt = k P given P(0)=P0 and P(1)=(3/2)P0; take P0=20
19 ​
20 from sympy import *
21 ​
22 t,k=symbols("t,k") #define symbols in equation
23 ​
24 P=Function("P")(t) # Say P is function of t
25 ​
26 C1=Symbol("C1") # define symbol C1, arbitrary constant of solution
27 ​
28 DE=Derivative(P,t)-k*P #take everything to LHS define LHS of differential equatio
29 print("Differential Equation is: ")
30 display(Eq(DE,0))
31 ​
32 GS=dsolve(Eq(DE,0),P) #dsolve is used to solve and Eq(LHS) is same as Eq(LHS,0)
33 print("General Solution is: ")
34 display(GS)
35 ​
36 e=solve(GS.subs({P:(3/2)*C1,t:1}),k) # find C1 at t=0 and P=2
37 ​
38 PS=GS.subs({k:e[0]}) #using C1 value write the particular solution
39 print("Particular Solution is: ")
40 display(PS)
41 ​
42 eq=PS.rhs.subs(C1,20)
43 y1=lambdify(t,eq)
44 ​
45 from pylab import *
46 t=linspace(0,10,100)
47 plot(t,y1(t))
48 axvline()
49 axhline()
50 grid()
51 show()

In [ ]: 1 #LAB 6
In [ ]: 1 # Program to Check whether the following system of homogenous linear equation
2 # has non-trivial solution.
3 # x1 + 2x2 − x3 = 0, 2x1 + x2 + 4x3 = 0, 3x1 + 3x2 + 4x3 = 0.
4 from sympy import *
5 ​
6 A=Matrix([[1,2,-1],[2,1,4],[3,3,4]])
7 ​
8 ρA=A.rank()
9 n=shape(A)
10 print("The coefficient Matrix A is: ")
11 display(A)
12 print(f"Rank of the matrix is {ρA}")
13 ​
14 if ρA==n[1]:
15 print("System has trivial solution")
16 else:
17 print("System has non trivial solution")

In [ ]: 1 # Program to solve non homogeneous linear system if it is consistent


2 # x1 + 2 x2 − x3 = 1, 2 x1 + x2 + 4 x3 = 2, 3 x1 + 3 x2 + 4 x3 = 1.
3 ​
4 from sympy import *
5 ​
6 x,y,z=symbols("x,y,z")
7 ​
8 A=Matrix([[1,2,-1],[2,1,4],[3,3,4]])
9 B=Matrix([[1],[2],[1]])
10 n=shape(A)
11 AB=A.col_insert(n[1],B)
12 ​
13 ρA=A.rank()
14 ρAB=AB.rank()
15 ​
16 print("The coefficient Matrix A is: ")
17 display(A)
18 print("The augmented Matrix AB is: ")
19 display(AB)
20 print(f"ρ(A)={ρA} and ρ(AB)={ρAB}")
21 ​
22 if ρA==ρAB:
23 if ρA==n[1]:
24 print("System has unique solution")
25 else:
26 print("The system has infinitely many solutions")
27 print(solve_linear_system(AB,x,y,z))
28 else:
29 print("The system of equations is inconsistent")

In [ ]: 1 #LAB 7
In [ ]: 1 #Solve the system of equations using Gauss-Seidel method:
2 #20 x + y − 2 z = 17; 3 x + 20 y − z = −18; 2 x − 3 y + 20 z = 25.
3 ​
4 f1=lambda x,y,z: (17-y+2*z)/20
5 f2=lambda x,y,z: (-18-3*x+z)/20
6 f3=lambda x,y,z: (25-2*x+3*y)/20
7 ​
8 x0,y0,z0=0,0,0
9 ​
10 count=1
11 e=0.0001 #the tolerable error
12 ​
13 print("\nCount\t x\t\t y\t\t z\n")
14 ​
15 condition=True
16 while condition:
17 x1=f1(x0,y0,z0)
18 y1=f2(x1,y0,z0)
19 z1=f3(x1,y1,z0)
20 print("%d\t%.5f \t%.5f\t%.5f\n"%(count,x1,y1,z1))
21 count=count+1
22 e1=abs(x0-x1)
23 e2=abs(y0-y1)
24 e3=abs(z0-z1)
25 x0,y0,z0=x1,y1,z1
26 condition = e1>=e and e2>=e and e3>=e
27 print("\n Solution: x=%0.5f, y=%0.5f and z=%0.5f"%(x1,y1,z1))

In [ ]: 1 #LAB 8

In [ ]: 1 #Obtain the eigenvalues and eigenvectors for the given matrix.


2 #A = [[4,3,2],[1,4,1],[3,10,4]]
3 ​
4 from numpy import *
5 A=[[4,3,2],[1,4,1],[3,10,4]]
6 mat=Matrix(A)
7 ​
8 print("The given matrix A is")
9 display(mat)
10 ​
11 eig_val,eig_vec=linalg.eig(array(A))
12 print("Eigen values: \n",eig_val)
13 print("Eigen values: \n",eig_vec)
In [ ]: 1 #Compute the numerically largest eigenvalue of following P by power method
2 # A = [[25,1,2],[1,3,0],[2,0,-4]] take transpose of [1 0 0] as initial
3 #approximation to the eigen vector
4 ​
5 from numpy import *
6 ​
7 A=array([[25,1,2],[1,3,0],[2,0,-4]])
8 x=array([1,0,0])
9 λ_i=0
10 ​
11 while(True):
12 Y=dot(A,x)
13 λ=abs(Y).max()
14 x=Y/λ
15 if abs(λ-λ_i)<0.001:
16 break
17 else:
18 λ_i=λ
19 print("\nThe largest Eigenvalue is: %.4f"%λ_i)
20 print("\nCorresponding EigenVector is: ",x)
21 print("\nThe solution is:\n")
22 print("The largest Eigenvalue is: %.4f"%λ)
23 print("Corresponding EigenVector is: ",x)

In [ ]: 1 #LAB 9

In [ ]: 1 #Program to find GCD of two integers using Euclid's Algorithm


2 ​
3 def gcd(a,b):
4 rem=1
5 if a<b:
6 temp=a
7 a=b
8 b=temp
9 print("Given integers are:",a,b)
10 print("Divisor\tremainder")
11 while rem>0:
12 rem=a%b #remainder when a divides b
13 print(b,"\t",rem)
14 a=b
15 b=rem
16 return a
17 ​
18 a=int(input("Enter first positive integer: "))
19 b=int(input("Enter second positive integer: "))
20 print(f"GCD{a,b}=",gcd(a,b))
In [ ]: 1 #Program to determine whether given two integers are relatively prime
2 # If GCD(a,b)=1, then integers a and b are called relatively prime numbers
3 def gcd(a,b):
4 rem=1
5 if a<b:
6 temp=a
7 a=b
8 b=temp
9 print("Given integers are:",a,b)
10 print("Divisor\tremainder")
11 while rem>0:
12 rem=a%b #remainder when a divides b
13 print(b,"\t",rem)
14 a=b
15 b=rem
16 return a
17 ​
18 a=int(input("Enter first positive integer: "))
19 b=int(input("Enter second positive integer: "))
20 GCD=gcd(a,b)
21 print(f"GCD{a,b}=",GCD)
22 if GCD==1:
23 print(f"{a} and {b} are relatively prime")
24 else:
25 print(f"{a} and {b} are not relatively prime")

In [ ]: 1 #Program to determine whether a divides b or b divides a


2 ​
3 def gcd(a,b):
4 rem=1
5 if a<b:
6 temp=a
7 a=b
8 b=temp
9 print("Given integers are:",a,b)
10 print("Divisor\tremainder")
11 while rem>0:
12 rem=a%b #remainder when a divides b
13 print(b,"\t",rem)
14 a=b
15 b=rem
16 return a
17 ​
18 a=int(input("Enter first positive integer: "))
19 b=int(input("Enter second positive integer: "))
20 GCD=gcd(a,b)
21 print(f"GCD{a,b}=",GCD)
22 ​
23 if a>b and GCD==b:
24 print(f"{b} divides {a}")
25 elif b>a and GCD==a:
26 print(f"{a} divides {b}")
27 elif a==b:
28 print(f"{a} and {b} divides each other")
29 else:
30 print(f"{a} doesnot divides {b} and {b} doesnot divides {a}")
In [ ]: 1 #Calculate GCD(a,b) and express it as linear combination of a and b
2 #using Euclid's algorithm
3 #
4 a=int(input("Enter first integer"))
5 b=int(input("Enter second integer"))
6 if a<b: # interchange if b is bigger
7 temp=a
8 a=b
9 b=temp
10 r1=a
11 r2=b
12 r3=r1%r2
13 q=(r1-r3)/r2
14 x1=1
15 x2=0
16 x3=x1-x2*q
17 y1=0
18 y2=1
19 y3=y1-y2*q
20 ​
21 while r3!=0:
22 r1=r2
23 r2=r3
24 r3=r1%r2
25 q=(r1-r3)/r2
26 x1=x2
27 x2=x3
28 x3=x1-x2*q
29 y1=y2
30 y2=y3
31 y3=y1-y2*q
32 ​
33 print(f"GCD{a,b} =",r2)
34 print("%d x %d + %d x %d = %d"%(a,x2,b,y2,r2))

In [ ]: 1 #LAB 10
In [ ]: 1 #Program to solve the linear congruence 4 x congruent 2 (mod 6);
2 #use module gcd() of sympy
3 ​
4 from sympy import *
5 a=4
6 b=2
7 n=6
8 g=gcd(a,n)
9 print(f"GCD{a,n} =",g)
10 if b%g != 0:
11 print("The congruence has no integer solution")
12 else:
13 if g==1:
14 print("The congruence has unique solution")
15 for i in range(1,n-1):
16 x=(n*i+b)/a
17 if x//1==x:
18 x0=x//1
19 print("Solution of the given congruence is:",x0)
20 break
21 else:
22 print(f"The congruence has {g} solutions")
23 for i in range(1,n-1):
24 x=(n*i+b)/a
25 if x//1==x:
26 x0=x//1
27 break
28 z=[]
29 for i in range(g):
30 x=(x0+i*(n//g))%n
31 z.append(int(x))
32 print("Solutions of the given congruence are:",z)

In [ ]: 1 #program to find multiplicative inverse of 4(mod 32)


2 from sympy import *
3 a=4
4 b=1
5 n=32
6 g=gcd(a,n)
7 print(f"GCD{a,n} =",g)
8 if g != 1:
9 print(f"Multiplicative inverse of {a} (mod {n}) doesnot exist")
10 else:
11 for i in range(1,n-1):
12 x=(n/a)*i+(b/a)
13 if x//1==x:
14 x0=x//1
15 print(f"Multiplicative inverse of {a} (mod {n}) is: ",x0)
16 break

In [ ]: 1 ​

You might also like