Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

create an m-file named RootFinder_P1.m that contains a function named RootFinder

ID: 2073826 • Letter: C

Question

create an m-file named RootFinder_P1.m that contains a function named RootFinder_P1. This function calculates the friction factor f using the bisection, Newton-Raphson, and secant methods. The m-file starts with the following line of code:

function [fBis, iterBis, fNR, iterNR, fSec, iterSec] = RootFinder_P1(y, dy, fa, fb, f0, f1, f2, eps);

The input arguments are the anonymous function y, the derivative of the anonymous function function dy, the bounds for the bisection method fa and fb, the initial guess for the Newton-Raphson method f0, the first two guesses for the secant method f1 and f2, and tolerance eps. The output arguments are the value of the root using the bisection method (fBis), the Newton-Raphson method (fNR), and the secant method (fSec). Additionally, you will output the number of iterations required by each method (iterBis, iterNR, and iterSec).

For all three methods, the termination criterion is: | (fi+1 – fi ) / fi+1 | < eps

Once the condition is met, the value of fi+1 is the root.

Important:

The Newton-Raphson and secant methods are not bracketed and it is possible that you will never converge to a solution. Your code should give the user an error message if the method does not converge after 100 iterations.

Part 1: Background The loss of mechanical energy in straight pipes due to friction is represented by the major head loss term ,major where f is the dimensionless friction factor, L is the pipe length, D is the pipe diameter, Vis the average fluid speed through the pipe, and g is the gravitational acceleration There are two important dimensionless parameters that impact the value of f ·The Reynolds number, Re , ·The relative roughness, e/D, where is the absolute roughness of the pipe wall where v is the fluid's kinematic viscosity For laminar flows (Re

Explanation / Answer

Ans:

Here I write all function for the Bisection, Fixed-Point, Newton-Raphson.

(1). Bisection method

function x=bisection(f,a,b,tol)

sfb = sign(f(b));

width = b-a;

disp(' a b sfx')

while width > tol

width = width/2;

x = a + width;

sfx = sign(f(x));

disp(sprintf('%0.8f %0.8f %2.0f', [a b sfx]))

if sfx == 0, a = x; b = x; return else

if sfx == sfb, b = x; else, a = x; end

end

(2). Newton - Raphson

function x=newtonraphson(f,df,x,nk)

disp('k x_k f(x_k) f''(x_k) dx')

for k = 0:nk

dx = df(x) (x);

disp(sprintf('%d %0.12f %9.2e %1.5f %15.12f',[k,x,f(x),df(x),dx]))

x = x - dx;

end

(3). Secant method

function xx = secant(f,xx,nk)

disp('k x_k f(x_k)')

ff = [f(xx(1)), f(xx(2))];

h = 10*sqrt(eps);

for k = 0:nk

disp(sprintf('%d %17.14f %14.5e',... [k,xx(1),ff(1)]))

if abs(diff(xx)) > h df = diff(ff)/diff(xx);

else

df = (f(xx(2)+h)-ff(2))/h;

end

xx = [xx(2), xx(2)-ff(2)/df]; % update xx

ff = [ff(2), f(xx(2))]; % update ff

end