Write a Matlab function to locate a root by the secant method. Your function sho
ID: 3849694 • Letter: W
Question
Write a Matlab function to locate a root by the secant method. Your function should be put in the file mysecant.m, and should be called as x = mysecant(f, x0, x1, to1, nmax) Here f is the function f, x0, x1 are the initial guesses, to1 is the error tolerance, nmax is the maximum number of iterations, and x is the output of your function. Test your function to find the value squareroot 2, as the root for f(x) = x^2 - 2, to see if it works. Use sprintf and "disp" to display your result in each iteration, so you can check the convergence. Then, use it to find a root for the function f(x) = e^-x - cos(x) in the interval [1.1, 1.6]. Use to1 = 1e = 12 and nmax = 10. Your two initial guesses should be on the interval [1.1, 1.60]. How does the result compare to those with Newton's method? Comment in detail. What to hand in: Your file mysecant.m, test result for f(x) = x^2 - 2, and the result for f(x) = e^-x - cos(x).Explanation / Answer
Matlab code for Secant Methode:
Create a file mysecant.m and paste given code into it:
function [root] = mysecant(f,x0,x1,tol,nmax)
%f = @(x) (x^4 - 64);
Xi_1 = x1;
Xi = x0;
iter = 1;
Xi_2 = x1;
while(abs(f(Xi_2)) > 0.00001 && nmax > 0)
Xi_2 = Xi_1 - (f(Xi_1) * (Xi_1 - Xi))/(f(Xi_1) - f(Xi));
Xi = Xi_1;
Xi_1 = Xi_2;
iteration = iter
Xi_2
fprintf(' ');
iter = iter + 1;
nmax = nmax - 1;
end
root = Xi_2
end
Sample Output 1:
Sample Output 2:
Note: The code prints the updated value pof root in each iteration.
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.