0% found this document useful (0 votes)
46 views2 pages

Euler's Forward Method Implementation

This document describes a function called euler_f that implements Euler's forward method to solve initial value problems for ordinary differential equations. The function takes in a function handle, initial values for x and y, the final x value, and three step sizes. It uses a for loop to calculate successive y values using the Euler method over the given x range for each step size. The results are printed out along with errors compared to the exact solution.

Uploaded by

Arif Rahmat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views2 pages

Euler's Forward Method Implementation

This document describes a function called euler_f that implements Euler's forward method to solve initial value problems for ordinary differential equations. The function takes in a function handle, initial values for x and y, the final x value, and three step sizes. It uses a for loop to calculate successive y values using the Euler method over the given x range for each step size. The results are printed out along with errors compared to the exact solution.

Uploaded by

Arif Rahmat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

function [x, y] = euler_f(fcn, x0, y0, xA, h1, h2, h3)

%fcn : fungsi yang di deklarasikan pada script lain


% x0 : nilai x awal
% y0 : nilai y awal
% xA : nilai x akhir
% h1 : nilai h pertama
% h1 : nilai h kedua
% h1 : nilai h ketiga
%
%PENTING !!!!!!!
% || || ||
% \||/ \||/ \||/
% \/ \/ \/
%***********************************************%
for x = 1:xA %%
%-----NPUT FUNGSI EKSAK------------------------%%
B(x) = sin(x) + cos(x);
end %%
%***********************************************%
disp(' A program for Euler Forward''s method ')
disp('By: Arif Rahmat S.R.')
disp('Prodi S1 Matematika, FMIPA, Universitas Riau')
disp('Date: 19 November 2017')
%
% selesaikan masalah nilai awal
% y' = f(x,y), x0 <= x <= b, y(x0)=y0
% membuat program pada script baru.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%*********************************%%
% contoh : **
% function z = eqn(x,y) ##
% z = (y+x^2-2)/(x+1); ##
% end ##
% **
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%===================================
%
% call
% euler_f('eqn', t0, z0, b, h1, h2, h3);
%
% Output :
% Output dari pers. diatas akan menghasilkan 2 buah vektor, x dan y.
%
disp('=====================================================')
fprintf(' h \t\tx \t\ty(x) \t Error \t Rel. Error\n');
display('------------------------------------------------------')
% ITERASI :
for h = [h1 h2 h3]
n = fix((xA-x0)/h)+1;
x = linspace(x0,xA,n)';
y = zeros(n,1);
y(1) = y0;

for i = 2:n
y(i) = y(i-1)+h*feval(fcn, x(i-1), y(i-1));
end
for j = 1:n
if (x(j)==1)
a=y(j);
end
end
c=abs(a-B(1));
fprintf('%3.3f\t %4.0f\t %8.4f\t %8.2s\t %4.4f\n',[h 1 a c abs(c/B(1))])
for r = 2:xA
for j = 1:n
if (x(j)==r)
c=abs(y(j)-B(r));
fprintf('\t %8.0f \t%9.4f \t%9.2s \t%8.4f\n',[x(j) y(j) c abs(c/B(r))]);
end
end

end
disp('______________________________________________________')
end
disp('Thanks| METODE EULER''S FORWARD |SR Ibn. Edwar')
%%=========================================================================%%
%%=========================================================================%%

You might also like