bisection

bisection - % choosing side of the root if fa(i)*fxbar(i)...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
4/12/10 8:19 PM C:\MATLAB701\work\bisection.m 1 of 2 % Artem Iakovlev % UID: 203-833-818 % Problem 5 bisection.m code func=input('What function do you wish to call? ' ,'s'); a(1)=input('What is the value of a?'); b(1)=input('What is the value of b?'); % checking if the entered boundaries are valid if a(1) >= b(1) disp('Boundary b must be greater than boundary a' ); return; end if feval(func,a(1))*feval(func,b(1)) > 0 disp('The boundaries have the same sign'); return; end e=input(' What is the value of the tolerance parameter? ' ); itrmax=input(' What is the max number of allowed iterations? ' ); % initial index and iteration i=1; itr=1; % while loop for the bisection method while ((b(i)-a(i)) >= e) & (itr <= itrmax); xbar(i) = (a(i)+b(i))/2.0; fa(i) = feval(func,a(i)); fxbar(i) = feval(func,xbar(i));
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: % choosing side of the root if fa(i)*fxbar(i) &lt; 0 b(i+1) = xbar(i); a(i+1) = a(i); else a(i+1) = xbar(i); b(i+1) = b(i); end % incrementing index and iteration i=i+1; itr=itr+1; % check if the iterations reached the max allowed if itr &gt;= itrmax disp('Maximum allowed iterations reached' ); return; end end file=strcat(func,'.txt'); output=fopen(file,'w'); % decrementing index i=i-1; fprintf(output,'%2c %10c %10c %13c %12c%c\n' , 'k' , 'a', 'b', 'm', 'fm'); for k=1:i fprintf(output,'%2i %10e %10e %10e %10f\n',k, a(k), b(k), xbar(k), fxbar(k)); end 4/12/10 8:19 PM C:\MATLAB701\work\bisection.m 2 of 2 fprintf(output,'%10s %10e %20s %10e\n', 'The approximate root is: ', xbar(i), ' with fun (m) = ', fxbar(i)); fclose(output); type(file);...
View Full Document

Page1 / 2

bisection - % choosing side of the root if fa(i)*fxbar(i)...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online