function [y_approx,Err,w,t,m]=mid_pt(f,a,b,y_in,y_ex,N,plott,error) %------------------------------------------------ %function mid_pt: % % INPUT: a the end point % b the other end point % N max number of steps % TOL tolerance % y_in initial value at y(a) % y_ex exact value at y(b) % plott if 1 is entered, the program will plot % error if 1 is entered, the program will calculate error based on y_ex % f the input functions % % OUTPUT: y_ap approximate answer for y(b) % w computed solution % Err error between the exact value of % our calculated value % m number of equations % t time array % % % example: fmid_pt('f',0,2,[1 0 0],[25 25 25],1000,1,1); where f is f.m %------------------------------------------------- t(1)=a; w(1,:)=y_in; % set w(0) = the initial value m=length(y_in); % set the number of equations h=(b-a)/N; % set h for i=1:N x = t(i)+h/2; y = w(i,:)+(h/2)*feval(f,t(i),w(i,:)); w(i+1,:) = w(i,:)+h*feval(f,x,y); t(i+1) = t(i)+h; end y_approx=w(N+1,:); Err=abs(y_ex - y_approx); if plott==1 figure hold for j=1:m plot(t,w(:,j)) end; end; if error==1 % output stuff... fprintf('\n'); disp('mid - point method'); disp('------------------------------'); disp(sprintf('exact Val:\t%f',y_ex)); disp(sprintf('Cal. Val:\t%f',y_approx)); disp(sprintf('Error: \t%d',Err)); disp(sprintf('Step: \t%d',N)); fprintf('\n'); end