Lab 9
Lab 9
method
Objectives:
Use python
import numpy as np
import sympy
zeros=np.zeros
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
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],X[1],Y[1],X[2],Y[2],X[3],Y[3]))
output: The required values are:at x=0.00, y=0.00000, x=0.10, y=0.3485,
x=0.20, y=0.81079 ,x =0.30 , y=1.41590
2.Solve y|+4y=x2 with initial conditions y(0) = 1 using Taylor series method at x =0.1, 0.2.
import numpy as np
import sympy
zeros=np.zeros
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]=[x ** 2-4*y[0]]
D[1]=[2*x-4*x ** 2+16*y[0]]
D[2]=[2-8*x+16*x ** 2-64*y[0]]
D[3]=[-8+32*x-64*x ** 2+256*y[0]]
return D
x=0.0
xStop=0.2
y=array([1.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],Y[0],X[1],Y[1],X[2],Y[2]))
output:
import numpy as np
h=0.2
y0=-1
n=3
y[0]=y0
x[0]=0
for i in range(0,n):
x[i+1]=x[i]+h
y[i+1]=y[i]+h*f(x[i], y[i])
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[1],y[1],x[2],y[2],x[3],y[3]))
print("\n\n")
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()
plt.legend(loc='best')
plt.show()
output: 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
2.Solve: y′=−2y+x3e−2x with y(0)=1 using Euler’s method at x =0.1,0.2.
import numpy as np
f=lambda x , y:-2*y+(x**3)*np.exp(-2*x)
h=0.1
y0=1
y[0] = y0
x[0]=0
x[i+1]=x[i]+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\n\n"%(
x[0],y[0],x[1],y[1],x[2],y[2]))
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()
plt.legend(loc ='best')
plt.show()
output:
Modified Euler’s method
ℎ
The iterative formula is:y1(n+1) = y0 + [f(x0, y0) + f(x1, y1(n))], n = 0, 1, 2, 3, . . . ,where, y1(n) is the nth
2
approximation to y1.
The first iteration will use Euler’s method: y1(0) = y0 + hf(x0, y0).
1.Solve y′ = −ky with y(0) = 100 using modified Euler’s method at x = 100, by taking h = 25.
import numpy as np
x=np.zeros(n+1)
y=np.zeros(n+1)
x[0]=x0
y[0]=y0
for i in range( n ):
x[i+1]=x[i] + h
k2=h*f(x[i+1], y[i]+k1)
y[i+1]=y[i]+0.5*(k1 + k2)
return x , y
def f (x , y ):
return -0.01*y
x0=0.0
y0=100.0
h=25
n=4
x,y=modified_euler(f , x0 , y0 , h , n)
print("\n\n")
plt.plot(x , y , 'bo-')
plt.xlabel('x')
plt.ylabel('y')
plt.grid(True)
plt.show()
output:
The required value at x= 100.00, y=37.25290