m2 Math Lab
m2 Math Lab
Differential Equation :
General solution :
In [12]: """
Lab 5. Computing the inner product and orthogonality
1.Find the inner product of the vectors (2, 1, 5, 4) and (3, 4, 7, 8)
"""
import numpy as np
A=np.array([2,1,5,4])
B=np.array([3,4,7,8])
output=np.dot(A,B)
print(output)
77
In [13]: """
2. Verify whether the following vectors (2, 1, 5, 4) and (3, 4, 7, 8) are orthogonal.
"""
import numpy as np
A=np.array([2,1,5,4])
B=np.array([3,4,7,8])
output=np.dot(A,B)
print('Inner product is:',output)
if output==0:
print("given vector are orthogonal")
else:
print("given vectors are not orthogonal")
3 2
1.0⋅t - 3.0⋅t + 1.0⋅t + 7.0
Do you want to interpolate at a point (y/n)? y
enter the point 2
t=symbols('t')
f=[]
p=(t-x[n-1])/(x[1]-x[0])
f.append(p)
for i in range(1,n-1):
f.append(f[i-1]*(p+i)/(i+1))
poly=y[n-1][0]
print(poly)
for i in range(n-1):
poly=poly+y[n-1][i+1]*f[i]
simp_poly=simplify(poly)
print('\n THE INTERPOLATING POLYNOMIAL IS \n');
pprint(simp_poly)
inter=input('Do you want to interpolate at a point (y/n)? ')
if inter=='y':
a= float(input('enter the point '))
interpol=lambdify(t, simp_poly)
result=interpol (a)
print('\nThe value of the function at',a, 'is\n', result);
This will use Newton's backward interpolation formula
Enter number of data points: 5
Enter data for x and y:
x[0]=1
y[0]=6
x[1]=3
y[1]=10
x[2]=5
y[2]=62
x[3]=7
y[3]=210
x[4]=9
y[4]=502
1.00 6.00
3.00 10.00 4.00
5.00 62.00 52.00 48.00
7.00 210.00 148.00 96.00 48.00
9.00 502.00 292.00 144.00 48.00 0.00
502.0
3 2
1.0⋅t - 3.0⋅t + 1.0⋅t + 7.0
Do you want to interpolate at a point (y/n)? y
enter the point 8
1.27631
In [6]: """
Lab 9. Solution of ODE of first order and first degree by Taylor’s series and Modified Euler’s metho
1. Solve: dy/ dx − 2y = 3ex with y(0) = 0 using Taylor series method at x = 0.1(0.1)0.3.
"""
from numpy import array, zeros, exp
def taylor(deriv,x,y,xStop,h):
X=[]
Y=[]
X.append(x)
Y.append(y)
while x<xStop:
D=deriv(x,y)
H=1.0
for j in range(3):
H=H*h/(j+1)
y=y+D[j]*H
x=x+h
X.append(x)
Y.append(y)
return array(X),array(Y)
def deriv(x,y):
D=zeros((4,1))
D[0]=[2*y[0]+3*exp(x)]
D[1]=[4*y[0]+9*exp(x)]
D[2]=[8*y[0]+21*exp(x)]
D[3]=[16*y[0]+45*exp(x)]
return D
x=0.0
xStop=0.3
y=array([[0.0]])
h=0.1
X,Y=taylor(deriv,x,y,xStop,h)
print("The required values are :at x=%0.2f, y=%0.5f, x=%0.2f, y=%0.5f, x=%0.2f, y=%0.5f, x=%0.2f, y=%0.5f"%(X[0],Y[0],
The required values are :at x=0.00, y=0.00000, x=0.10, y=0.34850, x=0.20, y=0.81079, x=0.30, y=1.41590
print("The required values are at x=%0.2f, y=%0.5f, x=%0.2f, y=%0.5f, x=%0.2f, y=%0.5f, x=%0.2f, y=%0.5f"%(x[0],y[0],x
print("\n\n")
plt.plot(x,y,'bo--',label='Approximate')
plt.plot(x,-np.exp(-x),'g*-',label='Exact')
plt.title("Approximate and Exact Solution")
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()
plt.legend(loc='best')
plt.show()
The required values are at x=0.00, y=-1.00000, x=0.20, y=-0.80000, x=0.40, y=-0.63625, x=0.60, y=-0.50219
In [16]: """
Lab 10. Solution of ODE of first order and first degree by Runge-Kutta 4th order method and Milne’s predictor and corr
1. Apply the Runge Kutta method to find the solution of dy/dx = 1 + (y/x) at y(2) taking h = 0.2. Given that y(1) = 2.
"""
from sympy import *
import numpy as np
def Rungekutta(g,x0,h,y0,xn):
x,y=symbols('x,y')
f=lambdify([x,y],g)
xt=x0+h
Y=[y0]
while xt<=xn:
k1=h*f(x0,y0)
k2=h*f(x0+h/2,y0+k1/2)
k3=h*f(x0+h/2,y0+k2/2)
k4=h*f(x0+h, y0+k3)
y1=y0+(1/6)*(k1+2*k2+2*k3+k4)
Y.append(y1)
# print('y(%3.3f'%xt,') is %3.3f'%y1)
x0=xt
y0=y1
xt=xt+h
return np.round(Y,2)
Rungekutta('1+(y/x)',1,0.2,2,2)