You are on page 1of 6

Python Programs using Eulers method

Program 1

Find the value of y(0.4) by solving the given


differential equation using Eulers Method:
dy/dx = x+y

Given initial condition y(0)=0.


def f(x,y):
return x+y
x0=float(input("Enter x0 = "))
y0=float(input("Enter y0 = "))
h=float(input("Enter h = "))
n=int(input("Enter n = "))
for i in range(n):
y1=y0+h*f(x0,y0) #you can directly put y0 instead of y1 and remove
the further command of y0=y1
x0=x0+h
y0=y1
print("Value of y at x",x0,"is = ",y0)
print("Value of y at x",round(x0,2),"is = ",round(y0,2))
Now if we want to check the error between obtained and exact solution
The exact solution of the given differential equation is
Y=-x-1+ex
Continue Program with the following
import math as mt
def f(x):
return -x-1+mt.exp(x)
print("Exact value = ",round(f(0.4),2))
To calculate the error between exact and obtained Continue Program
with the following
Err = abs(f(x0)-y0) #y0 is approx yn from eulers method and
f(x0) is exact value
print("Error = ",Err)
Program 2: Radioactivity
The initial mass of iodine isotope was 200g.
Determine the iodine mass after 30 days if the half
life of the isotope is 8 days.
Solve the differential equation using Euler’s method
(Take h=0.1).
dN/dt=-lambda*N
lambda=log(2)/T, where T=8days.

def f(t,N):
return -0.0866*N0
N0=int(input("Enter N0 = "))
t0=int(input("Enter t0 = "))
h=float(input("Enter h = "))
n=int(input("Enter n = "))
for i in range(n):
N1=N0+h*f(t0,N0)
t0=t0+h
N0=N1
print("Value of N at t",t0,"is = ",N0)
import math as mt
def f(t):
return 200*mt.exp(-0.0866*t)
print("Exact value = ",f(30))
Err = abs(f(30)-N0)
print("Error = ",Err)
Program 3:Newton Cooling Curve
A thermometer is taken from a room at
20oC outside where T =5oC. After 1 min, the
thermometer reads 12oC. Find out the
thermometer reading after 1 more min.
Solve the differential equation using Euler’s
method

def f(t,T):
return 0.7621*(5-T)
T0=int(input("Enter T0 = "))
t0=int(input("Enter t0 = "))
h=float(input("Enter h = "))
n=int(input("Enter n = "))
for i in range(n):
T1=T0+h*f(t0,T0)
t0=t0+h
T0=T1
print("Value of T at t",t0,"is = ",T0)
import math as mt
def f(t):
return 5+15*mt.exp(-0.7621*t)
print("Exact value = ",f(2))
Err = abs(f(2)-T0)
print("Error = ",Err)
PROGRAM 4
A coil having a resistance 15 ohms and an
inductance of 10 henries is connected to 90
volts supply. Determine the value of current
after 2 seconds.
By voltage law , Ri +L di/dt = E
Solve for the above differential equation
(Take h=0.01)

def f(t,i):
return 9-1.5*i
i0=int(input("Enter i0 = "))
t0=int(input("Enter t0 = "))
h=float(input("Enter h = "))
n=int(input("Enter n = "))
for i in range(n):
i1=i0+h*f(t0,i0)
t0=t0+h
i0=i1
print("Value of current at t",t0,"is = ",i0)
import math as mt
def f(t):
return 6*(1-mt.exp(-1.5*t))
print("Exact value = ",f(2))
Err = abs(f(2)-i0)
print("Error = ",Err)
PROGRAM 5: Program 1 using Modified
Eulers method
def f(x,y):
return x+y
x0=float(input("Enter x0 = "))
y0=float(input("Enter y0 = "))
h=float(input("Enter h = "))
n=int(input("Enter n = "))
for i in range(n+1):
yp1=y0+h*f(x0,y0)
x1=x0+h
y1=y0+(h/2)*(f(x0,y0)+f(x1,yp1))
print("Value of y at x",round(x0,2),"is = ",y0)
y0=y1
x0=x1
import math as mt
def f(x):
return -x-1+mt.exp(x)
print("Exact value = ",f(0.4))

Check how to evaluate error again……….


Err = abs(f(x0)-y0) #y0 is approx yn
print("Error = ",Err)

You might also like