Maths 20 Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

MATHS LAB 20 QUES

1. Plot sine and cosine curves.

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()

2. Find angle between r=4(1+cost) and r=5(1-cost)

from sympy import *

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

from sympy import *


t=Symbol( 't ')
r=4*(1+cos(t))
r1=Derivative(r,t).doit()
r2=Derivative(r1,t).doit()
rho=(r**2+r1**2)**(1.5)/(r**2+2*r1**2-r*r2);
rho1=rho.subs(t,pi/2)
print('The radius of curvature is %3.4f units'%rho1)

4. Prove that mixed partial derivatives Uxy = Uyx for u=exp(x)(xcos(y)-ysin(y))

from sympy import *


x,y =symbols('x y')

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

from sympy import *


x,y,z=symbols('x,y,z')
u=x+3*y**2-z**3
v=4*x**2*y*z
w=2*z**2-x*y
dux=diff(u,x)
duy=diff(u,y)
duz=diff(u,z)

dvx=diff(v,x)
dvy=diff(v,y)
dvz=diff(v,z)

dwx=diff(w,x)
dwy=diff(w,y)
dwz=diff(w,z)

J=Matrix ([[dux,duy,duz] ,[dvx,dvy,dvz] ,[dwx,dwy,dwz]]);

print("The Jacobian matrix is ")


display (J)

Jac=Determinant(J).doit()
print('\n\n J = \n')
display(Jac)

J1= J.subs([(x,1), (y,-1), (z,0)])

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

if(delta>0 and s1<0):


print(" f takes maximum ")
elif (delta>0and s1>0):
print(" f takes minimum")
if (delta<0):
print("The point is a saddle point")
if (delta==0):
print("further tests required")
7. Expand sin(x) as taylor series about x= pi/2 upto 3rd degree term. Find sin(100°)

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)

plt.plot(x, yat(x), color='red')


plt.plot(x, f(x), color='green')
plt.ylim([-3, 3])
plt.grid()
plt.show()+
8. Find maclaurin series expansion of sin(X) +cos(x) upto 3rd degree term ,
calculate sin(10) +cos(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)

plt.plot(x, yat(x), color='red')


plt.plot(x, f(x), color='green')
plt.ylim([-3, 3])
plt.grid()
plt.show()
9. Solve dP(t)/dt = r

from sympy import *


init_printing ()
t,r = symbols('t,r')
P = Function('P')(t)
C1= Symbol('C1')
print("\nDifferential Equation")
DE1=Derivative(P, t, 1)-r
display(DE1)

print("\nGeneral Solution")

GS1=dsolve(DE1)
display(GS1)

print("\nParticular Solution")
PS1=GS1.subs({C1:2})
display(PS1)

10. Solve dy/dt=-ky with parajmeter k =0.3 and y(0) =5

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

from numpy import *


def seidel(a, x, b):
n = len(a)
for j in range(0, n):
d = b[j]
for i in range(0, n):
if (j != i):
d -= a[j][i] * x[i]
x[j] = d / a[j][j]

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

f1 = lambda x,y,z: (1+y-2*z)/3


f2 = lambda x,y,z: (3-x+z)/2
f3 = lambda x,y,z: (2-2*x+2*y)/6

x0, y0, z0 = 0, 0, 0

e = float(input('Enter tolerable error: '))


print('\t Iteration \t x \t y \t z\n')
for i in range(0, 25):
x1 = f1(x0, y0, z0)
y1 = f2(x1, y0, z0)
z1 = f3(x1, y1, z0)

print('%d\t%0.4f\t%0.4f\t%0.4f\n' % (i, x1, y1, z1))

e1 = abs(x0 - x1)
e2 = abs(y0 - y1)
e3 = abs(z0 - z1)

x0 = x1
y0 = y1
z0 = z1

if e1 > e and e2 > e and e3 > e:


continue
else:
break
print ('\nSolution : x=%0.3f, y=%0.3f and z= %0.3f\n'% (x1,y1,z1))

output: Enter tolerable error: 0


Iteration x y z

0 0.3333 1.3333 0.6667

1 0.3333 1.6667 0.7778

2 0.3704 1.7037 0.7778

3 0.3827 1.6975 0.7716

4 0.3848 1.6934 0.7695

5 0.3848 1.6924 0.7692

Solution : x=0.385, y=1.692 and z= 0.769


6 −2 2
15. Compute numerically largest eigen value of p=[−2 3 −1] by power method
2 −1 3
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([[6,-2,2],[-2,3,-1], [2,-1,3]])

for i in range(10):
x = np. dot (a, x)
lambda_1, x = normalize(x)

print ('Eigenvalue: ',lambda_1)


print ('Eigenvector: ',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)

print ('Eigenvalue: ',lambda_1)


print ('Eigenvector: ',x)

17. Find GCD of (614,124)

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

from sympy import gcd


a=int( input('enter integer a'));
b=int( input('enter integer b'));
m=int( input('enter integer m'));
d=gcd(a,m)
if (b%d!=0):
print('the congruence has no integer solution');
else:
for i in range(1,m-1):
x=(m/a)*i+(b/a)
if(x//1==x):
print('the solution of the congruence is ',x)
break
output : enter integer a 6
enter integer b 5
enter integer m 15
the congruence has no integer solution
20. Find the inverse of 5 mod 13

from sympy import gcd


a=int( input('enter integer a'));
b=int( input('enter integer b'));
m=int( input('enter integer m'));
d=gcd(a,m)
if (b%d!=0):
print('the congruence has no integer solution');
else:
for i in range(1,m-1):
x=(m/a)*i+(b/a)
if(x//1==x):
print('the solution of the congruence is ',x)
break
output : enter integer a 5
enter integer b 1
enter integer m 13
the solution of the congruence is 8.0

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy