In MATLAB language please Write a function function xroot fr
In MATLAB language please
Write a function function [xroot, froot] = secantroot (func, x1, xu) that takes a function func and returns the approximate root x as well as the function value froot at the approximate root; x1 and xu are the lower and upper bound of the secant interval, respectively Secant root formula: X_root X_u-f(x_u) middot (x_1 - x_u)/f(x_1) - f(x_u), f_foot = f(x_root) Test your function with the following input (define w(t), y(x) as anonymous functions in the workspace): (a) y (x) = x^3, x_t = -5, x_u = 5 (b) w(t) = sin (t), t_t = 3, t_u = 5 (c) w(t) = sin(t), t_t = 3, t_u = 4Solution
function [xroot,froot] = secantroot(func,xl,xu)
clc;
%% Termination Condition
maxerr=1e-5;
maxIters=10;
%%%%%%%%%%%%%%%%%%%%%%%%%
numiters=0;
xroot = (xl*func(xu) - xu*func(xl))/(func(xu) - func(xl)); % Root Calculation Formula
froot=func(xroot);
while (abs(froot) > maxerr) % Check Error is less than tolerance
xl = xu;
xu = xroot;
xroot = (xl*func(xu) - xu*func(xl))/(func(xu) - func(xl));
numiters = numiters + 1;
if(numiters == maxIters) % Check num of iteration exceeds or not
break;
end
froot=func(xroot);
end
end
%% Output
>> [xroot,froot]=secantroot(y,-5,5)
xroot =
0
froot =
0
>> [xroot,froot]=secantroot(w,2,5)
xroot =
3.1416
froot =
8.1464e-11
>> [xroot,froot]=secantroot(w,3,4)
xroot =
3.1416
froot =
-7.4395e-08
>>
![In MATLAB language please Write a function function [xroot, froot] = secantroot (func, x1, xu) that takes a function func and returns the approximate root x as In MATLAB language please Write a function function [xroot, froot] = secantroot (func, x1, xu) that takes a function func and returns the approximate root x as](/WebImages/43/in-matlab-language-please-write-a-function-function-xroot-fr-1134779-1761607098-0.webp)
![In MATLAB language please Write a function function [xroot, froot] = secantroot (func, x1, xu) that takes a function func and returns the approximate root x as In MATLAB language please Write a function function [xroot, froot] = secantroot (func, x1, xu) that takes a function func and returns the approximate root x as](/WebImages/43/in-matlab-language-please-write-a-function-function-xroot-fr-1134779-1761607098-1.webp)