Euler's Method (Explicit) to Solve Numeric Intregration

Euler's Method (Explicit)

clc;
clear all;
h=input('h=');
k=input('x=');
F = @(x,y) (x+y);
Y = @(x) (2*exp(x)-x-1);
x(1)=0;
y(1)=1;
if rem(k,h)==0
for n=1:100000
    y(n+1)=y(n)+h*F(x(n),y(n));
    x(n+1)=x(n)+h;
    if (x(n+1)>k)
        break;
    end
end
fprintf('Numerical solution y=%0.5f',y(n+1));
fprintf('\nExact solution y=%0.5f',Y(k));
fprintf('\nError=%0.5f',abs(Y(k)-y(n+1)));
else
    fprintf('Incorrect interval');
    end

Comments