% Turn off the echo in case it was on (otherwise % all statements in the for-loop will be repeated % over and over again). % Clear all variables. Clear the figure. % echo off clear all clf % % Define the initial data and other parameters % t0=0, y0=1 tfin=10 N=1000 % % Calculate the step size % dt=(tfin-t0)/N; % % Reserve contiguous memory space for the data with % values initialized to zero, except the first ones % t=zeros(N+1,1); y=zeros(N+1,1); t(1)=t0; y(1)=y0; % % Euler's method implemented as a SLOW for-loop % Let us keep track of the computing time % starttime=cputime % for i=1:N t(i+1)=t(i)+dt; y(i+1)=y(i)+t(i)*sin(y(i)); end % % Calculate and display elapsed time % now=cputime mytime=now-starttime % % Plot the solutions and siplay how long the plotting takes. % plot(t,y,'b-') plottime=cputime-now