%%Euler's Method and ode45 for MA311 at Colby College
%%
%%This file is relavant to Section 2.6 in the MA311 Course
%%Notes. As written, this m-file runs Euler's method to solve the initial
%%value problem
%%{y'=y, y(0)=1
%%on the interval I=[0,1].

N=10;                      %%Value of N, adjust as you desire
t0=0;                       %%Left endpoint of I=[0,1]
T=1;                        %%Right endpoint of I=[0,1]
hN=(T-t0)/N ;               %%Time Step
t=zeros(1,N);               %%Initializes time vector
y=zeros(1,N);               %%Initializes vector of approximants
t(1)=t0;                     %%Initial t. Note: Matlab cannot index from 0
y(1)=1;                      %%Initial y
for k=1:1:N                 %%For-loop implementing Euler's method
    t(k+1)=t(k)+hN;         %%initially set up for y'=y
    y(k+1)=y(k)+y(k)*hN;
end

plot(t,y,'r-*')              %%Plots the approximants from Euler's method
hold on

tspan = [0 1];
y0 = 1;
[t,y] = ode45(@(t,y) y, tspan, y0); %%Matlab's numerical solver, "ode45"
plot(t,y,'g-o')              %%Plots results from "ode45"
hold off

