0% found this document useful (0 votes)
164 views16 pages

Runge Kutta 2nd Order

The document describes solving a second order differential equation using the Runge-Kutta second order method. It provides the equation, initial conditions, parameters, and steps through the numerical solution. Over 5 steps, it calculates the slope (k1, k2) at each point, uses the Runge-Kutta formula to estimate the next y-value, and displays the results. The approximate solution is compared to the exact solution, with a relative error of 3.46399%.

Uploaded by

ALlan ABiang
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)
164 views16 pages

Runge Kutta 2nd Order

The document describes solving a second order differential equation using the Runge-Kutta second order method. It provides the equation, initial conditions, parameters, and steps through the numerical solution. Over 5 steps, it calculates the slope (k1, k2) at each point, uses the Runge-Kutta formula to estimate the next y-value, and displays the results. The approximate solution is compared to the exact solution, with a relative error of 3.46399%.

Uploaded by

ALlan ABiang
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/ 16

2nd order problems

1. there was a ball that has a velocity of 100m/s then when it hits the ground the acceleration has
decreases to Xo=0 to Xf=2 with the time of y= 1s the differential equation of the velocity of the
𝑑𝑦 6
ball is given by = 𝑦𝑥 2 − 𝑦
𝑑𝑥 5
Find the velocity at time n=5s

Manual solution: 1 2
ynew=y+( 𝑘1 + 𝑘2)𝑛
3 3
Step 1 y3=0.438218
K1=f(x0,y0)
= f(0,1)
= -1.2 Step 4
3 3
K2=𝑓(𝑥0 + 4 𝑛, 𝑦0 + 4 𝑘1𝑛) K1=f(x3,y3)
=f(2/5,13/25) =f(1.2,0.438218)
=-0.5409 =0.105172
1 2
ynew=y+(3 𝑘1 + 3 𝑘2)𝑛
3 3
y1= 0.65184 K2=𝑓(𝑥2 + 𝑛, 𝑦2 + 𝑘1𝑛)
4 4
=f(1.6,0.482086)
Step 2 =0.65319
K1=f(x1,y1)
=f(2/5,0.65184)
=-0.677914 1 2
ynew=y+( 𝑘1 + 𝑘2)𝑛
3 3
y4=0.58989
3 3
K2=𝑓(𝑥1 + 4 𝑛, 𝑦1 + 4 𝑘1𝑛)
=f(0.8,0.380675) Step 5
=-0.213178 K1=f(x4,y4)
=f(1.6,0.58989)
1 2 =0.80225
ynew=y+(3 𝑘1 + 3 𝑘2)𝑛
y2=0.473622
3 3
K2=𝑓(𝑥2 + 4 𝑛, 𝑦2 + 4 𝑘1𝑛)
Step 3 =f(2,0.91079)
K1=f(x2,y2) =2.55021
=f(0.8,0.473622)
=-0.265228
1 2
ynew=y+( 𝑘1 + 𝑘2)𝑛
3 3 3 3
K2=𝑓(𝑥2 + 4 𝑛, 𝑦2 + 4 𝑘1𝑛) y5=1.26038
=f(1.2,0.36753)
=0.0882073 True error = exact-approximate

=1.30561-1.26038
=0.0452262
Matlab result

The 2nd Order Runge-Kutta Method of Solving Ordinary Differential Equations

***************************Introduction*******************************
The 2nd Order Runge-Kutta method approximates the solution to an
ordinary differential equation by using the equation expressed in
the form dy/dx = f(x,y) to approximate the slope. This slope is used
to project the solution to the ODE a fixed distance away.

***************************Input Data*******************************
Below are the input parameters to begin the simulation which can be
changed in the m-file input section

f = dy/dx
x0 = initial x
y0 = initial y
xf = final x
a2 = constant value between 0 and 1.
= 0.5, Heun Method
= 1.0, Midpoint Method
= 0.66667, Ralston's Method
n = number of steps to take

-----------------------------------------------------------------

f(x,y) = dy/dx = y*x^2-1.2*y


x0 = 0
y0 = 1
xf = 2
a2 = 0.5
n=5

-----------------------------------------------------------------
For this simulation, the following parameter is constant.

h = ( xf - x0 ) / n
=(2-0)/5
= 0.4
a1 = 1 - a2
= 1 - 0.5
= 0.5

p1 = 1 / ( 2 * a2 )
= 1 / ( 2 * 0.5 )
=1

q11 = p1
=1

***************************Simulation******************************

Step 1
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x0 , y0 )
= f( 0 , 1 ) )
= -1.2

k2 = f( x0 + p1 * h , y0 + q11 * k1 * h )
= f( 0 + 1 * 0.4 , 1 + 1 * -1.2 * 0.4)
= f( 0.4 , 0.52 )
= -0.5408

2) Apply the Runge-Kutta 2nd Order method to estimate y1


y1 = y0 + ( a1 * k1 + a2 * k2 ) * h
= 1 + -0.8704 * 0.4
= 0.65184

at x1 = 0.4

Step 2
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x1 , y1 )
= f( 0.4 , 0.65184 ) )
= -0.677914

k2 = f( x1 + p1 * h , y1 + q11 * k1 * h )
= f( 0.4 + 1 * 0.4 , 0.65184 + 1 * -0.677914 * 0.4)
= f( 0.8 , 0.380675 )
= -0.213178

2) Apply the Runge-Kutta 2nd Order method to estimate y2


y2 = y1 + ( a1 * k1 + a2 * k2 ) * h
= 0.65184 + -0.445546 * 0.4
= 0.473622

at x2 = 0.8

Step 3
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x2 , y2 )
= f( 0.8 , 0.473622 ) )
= -0.265228

k2 = f( x2 + p1 * h , y2 + q11 * k1 * h )
= f( 0.8 + 1 * 0.4 , 0.473622 + 1 * -0.265228 * 0.4)
= f( 1.2 , 0.36753 )
= 0.0882073

2) Apply the Runge-Kutta 2nd Order method to estimate y3


y3 = y2 + ( a1 * k1 + a2 * k2 ) * h
= 0.473622 + -0.0885104 * 0.4
= 0.438218

at x3 = 1.2

Step 4
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x3 , y3 )
= f( 1.2 , 0.438218 ) )
= 0.105172

k2 = f( x3 + p1 * h , y3 + q11 * k1 * h )
= f( 1.2 + 1 * 0.4 , 0.438218 + 1 * 0.105172 * 0.4)
= f( 1.6 , 0.480286 )
= 0.65319
2) Apply the Runge-Kutta 2nd Order method to estimate y4
y4 = y3 + ( a1 * k1 + a2 * k2 ) * h
= 0.438218 + 0.379181 * 0.4
= 0.58989

at x4 = 1.6

Step 5
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x4 , y4 )
= f( 1.6 , 0.58989 ) )
= 0.80225

k2 = f( x4 + p1 * h , y4 + q11 * k1 * h )
= f( 1.6 + 1 * 0.4 , 0.58989 + 1 * 0.80225 * 0.4)
= f( 2 , 0.91079 )
= 2.55021

2) Apply the Runge-Kutta 2nd Order method to estimate y5


y5 = y4 + ( a1 * k1 + a2 * k2 ) * h
= 0.58989 + 1.67623 * 0.4
= 1.26038

at x5 = 2

********************************Results**********************************

Approximate = 1.26038
Exact = 1.30561

True Error = Exact - Approximate


= 1.30561 - 1.26038
= 0.0452262

Absolute Relative True Error Percentage


= | ( Exact - Approximate ) / Exact | * 100
= | 0.0452262 / 1.30561 | * 100
= 3.46399
>>
Matlab codes

clc
clf
clear all

fcnstr='y*x^2-1.2*y' ;
f=inline(fcnstr) ;

x0=0 ;

y0=1 ;

xf=2 ;

% a2 = 0.5 Heun's Method


% = 2/3 Ralston's Method
% = 1.0 Midpoint Method

a2=0.5 ;

% n, number of steps to take

n=5 ;

%**********************************************************************

% Displays title information


disp(sprintf('\n\nThe 2nd Order Runge-Kutta Method of Solving Ordinary
Differential Equations'))
disp(sprintf('\n***************************Introduction**********************
*********'))

% Displays introduction text


disp('The 2nd Order Runge-Kutta method approximates the solution to an ')
disp('ordinary differential equation by using the equation expressed in')
disp('the form dy/dx = f(x,y) to approximate the slope. This slope is used')
disp('to project the solution to the ODE a fixed distance away.')

% displays what inputs are used


disp(sprintf('\n\n***************************Input
Data*******************************'))
disp('Below are the input parameters to begin the simulation which can be')
disp('changed in the m-file input section')
disp(sprintf('\n f = dy/dx '))
disp(sprintf(' x0 = initial x '))
disp(sprintf(' y0 = initial y '))
disp(sprintf(' xf = final x '))
disp(sprintf(' a2 = constant value between 0 and 1.'))
disp(sprintf(' = 0.5, Heun Method'))
disp(sprintf(' = 1.0, Midpoint Method'))
disp(sprintf(' = 0.66667, Ralston''s Method'))
disp(sprintf(' n = number of steps to take'))
format short g
disp(sprintf('\n-------------------------------------------------------------
----\n'))
disp(sprintf([' f(x,y) = dy/dx = ' fcnstr]))
disp(sprintf(' x0 = %g',x0))
disp(sprintf(' y0 = %g',y0))
disp(sprintf(' xf = %g',xf))
disp(sprintf(' a2 = %g',a2))
disp(sprintf(' n = %g',n))
disp(sprintf('\n-------------------------------------------------------------
----'))
disp(sprintf('For this simulation, the following parameter is constant.\n'))

% Calculates constants used in the method


h=(xf-x0)/n ;
disp(sprintf(' h = ( xf - x0 ) / n '))
disp(sprintf(' = ( %g - %g ) / %g ',xf,x0,n))
disp(sprintf(' = %g',h))
a1=1-a2 ;
disp(sprintf('\n a1 = 1 - a2'))
disp(sprintf(' = 1 - %g',a2))
disp(sprintf(' = %g',a1))
p1=1/2/a2 ;
disp(sprintf('\n p1 = 1 / ( 2 * a2 )'))
disp(sprintf(' = 1 / ( 2 * %g )',a2))
disp(sprintf(' = %g',p1))
q11=p1 ;
disp(sprintf('\n q11 = p1'))
disp(sprintf(' = %g',q11))

xa(1)=x0 ;
ya(1)=y0 ;

disp(sprintf('\n\n***************************Simulation**********************
********'))

for i=1:n
disp(sprintf('\nStep %g',i))
disp(sprintf('-------------------------------------------------------------
----'))

% Adding Step Size


xa(i+1)=xa(i)+h ;

% Calculating k1 and k2
k1 = f(xa(i),ya(i)) ;
k2 = f(xa(i)+p1*h,ya(i)+q11*k1*h) ;

% Using 2nd Order Runge-Kutta formula


ya(i+1)=ya(i)+(a1*k1+a2*k2)*h ;

disp('1) Find k1 and k2 using the previous step information.')


disp(sprintf(' k1 = f( x%g , y%g )',i-1,i-1))
disp(sprintf(' = f( %g , %g ) )',xa(i),ya(i)))
disp(sprintf(' = %g\n',k1))
disp(sprintf(' k2 = f( x%g + p1 * h , y%g + q11 * k1 * h )',i-1,i-1))
disp(sprintf(' = f( %g + %g * %g , %g + %g * %g *
%g)',xa(i),p1,h,ya(i),q11,k1,h))
disp(sprintf(' = f( %g , %g )',xa(i)+p1*h,ya(i)+q11*k1*h))
disp(sprintf(' = %g\n',k2))

disp(sprintf('2) Apply the Runge-Kutta 2nd Order method to estimate


y%g',i))
disp(sprintf(' y%g = y%g + ( a1 * k1 + a2 * k2 ) * h',i,i-1))
disp(sprintf(' = %g + %g * %g',ya(i),a1*k1+a2*k2,h))
disp(sprintf(' = %g\n',ya(i+1)))
disp(sprintf(' at x%g = %g',i,xa(i+1)))

end

disp(sprintf('\n\n********************************Results********************
**************'))

% The following finds what is called the 'Exact' solution


xspan = [x0 xf];
[x,y]=ode45(f,xspan,y0);
[yfi dummy]=size(y);
yf=y(yfi);

% Plotting the Exact and Approximate solution of the ODE.


hold on
xlabel('x');ylabel('y');
title('Exact and Approximate Solution of the ODE by the 2nd Order Runge-Kutta
Method');
plot(x,y,'--','LineWidth',2,'Color',[0 0 1]);
plot(xa,ya,'-','LineWidth',2,'Color',[0 1 0]);
legend('Exact','Approximation');

disp(sprintf('\n Approximate = %g',ya(n+1)))


disp(sprintf(' Exact = %g',yf))
disp(sprintf('\n True Error = Exact - Approximate'))
disp(sprintf(' = %g - %g',yf,ya(n+1)))
disp(sprintf(' = %g',yf-ya(n+1)))
disp(sprintf('\n Absolute Relative True Error Percentage'))
disp(sprintf(' = | ( Exact - Approximate ) / Exact | * 100'))
disp(sprintf(' = | %g / %g | * 100',yf-ya(n+1),yf))
disp(sprintf(' = %g',abs( (yf-ya(n+1))/yf )*100))

2. a boy stand still on a air-condition room with 15 degree Celsius, then the boy tries to change the
speed of the air condition to Xo=0, Xf=3 find the temperature rate when the boy lower the level
of the air condition the differential equation of the room temperature is.
𝑑𝑦
𝑑𝑥
= 𝑥𝑦 2 − 2.1𝑥 Find the temperature at n=5s

Manual solution: y3=-0.550826


Step 1
K1=f(x0,y0)
= f(0,1) Step 4
=0 K1=f(x3,y3)
3 3 =f(1.8,-0.550826)
K2=𝑓(𝑥0 + 𝑛, 𝑦0 + 𝑘1𝑛)
4 4
=f(0.6,1) =-3.23386
=-0.66
3 3
1 2
ynew=y+( 𝑘1 + 𝑘2)𝑛 K2=𝑓(𝑥2 + 4 𝑛, 𝑦2 + 4 𝑘1𝑛)
3 3
y1= 0.802 =f(2.4,-2.49114)
=9.85392
Step 2
K1=f(x1,y1)
1 2
=f(0.6,0.802) ynew=y+(3 𝑘1 + 3 𝑘2)𝑛
=-0.874078 y4=1.43519

3 3 Step 5
K2=𝑓(𝑥1 + 4 𝑛, 𝑦1 + 4 𝑘1𝑛)
=f(1.2,0.277553) K1=f(x4,y4)
=-2.42756 =f(2.4,1.43519)
=-0.0965512
1 2
ynew=y+(3 𝑘1 + 3 𝑘2)𝑛 3 3
K2=𝑓(𝑥2 + 4 𝑛, 𝑦2 + 4 𝑘1𝑛)
y2=-0.18849
=f(3,1.37726)
Step 3 =-0.609471
K1=f(x2,y2)
=f(1.2,-0.18849)
1 2
=-2.47737 ynew=y+(3 𝑘1 + 3 𝑘2)𝑛
y5=1.22338
3 3
K2=𝑓(𝑥2 + 4
𝑛, 𝑦2 + 4
𝑘1𝑛)
=f(1.8,-1.67491) True error = exact-approximate
=1.26958
= -1.44906-1.22338
=-2.67244
1 2
ynew=y+( 𝑘1 + 𝑘2)𝑛
3 3
Matlab result

The 2nd Order Runge-Kutta Method of Solving Ordinary Differential Equations

***************************Introduction*******************************
The 2nd Order Runge-Kutta method approximates the solution to an
ordinary differential equation by using the equation expressed in
the form dy/dx = f(x,y) to approximate the slope. This slope is used
to project the solution to the ODE a fixed distance away.

***************************Input Data*******************************
Below are the input parameters to begin the simulation which can be
changed in the m-file input section

f = dy/dx
x0 = initial x
y0 = initial y
xf = final x
a2 = constant value between 0 and 1.
= 0.5, Heun Method
= 1.0, Midpoint Method
= 0.66667, Ralston's Method
n = number of steps to take

-----------------------------------------------------------------

f(x,y) = dy/dx = x*y^2-2.1*x


x0 = 0
y0 = 1
xf = 3
a2 = 0.5
n=5

-----------------------------------------------------------------
For this simulation, the following parameter is constant.

h = ( xf - x0 ) / n
=(3-0)/5
= 0.6
a1 = 1 - a2
= 1 - 0.5
= 0.5

p1 = 1 / ( 2 * a2 )
= 1 / ( 2 * 0.5 )
=1

q11 = p1
=1

***************************Simulation******************************

Step 1
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x0 , y0 )
= f( 0 , 1 ) )
=0

k2 = f( x0 + p1 * h , y0 + q11 * k1 * h )
= f( 0 + 1 * 0.6 , 1 + 1 * 0 * 0.6)
= f( 0.6 , 1 )
= -0.66

2) Apply the Runge-Kutta 2nd Order method to estimate y1


y1 = y0 + ( a1 * k1 + a2 * k2 ) * h
= 1 + -0.33 * 0.6
= 0.802

at x1 = 0.6

Step 2
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x1 , y1 )
= f( 0.6 , 0.802 ) )
= -0.874078

k2 = f( x1 + p1 * h , y1 + q11 * k1 * h )
= f( 0.6 + 1 * 0.6 , 0.802 + 1 * -0.874078 * 0.6)
= f( 1.2 , 0.277553 )
= -2.42756

2) Apply the Runge-Kutta 2nd Order method to estimate y2


y2 = y1 + ( a1 * k1 + a2 * k2 ) * h
= 0.802 + -1.65082 * 0.6
= -0.18849

at x2 = 1.2

Step 3
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x2 , y2 )
= f( 1.2 , -0.18849 ) )
= -2.47737

k2 = f( x2 + p1 * h , y2 + q11 * k1 * h )
= f( 1.2 + 1 * 0.6 , -0.18849 + 1 * -2.47737 * 0.6)
= f( 1.8 , -1.67491 )
= 1.26958

2) Apply the Runge-Kutta 2nd Order method to estimate y3


y3 = y2 + ( a1 * k1 + a2 * k2 ) * h
= -0.18849 + -0.603892 * 0.6
= -0.550826

at x3 = 1.8

Step 4
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x3 , y3 )
= f( 1.8 , -0.550826 ) )
= -3.23386

k2 = f( x3 + p1 * h , y3 + q11 * k1 * h )
= f( 1.8 + 1 * 0.6 , -0.550826 + 1 * -3.23386 * 0.6)
= f( 2.4 , -2.49114 )
= 9.85392

2) Apply the Runge-Kutta 2nd Order method to estimate y4


y4 = y3 + ( a1 * k1 + a2 * k2 ) * h
= -0.550826 + 3.31003 * 0.6
= 1.43519

at x4 = 2.4

Step 5
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x4 , y4 )
= f( 2.4 , 1.43519 ) )
= -0.0965512

k2 = f( x4 + p1 * h , y4 + q11 * k1 * h )
= f( 2.4 + 1 * 0.6 , 1.43519 + 1 * -0.0965512 * 0.6)
= f( 3 , 1.37726 )
= -0.609471

2) Apply the Runge-Kutta 2nd Order method to estimate y5


y5 = y4 + ( a1 * k1 + a2 * k2 ) * h
= 1.43519 + -0.353011 * 0.6
= 1.22338

at x5 = 3

********************************Results**********************************

Approximate = 1.22338
Exact = -1.44906

True Error = Exact - Approximate


= -1.44906 - 1.22338
= -2.67244

Absolute Relative True Error Percentage


= | ( Exact - Approximate ) / Exact | * 100
= | -2.67244 / -1.44906 | * 100
= 184.426

Matlab codes
clc
clf
clear all

fcnstr='x*y^2-2.1*x' ;
f=inline(fcnstr) ;

x0=0 ;

y0=1 ;

xf=3 ;

% a2 = 0.5 Heun's Method


% = 2/3 Ralston's Method
% = 1.0 Midpoint Method

a2=0.5 ;

% n, number of steps to take

n=5 ;

%**********************************************************************

% Displays title information


disp(sprintf('\n\nThe 2nd Order Runge-Kutta Method of Solving Ordinary
Differential Equations'))
disp(sprintf('\n***************************Introduction**********************
*********'))

% Displays introduction text


disp('The 2nd Order Runge-Kutta method approximates the solution to an ')
disp('ordinary differential equation by using the equation expressed in')
disp('the form dy/dx = f(x,y) to approximate the slope. This slope is used')
disp('to project the solution to the ODE a fixed distance away.')

% displays what inputs are used


disp(sprintf('\n\n***************************Input
Data*******************************'))
disp('Below are the input parameters to begin the simulation which can be')
disp('changed in the m-file input section')
disp(sprintf('\n f = dy/dx '))
disp(sprintf(' x0 = initial x '))
disp(sprintf(' y0 = initial y '))
disp(sprintf(' xf = final x '))
disp(sprintf(' a2 = constant value between 0 and 1.'))
disp(sprintf(' = 0.5, Heun Method'))
disp(sprintf(' = 1.0, Midpoint Method'))
disp(sprintf(' = 0.66667, Ralston''s Method'))
disp(sprintf(' n = number of steps to take'))
format short g
disp(sprintf('\n-------------------------------------------------------------
----\n'))
disp(sprintf([' f(x,y) = dy/dx = ' fcnstr]))
disp(sprintf(' x0 = %g',x0))
disp(sprintf(' y0 = %g',y0))
disp(sprintf(' xf = %g',xf))
disp(sprintf(' a2 = %g',a2))
disp(sprintf(' n = %g',n))
disp(sprintf('\n-------------------------------------------------------------
----'))
disp(sprintf('For this simulation, the following parameter is constant.\n'))

% Calculates constants used in the method


h=(xf-x0)/n ;
disp(sprintf(' h = ( xf - x0 ) / n '))
disp(sprintf(' = ( %g - %g ) / %g ',xf,x0,n))
disp(sprintf(' = %g',h))
a1=1-a2 ;
disp(sprintf('\n a1 = 1 - a2'))
disp(sprintf(' = 1 - %g',a2))
disp(sprintf(' = %g',a1))
p1=1/2/a2 ;
disp(sprintf('\n p1 = 1 / ( 2 * a2 )'))
disp(sprintf(' = 1 / ( 2 * %g )',a2))
disp(sprintf(' = %g',p1))
q11=p1 ;
disp(sprintf('\n q11 = p1'))
disp(sprintf(' = %g',q11))

xa(1)=x0 ;
ya(1)=y0 ;

disp(sprintf('\n\n***************************Simulation**********************
********'))

for i=1:n
disp(sprintf('\nStep %g',i))
disp(sprintf('-------------------------------------------------------------
----'))

% Adding Step Size


xa(i+1)=xa(i)+h ;

% Calculating k1 and k2
k1 = f(xa(i),ya(i)) ;
k2 = f(xa(i)+p1*h,ya(i)+q11*k1*h) ;

% Using 2nd Order Runge-Kutta formula


ya(i+1)=ya(i)+(a1*k1+a2*k2)*h ;

disp('1) Find k1 and k2 using the previous step information.')


disp(sprintf(' k1 = f( x%g , y%g )',i-1,i-1))
disp(sprintf(' = f( %g , %g ) )',xa(i),ya(i)))
disp(sprintf(' = %g\n',k1))
disp(sprintf(' k2 = f( x%g + p1 * h , y%g + q11 * k1 * h )',i-1,i-1))
disp(sprintf(' = f( %g + %g * %g , %g + %g * %g *
%g)',xa(i),p1,h,ya(i),q11,k1,h))
disp(sprintf(' = f( %g , %g )',xa(i)+p1*h,ya(i)+q11*k1*h))
disp(sprintf(' = %g\n',k2))

disp(sprintf('2) Apply the Runge-Kutta 2nd Order method to estimate


y%g',i))
disp(sprintf(' y%g = y%g + ( a1 * k1 + a2 * k2 ) * h',i,i-1))
disp(sprintf(' = %g + %g * %g',ya(i),a1*k1+a2*k2,h))
disp(sprintf(' = %g\n',ya(i+1)))
disp(sprintf(' at x%g = %g',i,xa(i+1)))

end

disp(sprintf('\n\n********************************Results********************
**************'))

% The following finds what is called the 'Exact' solution


xspan = [x0 xf];
[x,y]=ode45(f,xspan,y0);
[yfi dummy]=size(y);
yf=y(yfi);

% Plotting the Exact and Approximate solution of the ODE.


hold on
xlabel('x');ylabel('y');
title('Exact and Approximate Solution of the ODE by the 2nd Order Runge-Kutta
Method');
plot(x,y,'--','LineWidth',2,'Color',[0 0 1]);
plot(xa,ya,'-','LineWidth',2,'Color',[0 1 0]);
legend('Exact','Approximation');

disp(sprintf('\n Approximate = %g',ya(n+1)))


disp(sprintf(' Exact = %g',yf))
disp(sprintf('\n True Error = Exact - Approximate'))
disp(sprintf(' = %g - %g',yf,ya(n+1)))
disp(sprintf(' = %g',yf-ya(n+1)))
disp(sprintf('\n Absolute Relative True Error Percentage'))
disp(sprintf(' = | ( Exact - Approximate ) / Exact | * 100'))
disp(sprintf(' = | %g / %g | * 100',yf-ya(n+1),yf))
disp(sprintf(' = %g',abs( (yf-ya(n+1))/yf )*100))

You might also like