0% found this document useful (0 votes)
29 views

Python Program For RK Method

Uploaded by

Joonie Crab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Python Program For RK Method

Uploaded by

Joonie Crab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Program 1

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


differential equation using RK2 Method
dy/dx = (x+y)/x

Given initial condition y(1)=2. Take h = 0.5


def f(x,y):
return (x+y)/x
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):
k1=h*f(x0,y0)
k2=h*f(x0+h,y0+k1)
y1=y0+(k1+k2)*0.5
x0=x0+h
y0=y1
print("Value of y at x",round(x0,2),"is = ",round(y0,4))
import math as mt
def f(x):
return x*mt.log(x)+2*x
print("Exact value = ",round(f(2),4))
Err = abs(f(x0)-y0)
print("Error = ",Err)

Same Program :Using RK 4 Method


def f(x,y):
return (x+y)/x
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):
k1=h*f(x0,y0)
k2=h*f(x0+h*0.5,y0+k1*0.5)
k3=h*f(x0+h*0.5,y0+k2*0.5)
k4=h*f(x0+h,y0+k3)
y1=y0+(1/6)*(k1+2*k2+2*k3+k4)
x0=x0+h
y0=y1
print("Value of y at x",round(x0,2),"is = ",round(y0,4))
import math as mt
def f(x):
return x*mt.log(x)+2*x
print("Exact value = ",round(f(2),4))
Err = abs(f(x0)-y0)
print("Error = ",Err)

You might also like