Question
Please help with with problem in MATLAB coding only, thank you function [x,n] = bisection(fun, a, b, maxtol, maxitr) if nargin if abs(funa) if funa*funb
Please help with with problem in MATLAB coding only, thank you
function [x,n] = bisection(fun, a, b, maxtol, maxitr)
if nargin
if abs(funa)
if funa*funb > 0 error(['The function values at the bracket endpoints must differ in sign. ', ... 'This means that there is either no root or possibly an even number ', ... 'of roots in the initial bracket.']) end
fprintf(' Iter# a f(a) b f(b) x f(x) '); fprintf('%i %f %f %f %f %f ',k,a,fun(a),b,fun(b))
%% Execute the bisection algorithm while er >= maxtol && k
%% Check results for no convergence if n == maxitr && er >= maxtol fprintf(' ') warning('Maximum number of iterations reached before convergence') fprintf(' Solution not obtained in %d iterations. ',maxitr); x = ('No answer'); n = ('No answer'); end
end
falsepos.
function [root, err, numIter, exitFlag]=falsepos(func,lb,ub,err_max,iter_max)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This functions find the the root of a function by repeatedly bisecting an interval and then selecting % a subinterval in which a root must lie for further processing.
% function [root,err,numIter,exitflag] = falsepos(fun,lb,ub,err_max,iter_max) % INPUT: % func: an input function % lb,ub: interval limits % err_max: maximum interval size in which final root should lie % iter_max: maximum number of iterations allowed
% OUTPUT: % root : the final root of the function func % err: final interval size of the interval in which root lies % numIter: number of iterations it took to obtain the root % exitFlag: status if return (>1 for normal return and -1 if error) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % INPUT TESTING AND DEFAULT VALUES
root=[]; func_val=[]; err=[]; numIter=[]; % Output set to empty matricesinitially exitFlag=1;
if(nargin)==3 % check if last two arguments have been passed or not err_max=0.0001; % assign the default values iter_max=50; elseif (nargin)==4 % check if last argument have been passed or not iter_max=50; % assign the default values elseif (nargin)5 % if less than three or more than five arguments have been passed, raise an error and return warning('Insufficient arguments passed!'); return; end
if func(lb)*func(ub)>0 % check if the function value at initial points are not same sign, raise and error return disp('Probelm with interval signs. Try again!'); exitFlag=-1; return; elseif (lb>ub) % check if x_min %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % MAIN BODY OF FUNCTION: n=0; % number of iteartions computed yet func_x_max=func(ub); func_x_min=func(lb); c=lb-((ub-lb)/(func_x_max-func_x_min))*func_x_min; % find the next guess point err = abs(ub-lb); % take initial error estimate % Uncomment the following line and line 72 to see result in each iteration % fprintf('Iteration x_min x_max Root Error estimated '); while err > err_max && n
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started