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

Please help with with problem in MATLAB coding only, thank you function

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 Problem 5: Engineering Application A trough of length L has a cross section in the shape of a semicircle with radius r, as shown. When filled with water to within a distance h of the top, the volume V of the water is Suppose L 10 ft, r-1 ft, and 12.4 ft3. Find the depth of water in the trough to within 0.01 ft accuracy using the bisection method and the false position method. Is there any advantage to apply the false position method over the bisection method for this problem? Compare the results and explain. This is a problem to practise good programming skill, as discussed in class. Define the variables clearly and establish the equation to be solved in terms of the variables. % solve the depth in a trough >> L = 10; % length in [ft] >> r 1; % radius in [ft] >>V= 12.4; % volume in [ft^3] >>f-e (h) L(0.5 pi r2-r2 asin (h/r)-h. sqrt (r 2-h.*2))-Vi Critical thinking: Remember that the fundamental root-finding problem is to solve for the solutions of the equation f(x)-0. Hence, first formulate the current problem into this mathematical form and then call the bisection and the false position codes.Think about this: the purpose of the false position method is to accelerate the convergence by making the brackets that contain the solution (root) smaller in a fewer number of iterations. Run the bisection and FalsePos codes and compare the results in terms of which method would require fewer number of iterations using the same convergence criterion. In general, the false position method will accelerate the convergence except the case when the slope around the solution is very steep. Plot this function to observe its behaviour

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!