Question: 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

 function [x,n] = bisection(fun, a, b, maxtol, maxitr) if nargin if

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 Post-lab problems Problem 4: False position method for root finding a) Modify the bisection method m-file to perform the false-position method and discuss the differences between these two methods. The false-position formula/algorithm is found in the lectures notes or any standard textbook. (Basically just change one line in the bisection code on how to define "x") Rename the root finding user-defined function to FalsePos. Modify the code to return the approximate root, the number of iterations required to perform the calculation, and any other useful information for the user Critical Thinking: The basic logics of the bisection method and the false position method are the same. The only difference is the definition of the variable x, i.e., how to make the bracket smaller. Thus, change one line in the bisection code to get the false position code. The false position method is supposed to accelerate the convergence by reducing the number of iterations to obtain a sufficiently small bracket for convergence since the bisection method is usually very slow in convergence by only halving the bracket in each iteration. Part (c) is an interesting problem to demonstrate a situation when the false position method actually fails to accelerate the convergence. Understand why by using graphical situation would the false position fail to accelerate the convergence method and then conclude under what b) Test the above user-defined function by applying it to find the roots of the polynomial function. Remember to compare results with those obtained by roots f(x)x3-8x2 +5x +14 Critical Thinking: For polynomials, we can quickly determine where all the roots (both real and complex) are. Use these results to guide you to make a plot which shows all the real roots. Then select the proper brackets based on the plot. c) Find the 10-th root of the number 15 using both the bisection method and false position method. Use an initial bracket defined by a 1 and b -2. Print the results of each iteration and compare the convergence of the two methods. Which method converges faster? Look at a graph of the function and comment on why this method is expected to converge faster

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!