Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

American University of Sharjah

College of Engineering

NGN 509 – Computational Methods


Professor Nai-Shyong Yeh
Assignment #4

Mostafa Mohamed Moussa - 59665

Submitted on 26th of April 2019


(1)
In m-file, summarized in table in (2)
(2) The Taylor Series approximation used and the formulae for y’, y’’, and y’’’ can all be found in the m-file
HW4_59665.

(3)
%% (3) RK4 Method
function [K1,K2,K3,K4,ytrk] = RK4(t,h,y0,yt,N)
K1=zeros(1,N);
K2=zeros(1,N);
K3=zeros(1,N);
K4=zeros(1,N);
ytrk=zeros(1,N);
for r=1:N
if r==1
K1(r)=h*(2*(t(r)-h)-y0);
K2(r)=h*(2*((t(r)-h)+h/2)-(y0+(K1(r)/2)));
K3(r)=h*(2*((t(r)-h)+h/2)-(y0+(K2(r)/2)));
K4(r)=h*(2*((t(r)-h)+h)-(y0+K3(r)));
ytrk(r)=y0+(1/6)*(K1(r)+2*K2(r)+2*K3(r)+K4(r)); % Use the formulae from slide
25.
else
K1(r)=h*(2*(t(r)-h)-yt(r-1));
K2(r)=h*(2*((t(r)-h)+h/2)-(yt(r-1)+(K1(r)/2)));
K3(r)=h*(2*((t(r)-h)+h/2)-(yt(r-1)+(K2(r)/2)));
K4(r)=h*(2*((t(r)-h)+h)-(yt(r-1)+K3(r)));
ytrk(r)=yt(r-1)+(1/6)*(K1(r)+2*K2(r)+2*K3(r)+K4(r)); % Use the formulae from
slide 25.
end
end
end
(4) In m-file, summarized in table below
(5)
%% (5) AM4 Method
function ytam = AM4(t,N,h,y0,ytrk)
ytam=zeros(1,10);
for i=1:3 % Use RK4 to estimate the first 3 values.
ytam(i)=ytrk(i);
end
% Use AB4 to predict ytAM(4)
ytam(4)= ytrk(3)+(h/24)*(55*(2*t(3)-ytrk(3))-59*(2*t(2)-ytrk(2))+37*(2*t(1)-ytrk(1))-
9*(2*0-y0));
for j=4:N % AM
ytam(j)= ytrk(j-1)+(h/24)*(9*(2*t(j)-ytrk(j))+19*(2*t(j-1)-ytrk(j-1))-5*(2*t(j-2)-
ytrk(j-2))+(2*t(j-3)-ytrk(j-3)));
end
end
(6) In m-file, summarized in table below

Discussion:
Because the problem posed is well-behaved, we do not see much difference in computational speed, but
we can see that AM4 yields a smaller error than RK4 which in turn yields a smaller error than 3 rd Order Taylor
Series method. Though the difference in error is small, you can still see the effect of each method building on
the previous one.

You might also like