I made the code for euler's method in matlab and now I have to plot the approximation and the exact result. The problem is that I don't know how to introduce the analytical solution and plot it. I made this but it doesn't work.
function [t,w] = euler (f,y0,a,b,h) %func=f(x,y)=dy/dx
%a and b the interval ends
%h=distance between partitions
%y0= initial value
s=input('Give an equation in x: ','s'); %the solution of the ODE
n=(b-a)/h;
t=zeros(1,n+1);
w=zeros(1,n+1);
t(1)=a;
w(1)=y0;
for i=1:n
w(i+1)=w(i)+h*f(t(i),w(i));
t(i+1)=a+h*i;
end
w
x=a:h:b;
y=s;
plot(x,y,'k')
hold on
plot(t,w,'r')
hold off
endfunction
The analytical solution is y=-x-1+2*exp(x) and the ODE is y'=x+y.