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

Write a MATLAB function (IncNR) combining the capabilities of both the increment

ID: 3824129 • Letter: W

Question

Write a MATLAB function (IncNR) combining the capabilities of both the incremental search method and the newton-raphson method to find all the roots in a given function

[xR,err,n] = IncNR(AF,xb,ed)

Inputs:

AF = anonymous function
xb = initial guess x bracket= [xL xU], where xL = lower x and xU = upper x

ed = desired error

Outpus:

xR = vector of roots
err = vector of errors corresponding to the roots

n = vector of the number of iterations

Suggested Steps:

1.   Use the Incremental Search Method to identify the number of roots and the brackets
2.   Use the Newton-Raphson Method in each of the brackets starting at either the lower domain or the upper domain

Explanation / Answer

clc;
clear all;
%% Incremental Method
g = 9.81;
v = 5;
t = 3;
L = 5;
h = 0; %initial guess of height
dh = 0.01; %incremental value
f1 = v - sqrt(2.*g.*h)*tanh((t./(2*L))*sqrt(2.*g.*h)); %initial error
h = h+dh;
f2 = v - sqrt(2.*g.*h)*tanh((t./(2*L))*sqrt(2.*g.*h));
while f1*f2 > 0 % check whether solution exists or not
f1 = v - sqrt(2.*g.*h)*tanh((t./(2*L))*sqrt(2.*g.*h));
h = h+dh;
f2 = v - sqrt(2.*g.*h)*tanh((t./(2*L))*sqrt(2.*g.*h));
end
disp(h); % result height using incremental method
%% Bisection Method
g = 9.81;   
v = 5; % 2nd question part a
t = 3;
L = 5;
syms h
f(h) = v - sqrt(2.*g.*h)*tanh((t./(2*L))*sqrt(2.*g.*h));
a = 0 ; %inital guesses of height
b = 5; %inital guesses of height
if f(a)*f(b)>0
disp('Wrong choice')
else
p = (a + b)/2; %bisection algorithm
err = abs(f(p));
while err > 1e-3
if f(a)*f(p)<0
b = p;
else
a = p;
end
p = (a + b)/2;
err = abs(f(p));
end
end
disp(a) % result height using bisection method
v = 5; % 2nd question part b
t = 3;
L = 10;
syms h
f(h) = v - sqrt(2.*g.*h)*tanh((t./(2*L))*sqrt(2.*g.*h));
a = 0 ; %inital guesses of height
b = 5; %inital guesses of height
if f(a)*f(b)>0
disp('Wrong choice')
else
p = (a + b)/2; %bisection algorithm
err = abs(f(p));
while err > 1e-3
if f(a)*f(p)<0
b = p;
else
a = p;
end
p = (a + b)/2;
err = abs(f(p));
end
end
disp(a) % result height using bisection method
v = 10; % 2nd question part c
t = 2;
L = 10;
syms h
f(h) = v - sqrt(2.*g.*h)*tanh((t./(2*L))*sqrt(2.*g.*h));
a = 0 ; %inital guesses of height
b = 10; %inital guesses of height
if f(a)*f(b)>0
disp('Wrong choice')
else
p = (a + b)/2; %bisection algorithm
err = abs(f(p));
while err > 1e-3
if f(a)*f(p)<0
b = p;
else
a = p;
end
p = (a + b)/2;
err = abs(f(p));
end
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote