Euler

%Euler
%Author: Minster
clear all
close all
h = 0.1;
x = 0:h:2;
y=0:h:2;
ye(1) = 1/2;
%yt =0.5*exp(2*x+1)+0.5;
yt=1/(x^2+y^2)^(0.5);
disp(‘ error ‘)
disp(‘ ====================================================’)
for i = 2:length(x)
% Replace the expression in parentheses with f(x(i-1),ye(i-1))
% Currently f(x,y) = 2y-1
ye(i) = ye(i-1)+h*(2*ye(i-1)-1);

end
%for i=2:length(x)
error=abs(ye-yt);
s=0:0.1:2;
disp(fprintf(‘ %20.12e \n’, error))
%end

% The vector ye now contains our computed solution
% Put the true solution in a vector yt
% Plot the solutions and the error;
figure;
plot(x,ye,’b-‘,x,yt,’g:’);
xlabel(‘x’);ylabel(‘y(x)’);
title(‘Euler”s and true solutions’);
legend(‘Euler”s method’,’True solution’);

figure;
plot(x,abs(ye-yt));
xlabel(‘x’);ylabel(‘E(x)’);
title(‘Error of Euler”s Method’);
legend([‘Final error = ‘ num2str(abs(ye(end)-yt(end)))]);