0% found this document useful (0 votes)
49 views10 pages

Xavier University - Ateneo de Cagayan University College of Engineering Electronics Engineering Department

The document discusses solving second-order ordinary differential equations numerically using Runge-Kutta methods such as Heun's method, the midpoint method, and Ralston's method. It presents the mathematical formulas for each method and MATLAB code to apply the methods to the ODE dy/dt = -4y with initial conditions y(0)=1, y'(0)=0. The codes calculate the solution over time steps from t=0 to t=10 and output the step-by-step results, showing differences between the methods' accuracy.

Uploaded by

Mor DepRz
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)
49 views10 pages

Xavier University - Ateneo de Cagayan University College of Engineering Electronics Engineering Department

The document discusses solving second-order ordinary differential equations numerically using Runge-Kutta methods such as Heun's method, the midpoint method, and Ralston's method. It presents the mathematical formulas for each method and MATLAB code to apply the methods to the ODE dy/dt = -4y with initial conditions y(0)=1, y'(0)=0. The codes calculate the solution over time steps from t=0 to t=10 and output the step-by-step results, showing differences between the methods' accuracy.

Uploaded by

Mor DepRz
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/ 10

Xavier University - Ateneo de Cagayan University

College of Engineering
Electronics Engineering Department

MACHINE PROBLEM #4
SOLVING ODE 2ND ORDER RUNGE-KUTTA METHOD

Submitted by:

Instructor:

ROMMEL PEDRAZA

ENGR. MARY JEAN O. APOR, PECE, MEng

BSEcE 5

Title and Objective

(05):____

Procedure, Data, Results

(20):____

Observation, Analysis

(30):____

Conclusion

(25):____

Tardiness

(10):____

Presentation, Neatness

(20):____

Total

(100):____

INTRODUCTION
In mathematics and computational

science, Heun's

method may

refer

to

the improved[1] or modified Euler's method (that is, the explicit trapezoidal rule[2]), or a similar twostage RungeKutta method. It is named after Karl Heun and is a numerical procedure for
solving ordinary differential equations (ODEs) with a given initial value. Both variants can be seen as
extensions of theEuler method into two-stage second-order RungeKutta methods.
The procedure for calculating the numerical solution to the initial value problem via the improved
Euler's method is:

by way of Heun's method, is to first calculate the intermediate value


approximation

and then the final

at the next integration point.

The midpoint method is a one-step method for numerically solving the differential equation, y'(t) = f(t,
y(t)),

\quad

y(t_0)

y_0

and

is

given

by

the

formula

y_{n+1}

y_n

hf\left(t_n+\frac{h}{2},y_n+\frac{h}{2}f(t_n, y_n)\right), \qquad\qquad (1) for n=0, 1, 2, \dots Here, h is


the step size a small positive number, t_n=t_0 + n h, and y_n is the computed approximate value of
y(t_n). The midpoint method is also known as the modified Euler method.
The name of the method comes from the fact that in the formula above the function f is evaluated at
t=t_n+h/2, which is the midpoint between t_n at which the value of y(t) is known and t_{n+1} at which
the value of y(t) needs to be found.
The local error at each step of the midpoint method is of order O\left(h^3\right), giving a global error of
order O\left(h^2\right). Thus, while more computationally intensive than Euler's method, the midpoint
method generally gives more accurate results.
The method is an example of a class of higher-order methods known as Runge-Kutta methods.

Ralston's second order method is a Runge-Kutta method for approximating the solution of the initial
value problem y'(x) = f(x,y); y(x0) = y0 which evaluates the integrand, f(x,y), twice for each step. For step
i+1,

yi+1 = yi + 1/4 ( k1 + 3 k2 ),

where
k1 = h f(xi, yi),
k2 = h f(xi + 2 h / 3, yi + 2 k1 / 3 ),
and xi = x0 + i h.
Ralston's second order method is a second order procedure for which Richardson extrapolation can be
used.

OBJECTIVES
To solve [d2/dt2](y) + 4y = 0, given the initial conditions, y(0) = 1 and y(0) = 0, from t = 0 to 10
Using:
2nd order Runge-Kutta methods
a.

Heuns

b.

Midpoint

c.

Ralstons

3rd order, 4th order, and 5th order Runge-Kutta methods


To apply various method for solving ordinary differential equations numerically using MATLAB
software.
To compare and evaluate each method
MATLAB CODES
Heuns Method
iter1 = 0;
yi1 = 0;
yiold = 0;
z = 0;
%%
%function
ya = -4;
za =1;
y0 =1;
z0 =0;
t = 0;
%%
%heun's method
h1 = input('input step size (h):');
while t <= 10;
iter1 = iter1 +1;

k1y = za*z0;
k1z = ya*y0;
k2y = za*(z0 + k1z*h1);
k2z = ya*(y0 + k1y*h1);
yi1 = y0 + 0.5*h1*(k1y + k2y);
zi1 = z0 + 0.5*h1*(k1z + k2z);
ea = abs(((yi1 - y0)/yi1))*100;
disp(['Iteration
',num2str(iter1)]);
disp(['t =', num2str(t)]);
disp(['k1y = ', num2str(k1y)]);
disp(['k1z = ', num2str(k1z)]);
disp(['k2y = ', num2str(k2y)]);
disp(['k2z = ', num2str(k2z)]);
disp(['y(i+1) = ', num2str(yi1)]);
disp(['z(i+1) = ', num2str(zi1)]);

disp(['Approximate relative error


=',num2str(ea),'%']);
disp('--------------------------');
t = t+ h1;

y0 = yi1;
z0 = zi1;
else
break;
end;
end;

if (t <= 10)

Midpoint Method
clc;
clear all;
%%
%constants
iter1 = 0;
yi1 = 0;
yiold = 0;
z = 0;
%%
%function
ay = -4;
az =1;
y0 =1;
z0 =0;
t = 0;
%%
%midpoint method
h1 = input('input step size:');
while t <= 10;
iter1 = iter1 +1;
k1y = az*z0;
k1z = ay*y0;
k2y = az*(z0 + 0.5*k1z*h1);
Ralstons Method
clc;
clear all;
%%
%constants
iter1 = 0;
yi1 = 0;
yiold = 0;
z = 0;
%%
%function
ay = -4;
az =1;
y0 =1;
z0 =0;
t = 0;

k2z = ay*(y0 + 0.5*k1y*h1);


yi1 = y0 + h1*k2y;
zi1 = z0 + h1*k2z;
ea = abs(((yi1 - y0)/yi1))*100;
disp(['iteration ',num2str(iter1)]);
disp(['t =', num2str(t)]);
disp(['k1y = ', num2str(k1y)]);
disp(['k1z = ', num2str(k1z)]);
disp(['k2y = ', num2str(k2y)]);
disp(['k2z = ', num2str(k2z)]);
disp(['y(i+1) = ', num2str(yi1)]);
disp(['z(i+1) = ', num2str(zi1)]);
disp(['ea =',num2str(ea),'%']);
disp('---------------------------');
t = t+ h1;
if (t <= 10)
y0 = yi1;
z0 = zi1;
else
break;
end;
end;

%%
%heun's method
h1 = input('input step size:');
while t <= 10;
iter1 = iter1 +1;
k1y = az*z0;
k1z = ay*y0;
k2y = az*(z0 + 0.75*k1z*h1);
k2z = ay*(y0 + 0.75*k1y*h1);
yi1 = y0 + h1*((1/3)*k1y +(2/3)* k2y);
zi1 = z0 + h1*((1/3)*k1z + (2/3)*k2z);
ea = abs(((yi1 - y0)/yi1))*100;
disp(['iteration ',num2str(iter1)]);
disp(['t =', num2str(t)]);
disp(['k1y = ', num2str(k1y)]);

disp(['k1z = ', num2str(k1z)]);


disp(['k2y = ', num2str(k2y)]);
disp(['k2z = ', num2str(k2z)]);
disp(['y(i+1) = ', num2str(yi1)]);
disp(['z(i+1) = ', num2str(zi1)]);
disp(['ea =',num2str(ea),'%']);
disp('---------------------------');
t = t+ h1;

Runge-Kutta Method
clc;
clear all;
%%
%constants
iter1 = 0;
yi1 = 0;
yiold = 0;
z = 0;
%%
%function
ay = -4;
az =1;
y0 =1;
z0 =0;
t = 0;
%%
%heun's method
h1 = input('input step size:');
while t <= 10;
iter1 = iter1 +1;
k1y = az*z0;
k1z = ay*y0;
k2y = az*(z0 + k1z*h1);

if (t <= 10)
y0 = yi1;
z0 = zi1;
else
break;
end;
end;

k2z = ay*(y0 + k1y*h1);


yi1 = y0 + 0.5*h1*(k1y + k2y);
zi1 = z0 + 0.5*h1*(k1z + k2z);
ea = abs(((yi1 - y0)/yi1))*100;
disp(['iteration ',num2str(iter1)]);
disp(['t =', num2str(t)]);
disp(['k1y = ', num2str(k1y)]);
disp(['k1z = ', num2str(k1z)]);
disp(['k2y = ', num2str(k2y)]);
disp(['k2z = ', num2str(k2z)]);
disp(['y(i+1) = ', num2str(yi1)]);
disp(['z(i+1) = ', num2str(zi1)]);
disp(['ea =',num2str(ea),'%']);
disp('---------------------------');
t = t+ h1;
if (t <= 10)
y0 = yi1;
z0 = zi1;
else
break;
end;
end;

OUTPUT
Heuns Method
input step size (h):1
Iteration 1
t =0
k1y = 0
k1z = -4
k2y = -4
k2z = -4
y(i+1) = -1
z(i+1) = -4

Approximate relative error =200%


--------------------------Iteration 2
t =1
k1y = -4
k1z = 4
k2y = 0
k2z = 20
y(i+1) = -3

z(i+1) = 8
Approximate relative error =66.6667%
--------------------------Iteration 3
t =2
k1y = 8
k1z = 12
k2y = 20
k2z = -20
y(i+1) = 11
z(i+1) = 4
Approximate relative error =127.2727%
--------------------------Iteration 4
t =3
k1y = 4
k1z = -44
k2y = -40
k2z = -60
y(i+1) = -7
z(i+1) = -48
Approximate relative error =257.1429%
--------------------------Iteration 5
t =4
k1y = -48
k1z = 28
k2y = -20
k2z = 220
y(i+1) = -41
z(i+1) = 76
Approximate relative error =82.9268%
--------------------------Iteration 6
t =5
k1y = 76
k1z = 164
k2y = 240
k2z = -140
y(i+1) = 117
z(i+1) = 88
Approximate relative error =135.0427%
--------------------------Iteration 7
t =6
k1y = 88
Midpoint Method

k1z = -468
k2y = -380
k2z = -820
y(i+1) = -29
z(i+1) = -556
Approximate relative error =503.4483%
--------------------------Iteration 8
t =7
k1y = -556
k1z = 116
k2y = -440
k2z = 2340
y(i+1) = -527
z(i+1) = 672
Approximate relative error =94.4972%
--------------------------Iteration 9
t =8
k1y = 672
k1z = 2108
k2y = 2780
k2z = -580
y(i+1) = 1199
z(i+1) = 1436
Approximate relative error =143.9533%
--------------------------Iteration 10
t =9
k1y = 1436
k1z = -4796
k2y = -3360
k2z = -10540
y(i+1) = 237
z(i+1) = -6232
Approximate relative error =405.9072%
--------------------------Iteration 11
t =10
k1y = -6232
k1z = -948
k2y = -7180
k2z = 23980
y(i+1) = -6469
z(i+1) = 5284
Approximate relative error =103.6636%

input step size:1


iteration 1
t =0
k1y = 0
k1z = -4
k2y = -2
k2z = -4
y(i+1) = -1
z(i+1) = -4
ea =200%
--------------------------iteration 2
t =1
k1y = -4
k1z = 4
k2y = -2
k2z = 12
y(i+1) = -3
z(i+1) = 8
ea =66.6667%
--------------------------iteration 3
t =2
k1y = 8
k1z = 12
k2y = 14
k2z = -4
y(i+1) = 11
z(i+1) = 4
ea =127.2727%
--------------------------iteration 4
t =3
k1y = 4
k1z = -44
k2y = -18
k2z = -52
y(i+1) = -7
z(i+1) = -48
ea =257.1429%
--------------------------iteration 5
t =4
k1y = -48
k1z = 28
k2y = -34
k2z = 124
y(i+1) = -41

z(i+1) = 76
ea =82.9268%
--------------------------iteration 6
t =5
k1y = 76
k1z = 164
k2y = 158
k2z = 12
y(i+1) = 117
z(i+1) = 88
ea =135.0427%
--------------------------iteration 7
t =6
k1y = 88
k1z = -468
k2y = -146
k2z = -644
y(i+1) = -29
z(i+1) = -556
ea =503.4483%
--------------------------iteration 8
t =7
k1y = -556
k1z = 116
k2y = -498
k2z = 1228
y(i+1) = -527
z(i+1) = 672
ea =94.4972%
--------------------------iteration 9
t =8
k1y = 672
k1z = 2108
k2y = 1726
k2z = 764
y(i+1) = 1199
z(i+1) = 1436
ea =143.9533%
--------------------------iteration 10
t =9
k1y = 1436
k1z = -4796
k2y = -962

k2z = -7668
y(i+1) = 237
z(i+1) = -6232
ea =405.9072%
--------------------------iteration 11
t =10

k1y = -6232
k1z = -948
k2y = -6706
k2z = 11516
y(i+1) = -6469
z(i+1) = 5284
ea =103.6636%

Ralston Method
input step size:1
iteration 1
t =0
k1y = 0
k1z = -4
k2y = -3
k2z = -4
y(i+1) = -1
z(i+1) = -4
ea =200%
--------------------------iteration 2
t =1
k1y = -4
k1z = 4
k2y = -1
k2z = 16
y(i+1) = -3
z(i+1) = 8
ea =66.6667%
--------------------------iteration 3
t =2
k1y = 8
k1z = 12
k2y = 17
k2z = -12
y(i+1) = 11
z(i+1) = 4
ea =127.2727%
--------------------------iteration 4
t =3
k1y = 4
k1z = -44
k2y = -29
k2z = -56
y(i+1) = -7
z(i+1) = -48

ea =257.1429%
--------------------------iteration 5
t =4
k1y = -48
k1z = 28
k2y = -27
k2z = 172
y(i+1) = -41
z(i+1) = 76
ea =82.9268%
--------------------------iteration 6
t =5
k1y = 76
k1z = 164
k2y = 199
k2z = -64
y(i+1) = 117
z(i+1) = 88
ea =135.0427%
--------------------------iteration 7
t =6
k1y = 88
k1z = -468
k2y = -263
k2z = -732
y(i+1) = -29
z(i+1) = -556
ea =503.4483%
--------------------------iteration 8
t =7
k1y = -556
k1z = 116
k2y = -469
k2z = 1784
y(i+1) = -527

z(i+1) = 672
ea =94.4972%
--------------------------iteration 9
t =8
k1y = 672
k1z = 2108
k2y = 2253
k2z = 92
y(i+1) = 1199
z(i+1) = 1436
ea =143.9533%
--------------------------iteration 10
t =9
k1y = 1436

k1z = -4796
k2y = -2161
k2z = -9104
y(i+1) = 237
z(i+1) = -6232
ea =405.9072%
--------------------------iteration 11
t =10
k1y = -6232
k1z = -948
k2y = -6943
k2z = 17748
y(i+1) = -6469
z(i+1) = 5284
ea =103.6636%

Runge Kutta Method


input step size:1
iteration 1
t =0
k1y = 0
k1z = -4
k2y = -4
k2z = -4
y(i+1) = -1
z(i+1) = -4
ea =200%
--------------------------iteration 2
t =1
k1y = -4
k1z = 4
k2y = 0
k2z = 20
y(i+1) = -3
z(i+1) = 8
ea =66.6667%
--------------------------iteration 3
t =2
k1y = 8
k1z = 12
k2y = 20
k2z = -20
y(i+1) = 11
z(i+1) = 4
ea =127.2727%

--------------------------iteration 4
t =3
k1y = 4
k1z = -44
k2y = -40
k2z = -60
y(i+1) = -7
z(i+1) = -48
ea =257.1429%
--------------------------iteration 5
t =4
k1y = -48
k1z = 28
k2y = -20
k2z = 220
y(i+1) = -41
z(i+1) = 76
ea =82.9268%
--------------------------iteration 6
t =5
k1y = 76
k1z = 164
k2y = 240
k2z = -140
y(i+1) = 117
z(i+1) = 88
ea =135.0427%

--------------------------iteration 7
t =6
k1y = 88
k1z = -468
k2y = -380
k2z = -820
y(i+1) = -29
z(i+1) = -556
ea =503.4483%
--------------------------iteration 8
t =7
k1y = -556
k1z = 116
k2y = -440
k2z = 2340
y(i+1) = -527
z(i+1) = 672
ea =94.4972%
--------------------------iteration 9
t =8
k1y = 672
k1z = 2108

ANALYSIS AND CONCLUSION

k2y = 2780
k2z = -580
y(i+1) = 1199
z(i+1) = 1436
ea =143.9533%
--------------------------iteration 10
t =9
k1y = 1436
k1z = -4796
k2y = -3360
k2z = -10540
y(i+1) = 237
z(i+1) = -6232
ea =405.9072%
--------------------------iteration 11
t =10
k1y = -6232
k1z = -948
k2y = -7180
k2z = 23980
y(i+1) = -6469
z(i+1) = 5284
ea =103.6636%

You might also like