function [x,y]=EulerTest(xstart,ystart,xend,n) %This test function uses Euler's method to solve the ODE %dy/dx = - y ^2 from xstart to xend . hstep=(xend-xstart)/(n-1); x=zeros(n,1); for j=1:n x(j)=xstart+(j-1)*hstep; end y=zeros(n,1); y(1)=ystart; for j=1:n-1 y(j+1)=y(j)-hstep*(y(j))^2; end %Plot the exact solution y = ystart/(1+ystart*(x - xstart)) . xexact=[xstart:0.02:xend]'; yexact=ystart./(1+ystart*(xexact-xstart)); plot(xexact,yexact,'r') hold on %Plot the approximate solution in blue spots. plot(x,y,'bo') hold off