%This program is set up to use Euler's Method to approximate the solution
%to a first order ODE and graph the approximation.  
clear
a=0; %replace 0 with the inital independent variable value x0
b=1; %replace 3 with the final value you want to compute a solution for
y0=1; %replace 1 with the inital dependent variable value x0
n=10; % replace 10 with the number of steps you want to use.
f=@(x,y) y; %replace the final y with f(x,y) from your DE y'=f(x,y)
xn=zeros(1,n);
x2n=zeros(1,2*n);
yn=zeros(1,n);
y2n=zeros(1,2*n);
xn(1)=a;
yn(1)=y0; 
x2n(1)=a;
y2n(1)=y0; 
h=(b-a)/n;
j=1;
for i = 1:n
    xn(i+1)=xn(i)+h; %if you delete the ; then the values will be in the CW
    yn(i+1)=yn(i)+h*f(xn(i),yn(i)); %ditto
    x2n(j+1)=x2n(j)+h/2; %if you delete the ; then the values will be in the CW
    y2n(j+1)=y2n(j)+h*f(x2n(j),y2n(j))/2; %ditto
    j=j+1;
    x2n(j+1)=x2n(j)+h/2; %if you delete the ; then the values will be in the CW
    y2n(j+1)=y2n(j)+h*f(x2n(j),y2n(j))/2; %ditto
    j=j+1;
  end
%If you know the real solution replace exp(xn) with it, where xn is the
%variable. Otherwise put a percent sign in front of the first plot and take
%it off the second one.
plot(xn,yn,'red',x2n,y2n,'green', x2n,exp(x2n),'blue')
%plot(xn,yn,'red',x2n,y2n,'green')
    