Sie sind auf Seite 1von 4

4th RUNGE-KUTTA METHOD w/ MATLAB

The Runge-Kutta method is the most popular method for solving ordinary differential
equations (ODEs) by means of numerical approximations. There are several version of
the method depending on the desired accuracy. The most popular Runge-Kutta method
is of fourth order, which in simpler terms means that the error is of the order of h4 ,
abbreviated as O(h4).

We will attempt to solve a simple differential equation with the simplest Runge-Kutta
method available. The formulas for the fourth-order Runge-Kutta are
where

h = (b-a)/n, the step size of the independent variable


n = the number of values of the independent variable in which we wish to calculate the
approximate solution

[a, b] = the limits of the t interval.


k1, k2, k3, k4 Runge-Kutta parameters or functions (actually ‘slopes’)
f(ti,yi)=function evaluated at ti and yi
1 1 1 1
f (t i  h, yi  k1 )  function evaluated at ti  h, yi  k1
2 2 2 2

In order to calculate a new point in the solution yi+1 you need the previous solution yi
and k1 , k2 , k3 , and k4 in that order. Then the calculation sequence is k1, k2, k3, k4, and
then yi+1.

EXAMPLE-1
Below a MATLAB program to implement the fourth-order Runge-Kutta method to solve

y'  3et  0.4 y, y(0)  5 on t  [0,3] .

For h= 0.1; The RK method is highly accurate for small h.

% Runge-Kutta 4th order with MATLAB


% It calculates an ODE using Runge-Kutta 4th order method
% Equation to solve: Y'=3*exp(-t)-0.4*Y; Y(0)=5; t=[0,3];
% Author Ido Schwartz, modified by Marco Arocha

clc, clear all, close all

% instruction to write results on external file


fid=fopen('RKout.m','w');

h=0.1; a=0; b=3; % h is the step size, t=[a,b] t-range


t = a:h:b; % Computes t-array up to t=3
y = zeros(1,numel(t)); % Memory preallocation
y(1) = 5; % initial condition; in MATLAB indices start at 1

Fyt = @(t,y) 3.*exp(-t)-0.4*y; % change the function as you desire


% the function is the expression after (t,y)
% table title
fprintf(fid,'%7s %7s %7s %7s %7s %7s %7s \n','i','t(i)','k1','k2','k3',
'k4','y(i)');

for ii=1:1:numel(t)
k1 = Fyt(t(ii),y(ii));
k2 = Fyt(t(ii)+0.5*h,y(ii)+0.5*h*k1);
k3 = Fyt((t(ii)+0.5*h),(y(ii)+0.5*h*k2));
k4 = Fyt((t(ii)+h),(y(ii)+h*k3));

y(ii+1) = y(ii) + (h/6)*(k1+2*k2+2*k3+k4); % main equation


% table data
fprintf(fid,'%7d %7.2f %7.3f %7.3f',ii, t(ii), k1, k2);
fprintf(fid,' %7.3f %7.3f %7.3f \n', k3, k4, y(ii));
end

y(numel(t))=[ ]; % erase the last computation of y(n+1)

% Solution PLOT:
plot(t,y,' ok')
title('RK-4--Numerical Solution---');
ylabel('y'); xlabel('t'); legend('numerical');
grid on

fclose(fid);

MATLAB solution

RKout.m (output file):

i t(i) k1 k2 k3 k4 y(i)
1 0.00 1.000 0.834 0.837 0.681 5.000
2 0.10 0.681 0.535 0.538 0.401 5.084
3 0.20 0.401 0.273 0.276 0.156 5.138
4 0.30 0.156 0.045 0.047 -0.057 5.165
5 0.40 -0.057 -0.154 -0.152 -0.242 5.170
6 0.50 -0.242 -0.326 -0.324 -0.402 5.155
7 0.60 -0.402 -0.475 -0.473 -0.540 5.122
8 0.70 -0.540 -0.602 -0.601 -0.658 5.075
9 0.80 -0.658 -0.711 -0.709 -0.758 5.015
10 0.90 -0.758 -0.802 -0.801 -0.842 4.944
11 1.00 -0.842 -0.879 -0.878 -0.912 4.864
12 1.10 -0.912 -0.942 -0.942 -0.969 4.776
13 1.20 -0.969 -0.994 -0.993 -1.015 4.682
14 1.30 -1.015 -1.035 -1.035 -1.052 4.583
15 1.40 -1.052 -1.067 -1.067 -1.080 4.479
16 1.50 -1.080 -1.091 -1.090 -1.100 4.372
17 1.60 -1.100 -1.107 -1.107 -1.113 4.263
18 1.70 -1.113 -1.118 -1.117 -1.121 4.153
19 1.80 -1.121 -1.122 -1.122 -1.123 4.041
20 1.90 -1.123 -1.122 -1.122 -1.121 3.929
21 2.00 -1.121 -1.118 -1.118 -1.115 3.817
22 2.10 -1.115 -1.110 -1.110 -1.105 3.705
23 2.20 -1.105 -1.099 -1.099 -1.093 3.594
24 2.30 -1.093 -1.086 -1.086 -1.078 3.484
25 2.40 -1.078 -1.070 -1.070 -1.061 3.375
26 2.50 -1.061 -1.052 -1.052 -1.042 3.268
27 2.60 -1.042 -1.032 -1.033 -1.022 3.163
28 2.70 -1.022 -1.012 -1.012 -1.001 3.060
29 2.80 -1.001 -0.990 -0.990 -0.979 2.959
30 2.90 -0.979 -0.967 -0.968 -0.956 2.860
31 3.00 -0.956 -0.944 -0.944 -0.932 2.763

Next, we show the plot of the numerical solution y versus t. As the numerical solution
occurs only at discrete points, the typical convention is to plot the solution with marks
(no line, in this case small circles) as it just occurs at discrete points.

Das könnte Ihnen auch gefallen