0% found this document useful (0 votes)
138 views8 pages

Lab 9

Lab 9 programs

Uploaded by

akshayandani05
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)
138 views8 pages

Lab 9

Lab 9 programs

Uploaded by

akshayandani05
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/ 8

LAB 9: Solution of ODE of first order and first degree by Taylor’s series and Modified Euler’s

method

Objectives:

Use python

1. to solve ODE by Taylor series method.

2. to solve ODE by Modified Euler method.

3. to trace the solution curves.

Taylor series method to solve ODE


𝒅𝒚
1.Solve: -2y=3𝒆𝒙 with y(0) = 0 using Taylor series method at x = 0.1,0.2,0.3.
𝒅𝒙

import numpy as np

import sympy

from sympy import exp

zeros=np.zeros

from numpy import array

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],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

from sympy import exp

zeros=np.zeros

from numpy import array

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:

The required values are:at x=0.00 , y=1.00000 , x=0.10 , y=0.66967 , x=0.20


, y=0.45026
Euler’s method to solve ODE:
𝑑𝑦
1.To solve the ODE of the form = f(x, y) with initial conditions y(x0) = y0. The iterative formula is given
𝑑𝑥
by : y(x(i+1) = y(xi) + hf(xi, y(xi)).Solve: y′ = e−x with y(0) = −1 using Euler’s method at x = 0.2(0.2)0.6.

import numpy as np

import matplotlib.pyplot as plt

f=lambda x,y: np.exp(-x)

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

import matplotlib.pyplot as plt

f=lambda x , y:-2*y+(x**3)*np.exp(-2*x)

h=0.1

y0=1

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\n\n"%(
x[0],y[0],x[1],y[1],x[2],y[2]))

plt.plot(x , y , 'bo--', label =" Approximate ( Euler 's method )")

plt.title(" Solution by Euler 's method ")

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

import matplotlib.pyplot as plt

def modified_euler(f , x0 , y0 , h , n):

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

k1=h*f( x[i], y[i])

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("The required value at x= %0.2f , y=%0.5f"%( x[4],y[4]))

print("\n\n")

plt.plot(x , y , 'bo-')

plt.xlabel('x')

plt.ylabel('y')

plt.title('Solution of dy/dx = -ky using Modified Euler \'s Method ')

plt.grid(True)

plt.show()

output:
The required value at x= 100.00, y=37.25290

You might also like