Maths 20 Lab
Maths 20 Lab
Maths 20 Lab
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10, 10, 0.001)
y1 = np.sin(x)
y2=np.cos(x)
plt.plot(x,y1,x,y2)
plt.title("sine curve and cosine curve")
plt.xlabel("Values of x")
plt.ylabel("Values of sin(x) and cos(x)")
plt.grid()
plt.show()
r,t =symbols('r,t')
r1=4*(1+cos(t));
r2=5*(1-cos(t));
dr1=diff(r1,t)
dr2=diff(r2,t)
t1=r1/dr1
t2=r2/dr2
q=solve(r1-r2,t)
w1=t1.subs({t:float(q[1])})
w2=t2.subs({t:float(q[1])})
y1=atan(w1)
y2=atan(w2)
w=abs(y1-y2)
print('Angle between curves in radians is %0.3f'%(w))
3. Find radius of curvature r=4(1+cost) at t==pi/2
u=exp(x)*(x*cos(y)-y*sin(y))
dux=diff(u,x)
duy=diff(u,y)
duxy=diff(dux,y)
duyx=diff(duy,x)
duyx=diff(u,y,x)
if duxy==duyx:
print('Mixed partial derivatives are equal')
else:
print('Mixed partial derivatives are not equal')
5. If u=x+3y^2-z^3, v=4x^2yz, w=2z^2-xy
Then prove that at (1,-1,0) , J= 20
dvx=diff(v,x)
dvy=diff(v,y)
dvz=diff(v,z)
dwx=diff(w,x)
dwy=diff(w,y)
dwz=diff(w,z)
Jac=Determinant(J).doit()
print('\n\n J = \n')
display(Jac)
print("\n\n J at (1,-1,0):\n")
Jac1=Determinant(J1).doit()
display(Jac1)
6. Find maxima and minima of f(x,y) = x^2 +y^2+3x-3y+4
import sympy
from sympy import Symbol, solve, Derivative, pprint
x=Symbol( 'x')
y=Symbol('y')
f=x**2+y**2+3*x-3*y+4
d1=Derivative(f ,x). doit()
d2=Derivative(f ,y). doit()
criticalpoints1=solve(d1)
criticalpoints2=solve(d2)
s1=Derivative(f,x,2).doit()
s2=Derivative(f,y,2).doit()
s3=Derivative(Derivative(f,y),x).doit()
print('function value is ')
delta=s1*s2-s3**2
import numpy as np
from matplotlib import pyplot as plt
from sympy import *
x=Symbol( 'x')
y=sin(1*x)
format
x0=float(pi/2)
dy=diff(y,x)
d2y=diff(y,x,2)
d3y=diff(y,x,3)
yat=lambdify(x,y)
dyat=lambdify(x,dy)
d2yat=lambdify(x,d2y)
d3yat=lambdify(x,d3y)
y=yat(x0)+((x-x0))*dyat(x0)+((x-x0)**2/2)*d2yat(x0)+((x-x0)**3/6)*d3yat(x0)
print(simplify(y))
yat=lambdify(x,y)
print("%.3f" % yat(pi/2+10*(pi/180)))
def f(x):
return np.sin(1*x)
x = np.linspace(-10, 10)
import numpy as np
from matplotlib import pyplot as plt
from sympy import *
x=Symbol('x')
y=sin(x)+cos(x)
format
x0=float(0)
dy=diff(y,x)
d2y=diff(y,x,2)
d3y=diff(y,x,3)
yat=lambdify(x,y)
dyat=lambdify(x,dy)
d2yat=lambdify(x,d2y)
d3yat=lambdify(x,d3y)
y=yat(x0)+((x-x0)/2)*dyat(x0)+((x-x0)**2/6)*d2yat(x0)+((x-x0)**3/24)*d3yat(x0)
print(simplify(y))
yat=lambdify(x,y)
print("%.3f" % yat(10*(pi/180)))
def f(x) :
return np.sin(1*x)+np.cos(x)
x = np.linspace(-10, 10)
print("\nGeneral Solution")
GS1=dsolve(DE1)
display(GS1)
print("\nParticular Solution")
PS1=GS1.subs({C1:2})
display(PS1)
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def model(y,t):
k=0.3
return -k*y
y0=5
t=np.linspace(0,20)
y= odeint(model,y0,t)
plt.plot(t,y)
plt.title('Solution of dy/dt=-ky; k=0.3, y(0)=5')
plt.xlabel('time')
plt.ylabel('y(t)')
plt. show()
11. Check whether system of homo
x1+2x2-x3=0, 2x1+x2+4x3=0, x1-x2+5x3=0
import numpy as np
A=np.matrix ([[1,2,-1], [2,1,4], [1,-1,5]])
B=np.matrix ([[0],[0],[0]])
r=np.linalg.matrix_rank(A)
n=A.shape [1]
if (r==n):
print("System has trivial solution")
else:
print("System has", n-r, "non-trivial solution(s)")
12. Check whether system of homo linear eqn has trivial soln
X1+2x2-x3=1 , 2x1+x2+4x3=2, 3x1+3x2+4x3=1
A=np.matrix([[1,2,-1],[2,1,4],[3,3,4]])
B=np.matrix( [[1],[2],[1]])
AB=np.concatenate((A,B), axis=1)
rA=np.linalg.matrix_rank(A)
rAB=np.linalg.matrix_rank(AB)
n=A.shape [1]
if (rA==rAB):
if (rA==n):
print("The system has unique solution")
print(np.linalg.solve(A,B))
else:
print("The system has infinitely many solutions")
else:
print("The system of equations is inconsistent")
13. Solve using gauss seidel method
20x+y-2z=17, 3x+20y-z=-18, 2x-3y+20z=25
return x
a = array([[20.0, 1.0, -2.0], [3.0, 20.0, -1.0], [2.0, -3.0, 20.0]])
x = array([0.0, 0.0, 0.0])
b = array([17.0, -18.0, 25.0])
for i in range(0, 25):
x = seidel(a, x, b)
print(x)
14. Solve by gauss seidel iteration method
x+2y-z=3, 3xy+2z=1, 2x-2y+6z=2
x0, y0, z0 = 0, 0, 0
e1 = abs(x0 - x1)
e2 = abs(y0 - y1)
e3 = abs(z0 - z1)
x0 = x1
y0 = y1
z0 = z1
for i in range(10):
x = np. dot (a, x)
lambda_1, x = normalize(x)
1 1 3
16. Compute numerically largest eigen value of p=[1 5 1] by power method
3 1 1
import numpy as np
def normalize(x):
fac= abs(x) .max()
x_n = x / x.max()
return fac, x_n
x = np.array([1, 1,1])
a= np.array([[1,1,3],[1,5,1], [3,1,1]])
for i in range(10):
x = np. dot (a, x)
lambda_1, x = normalize(x)
def gcd1(a,b):
c=1
if b < a:
t=b
b=a
a=t
while (c > 0):
c = b%a
print(a,c)
b=a
a=c
continue
print('GCD =',b)
gcd1( 614,124)
18. Prove that 163 and 512 are relatively prime
def gcd1(a,b):
c=1;
if b <a:
t=b;
b=a;
a=t;
while ( c>0):
c=b%a;
print(a,c);
b=a;
a=c;
continue
print('GCD= ',b);
gcd1(163,512)
19. Show that the linear congruence 6x≡5 (mod 15) has no soln