Please help me doing this problem only in MATLAB coding by part by part. Thank you and please use only Matlab.
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 6: Engineering Application-Colebrook Equation (ME3300 Fluid Mechanics) Background: The flow of fluids through pipes and tubes is important in many fields of engineering. The resistance to this flow is parameterized by a dimensionless number called the Darcy friction factor f. The Colebrook equation allows engineers to calculate the friction factor f: 7-2 log-GT 257) where D is the hydraulic diameter, Re is the dimensionless Reynolds number, g is a roughness parameter in meter, and fis the Darcy friction factor. The Reynolds number is defined as the ratio of the inertia momentum forces to the viscous forces by the following equation: pVD VD where, p is the fluid density, u is the dynamic viscosity, and v is the kinematic viscosity. The fluid viscosity characterizes its resistance in deformations under applications of forces. Laminar and turbulent flows are generally characterized by low and high Reynolds number, respectively The Colebrook equation is generally valid for pipes full of fluid at Re > 4000 Problem Statement Based on the Colebrook equation, the friction factor needs to be solved numerically. Use the following values for computations, which are based on the flow of water: p 1,000 kg/m3,u - 8.9 x 10-4 N-s/m2, D 0.01 m,V - 5 m/s,e2.5 x 10-6 m For the function whose roots are to be solved, develop an anonymous or user-defined function of one variable f. Using the programming approach taught in this class, we suggest to first type in the values of the parameters , D, , and E, and then the function in terms of those parameters. This approach allows easy debugging, should typo errors occur a) b) Use the fplot command to create a graph of the friction factor. We suggest a range of 0.005s fs0.1. Properly label your graph, using either commands or graph GUI tools c) Apply the bisection code to numerically find the solution of f. Use the results of part (b) to determine your initial bracket limits a and b. Use an error tolerance of 0.001 and a maximum of 30 iterations. Also, print out the results of the iteration number k, a, f (a), b, f (b), x, and approx_error for each iteration. d) Repeat part (c) by applying the FalsePos code. What can you conclude in terms of the computational efficiency of these two numerical methods? Problem 6: Engineering Application-Colebrook Equation (ME3300 Fluid Mechanics) Background: The flow of fluids through pipes and tubes is important in many fields of engineering. The resistance to this flow is parameterized by a dimensionless number called the Darcy friction factor f. The Colebrook equation allows engineers to calculate the friction factor f: 7-2 log-GT 257) where D is the hydraulic diameter, Re is the dimensionless Reynolds number, g is a roughness parameter in meter, and fis the Darcy friction factor. The Reynolds number is defined as the ratio of the inertia momentum forces to the viscous forces by the following equation: pVD VD where, p is the fluid density, u is the dynamic viscosity, and v is the kinematic viscosity. The fluid viscosity characterizes its resistance in deformations under applications of forces. Laminar and turbulent flows are generally characterized by low and high Reynolds number, respectively The Colebrook equation is generally valid for pipes full of fluid at Re > 4000 Problem Statement Based on the Colebrook equation, the friction factor needs to be solved numerically. Use the following values for computations, which are based on the flow of water: p 1,000 kg/m3,u - 8.9 x 10-4 N-s/m2, D 0.01 m,V - 5 m/s,e2.5 x 10-6 m For the function whose roots are to be solved, develop an anonymous or user-defined function of one variable f. Using the programming approach taught in this class, we suggest to first type in the values of the parameters , D, , and E, and then the function in terms of those parameters. This approach allows easy debugging, should typo errors occur a) b) Use the fplot command to create a graph of the friction factor. We suggest a range of 0.005s fs0.1. Properly label your graph, using either commands or graph GUI tools c) Apply the bisection code to numerically find the solution of f. Use the results of part (b) to determine your initial bracket limits a and b. Use an error tolerance of 0.001 and a maximum of 30 iterations. Also, print out the results of the iteration number k, a, f (a), b, f (b), x, and approx_error for each iteration. d) Repeat part (c) by applying the FalsePos code. What can you conclude in terms of the computational efficiency of these two numerical methods