You are on page 1of 8

Third year

Mechanical power engineering department


Computer applications report:
Butcher’s fifth order technique
prof: Elshafei Mahmoud Zidan

Name: Mahmoud Mohamed Mohamed Samaha


Section: 8
Algorithm for Butcher’s fifth order Method:

(a) Main or “Driver” Program (b) Routine to Take One Output Step
SUB Integrator (x, y, h, xend)
Assign values for
DO
y = initial value dependent variable
IF (xend – x<h) THEN h=xend - x
xi = initial value independent variable
CALL RK5 (x, y, h, ynew)
xf = final value independent variable
y = ynew
dx = calculation step size
IF (x ≥ xend) EXIT
x out= output interval
END DO
END SUB
x = xi
m=0 (c) RK5 Method for a Single ODE
xpm = x SUB RK5 (x, y, h, ynew)
ypm = y CALL Derivs (x, y, k1)
DO ym= y + k1. h/4
xend = x + xout CALL Derivs (x +h/4, ym, k2)
IF (xend > xf) THEN xend = xf ym= y +k2 .h/8+k1.h/8
h = dx CALL Derivs (x +h/4, ym, k3)
CALL Integrator (x, y, h, xend) ym=y - .5.k2.h+k3.h
m=m+1 CALL Derivs (x +h/2, ym,k4)
xpm = x ym=y+(3/16).k1.h+(9/16).k4.h
ypm = y CALL Derivs (x +(3/4).h, ym,k5)
IF (x ≥ xf) EXIT ym=y-(3/7).k1.h+(2/7)k2.h+(12/7)k3.h-(12/7)k4.h+(8/7)k5.h
END DO CALL Derivs (x +h, ym, k6)
DISPLAY RESULTS slope=(1/90).(7k1+32k3+12k4+32k5+7k6)
END ynew=slope . h
END SUB
(d) Routine to Determine Derivative
SUB Derivs (x, y, dydx)
dydx= ...
END SUB
Butcher’s fifth order technique code:

function[t,y]=rungek5th(f,y0,t0,h,tf)
%to make a vector for t
t=t0:h:tf; t=t';
%to make sure that the last value of t=tf
if t(end)< tf
t(end+1)=tf;
end
n=length(t);
%to make an empty vector for y
y=zeros(n,1); y(1)=y0;
for i=1:n-1
h=t(i+1)-t(i);
k1=f(t(i),y(i));
k2=f(t(i)+.25*h,y(i)+.25*h*k1);
k3=f(t(i)+.25*h,y(i)+(1/8)*h*k1+(1/8)*k2*h);
k4=f(t(i)+.5*h,y(i)+h*k3-.5*k2*h);
k5=f(t(i)+.75*h,y(i)+(3/16)*h*k1+(9/16)*k4*h);
k6=f(t(i)+h,y(i)-(3/7)*h*k1+(2/7)*k2*h+(12/7)*k3*h-(12/7)*k4*h+(8/7)*k5*h);
slope=(1/90)*(7*k1+32*k3+12*k4+32*k5+7*k6);
y(i+1)=y(i)+slope*h;
%note:beyond fourth order methods the gain in accuracy is offset by the added
computational effort and complexity.
end
%the exact solution of the example that we will use to see the accuracy of this methode
tex=0:h:2; tex=tex';
yex=(exp(.25.*tex.^4-1.5.*tex));
%to calculate error and relative error
error=yex-y;
relative = abs((error./yex).*100);
%to display solution
plot(tex,yex,'k',tex,y,'-*r')
legend('exact','RK5')
display(' t yex y error %relative err');
disp([t,yex,y,error,relative])
end

note: Butcher’s fifth order technique requires six function evaluations (k1, k2, k3, k4, k5, k6), beyond
fourth order methods the gain in accuracy is offset by the added computational effort and complexity.
that is another way to write the code but here we ask user to enter the inputs by its names to be
easy for him to understand what he must enter.

%another way to write the code but here we ask user to enter the inputs by
%its names to be easy for him to understand what he should enter
f=input('enter your function in the form (@(t,y) t*2+3+y^5):');%right hand side of ODE
y0=input('enter the initial value of dependent variable :');
t0=input('enter the initial value of independent variable :');
h=input('enter step size (t(i+1)-t(i)):');
tf=input('enter the point at which you want to evaluate the solution :');
%to make a vector for t
t=t0:h:tf; t=t';
%to make sure that the last value of t=tf
if t(end)< tf
t(end+1)=tf;
end
n=length(t);
%to make an empty vector for y
y=zeros(n,1); y(1)=y0;
for i=1:n-1
h=t(i+1)-t(i);
k1=f(t(i),y(i));
k2=f(t(i)+.25*h,y(i)+.25*h*k1);
k3=f(t(i)+.25*h,y(i)+(1/8)*h*k1+(1/8)*k2*h);
k4=f(t(i)+.5*h,y(i)+h*k3-.5*k2*h);
k5=f(t(i)+.75*h,y(i)+(3/16)*h*k1+(9/16)*k4*h);
k6=f(t(i)+h,y(i)-(3/7)*h*k1+(2/7)*k2*h+(12/7)*k3*h-(12/7)*k4*h+(8/7)*k5*h);
y(i+1)=y(i)+(1/90)*(7*k1+32*k3+12*k4+32*k5+7*k6)*h;
end
%the exact solution of the example that we will use to see the accuracy of this methode
tex=0:h:2; tex=tex';
yex=(exp(.25.*tex.^4-1.5.*tex));
%to calculate error and relative error
error=yex-y;
relative = abs((error./yex).*100);
%to display solution
plot(tex,yex,'k',tex,y,'-*r',tex,relative,'-*b')
legend('exact','RK5','relative_err')
display(' t yex y error %relative err');
disp([t,yex,y,error,relative])
Hint: the example we used to compare accuracy of different methods with accuracy of Runge Kutta 5 th
order is problem 25.1 in chapra
Problem25.1
‘’Solve the following initial value problem over the interval from t = 0 to 2 where y(0) = 1. Display all your
𝑡4
𝑑𝑦 − 1.5𝑡
results on the same graph 𝑑𝑡 3
= 𝑦𝑡 − 1.5𝑦 ’’ Exact solution for this problem is : 𝑦=𝑒 4

The results of Fourth-Order Runge-Kutta Method and Butcher’s fifth order technique respectively are
shown in the figures below for step size equals 0.5:

figure (1)

figure (2)

As we see Fourth-Order Runge-Kutta Method couldn’t capture the relatively large changes from t= ]1.5:2[
and that is clear in the large value of relative error in that region. So, we tried to find a solution for this
problem instead of using Butcher’s fifth order technique.
To capture these large changes, we tried to reduce the step size to 0.25 instead of 0.5 (with Fourth-Order
Runge-Kutta Method) and we got the results shown below:

If you compared the results shown above with the results in fig (1), you will note that although we have
got a more accurate solution with about the same relative error that we got with Butcher’s fifth order
technique, the computational cost became more even more than Butcher’s fifth order technique. So, in
that problem we may prefer to use Butcher’s fifth order technique.

comparing IVP solving methods


3
exact
RK5
RK4
2.5
EULER
MIDPOINT
RK2
2
dependent variable y

1.5

0.5

0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
independent variable t

As we can see Butcher’s fifth order technique is the most accurate method and Euler is the least
accurate method. The accuracy of Fourth-Order Runge-Kutta Method is acceptable but it
couldn’t capture the large changes from t= ]1.5:2[.
in the region where large changes occur Butcher’s fifth order technique do better than Fourth-
Order Runge-Kutta Method, on the other hand We can see that in the region where there is no
discontinuities Fourth-Order Runge-Kutta Method provides an acceptable accuracy with a lower
computational cost.
Problem 2:

’Solve the following initial value problem over the interval from t = 0 to 3 where y (0) = 2 , h=.5 . Display
all your results on the same graph 𝑦 ′ = 0.5𝑒 0.8𝑡 − 0.5𝑦 ‘
4
Exact solution for this problem is : 𝑦= (𝑒 0.8𝑡 − 𝑒 −.05𝑡 ) + 2𝑒 −0.5𝑡
1.3

comparing IVP solving methods


35
exact
RK5
30
RK4
EULER
25 MIDPOINT
RK2
dependent variable y

20

15

10

0
0 0.5 1 1.5 2 2.5 3
independent variable t

As we can see in such stiff problem, even Butcher’s fifth order technique cannot give an acceptable
accuracy even if we reduced the step size. that’s because the omitted terms of Taylor expansion in this
problem have large values so, the truncation error in this problem is very large. that’s because the
derivative of an exponential function of x is the exponential function itself and then the higher derivative
terms that we have omitted from Taylor expansion still have large values and that large values can’t be
neglected as we have done in the derivation of Butcher’s fifth order technique. Even if we decreased the
step size that large values of the higher derivatives still have a great effect. To get an accurate solution for
such stiff problem one can use multistep methods.
REFERENCES:
• Numerical Methods for Engineers, 6th Edition by Steven Chapra
• Applied-Numerical Methods by Steven Chapra

You might also like