% Uses Euler's method to compute numerical solution to the ode % y'=f(t,y) y(a)=alpha a<=t<=b. % Asks user to input a,b,step size h and initial value alpha % Assumes f is evaluated in a separate function file f.m % Plots the result with discrete points. a=input('Please enter the left endpoint:'); b=input('Please enter the right endpoint:'); h=input('Please enter the step size'); n=ceil((b-a)/h); alpha=input('Please enter the initial value:'); clear t w t=a:h:(a+n*h); w(1)=alpha; for i=1:n w(i+1)=w(i)+h*f(t(i),w(i)); end hold off plot(t,w,'*'); %For the example y'=y^2-t^2+1 y(0)=0.5 % here we now plot the true solution alongside the % approximation. %hold on %tvals=a:0.01:b; %plot(tvals,1+tvals.^2+2*tvals-0.5*exp(tvals)); %plot(tvals,exp(-50*tvals));