1. Engineering
  2. Computer Science
  3. please help with with problem in matlab coding only thank...

Question: please help with with problem in matlab coding only thank...

Question details

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

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

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

if nargin<5, maxitr=50; end
if nargin<4, maxtol=1e-4; end
x = a;   
k = 0;
funa = fun(a); funb = fun(b); funx = fun(x);
er = abs(funx);

if abs(funa) < eps
fprintf('Left bracket point is the root = %10.4e ',a), return
if abs(funb) < eps
fprintf('Right bracket point is the root = %10.4e ',b), return

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.'])

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 < maxitr
k = k + 1;
x = (a + b)/2;
funx = fun(x);
if abs(funx) < eps
fprintf(' In this iteration, the midpoint is found to be a root = %10.4e ',x), break
if fun(a)*funx < 0, b = x;
else a = x;
er = abs(funx);
fprintf(' %i %f %f %f %f %f %f %f ',k,a,fun(a),b,fun(b),x,funx)
fprintf(' ')
n = 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');



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)
% 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

% 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)

root=[]; func_val=[]; err=[]; numIter=[]; % Output set to empty matricesinitially

if(nargin)==3 % check if last two arguments have been passed or not
err_max=0.0001; % assign the default values
elseif (nargin)==4 % check if last argument have been passed or not
iter_max=50; % assign the default values
elseif (nargin)<3 || nargin>5 % if less than three or more than five arguments have been passed, raise an error and return
warning('Insufficient arguments passed!');

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!');
elseif (lb>ub) % check if x_min<x_max i.e. intervals ae not in same order,raise and error return
disp('Probelm with interval size. Try again!');


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<iter_max % loop will continue as long as this criteria holds true
n=n+1; % iteration number
if func(lb)*func(c)<0 % select the interval of root
ub = c;
else % select the interval of root
lb = c;
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);
% Uncomment the following line to see result in each iteration
% fprintf('%d %3.6f %3.4f %3.4f %3.8f ',n,lb,ub,c,err); % display the iteration result

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution