4) Implement the Secant method algorithm asa MATLAB function using the pseudocod
ID: 3751226 • Letter: 4
Question
4) Implement the Secant method algorithm asa MATLAB function using the pseudocode provided below The inputs to the function should be the initial guessesxi and x-1(xi and xim1), the approximation toleranoe (ss), the maximum iterations (imax), and a function handle (f) to hold the equation being investigated. The outputs should be the approximate root location (xr), the number ofiterations performed (iter), and the approximate percent relative error (sa). Confirm your function is working correctly by reproducing your results from the previou s problem. Do not forget to include both your MATLAB function and the required outputs when you print and submit your work. Function Secant(xi, xim1, es, imax, f, xr, iter, ea) iter - 0 DO f(xi)(xim1-xi) f(xim1)-f(xi) xt= X1- iter - iter 1 IF xr # 0 THEN xr x,I * 100 ea = END IF IF ea es 0R iter> imax EXIT. END IF xim1-xi END DO END SecantExplanation / Answer
Save the following code in Secant.m
function [xr, iter, ea] = Secant(xi, xim1, es, imax, f)
iter = 0;
ea = 1;
while ea > es or iter <= imax
xr = xi - (f(xi)*(xim1-xi)) /(f(xim1) - f(xi));
iter = iter + 1;
if xr ~= 0
ea = abs((xr-xi)/xr) * 100
end
xim1 = xi;
xi = xr;
end
end
========================
You can now test it using the following lines in command window
f = @(x) 3*x+4
[xr, iter, ea] = Secant(-2, -1, 1e-6, 100, f)
output
-----
f =
@(x) 3 * x + 4
xr = -1.3333
iter = 2
ea = 0
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.