Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

PROBLEM 14.

%PROGRAM CODE FOR 4TH ORDER CLASSICAL RUNGE-KUTTA


METHOD
clc
close all
clear all
f=@(x,y) x-y^2; %y'=f(x,y)
g=@(x) .75*exp(-2*x)+0.5*x+0.25;
%exact solution to the test equation y'=1+x-2y
x(1)=0;
y(1)=1;
exact(1)=1;
h=0.1;
fprintf('Grid Point\t y(exact) \t y(computed)\t
error\n');
fprintf('----------------------------------------------
---\n')
for i=1:4
k1=h*f(x(i),y(i));
k2=h*f(x(i)+0.5*h,y(i)+0.5*k1);
k3=h*f(x(i)+0.5*h,y(i)+0.5*k2);
k4=h*f(x(i)+h,y(i)+k3);
y(i+1)=y(i)+(k1+2*k2+2*k3+k4)/6;
x(i+1)=x(i)+h;
exact(i+1)=g(x(i+1));
err=abs(exact(i+1)-y(i+1));
fprintf('%.2f \t %.4f \t %.4f \t
%.4f\n',x(i+1),exact(i+1),y(i+1),err);
end
hold on
plot(x,y,'r') %plotting of the solution
plot(x,exact,'g')
xlabel('x')
ylabel('y')
title('4th Order Classical R-K Method')
OUTPUT :
Grid Point y(exact) y(computed) error
---------------------------------------------------------------------------
0.10 0.9140 0.9138 0.0003
0.20 0.8527 0.8512 0.0015
0.30 0.8116 0.8076 0.0040
0.40 0.7870 0.7798 0.0072

4th Order Classical R-K Method


1
Computed
solution
Exact
0.95
solution

0.9
y

0.85

0.8

0.75
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4
x

You might also like