% This is a Matlab script that uses the Backward Euler Method to % solve the first order differential equation y'=f(t,y), % and plots the solution from TO to TF. The functions % to be evaluated are given in a separate function m-file, % called feuler(t,y). The first part of this file solves % the equation numerically. The second part plots the % direction fields. hold off clear all T0=0; Y0=1; TF=1; NK=100; DELTAT=(TF-T0)/NK; T=T0;Y=Y0; tk=T0;yk=Y0; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Backward Euler Method (nonlinear solve via secant method) % %%%%%%% % % define tolerance, max iteration and initial guess for secant method % sectol=DELTAT*10^(-2); itmax=20; y1=Y0; y2=Y0+DELTAT; % a somewhat uneducated guess for y2 ... % % execute the integration loop % for k=1:NK; tk=tk+DELTAT; yold=yk; f1=y1-yold-DELTAT*feuler(tk,y1); f2=y2-yold-DELTAT*feuler(tk,y2); % % set or reset parameters for secant method solve % dy=1; i=1; % % implement a Secant Method to solve y(i+1)-y(i)-h*f(t(i+1),y(i+1))=0; % while (dy>sectol & i