4 Write a function file for the secant method to find the ro
4. Write a function file for the secant method to find the roots of a function. Test your code for the following function. Your inputs should be the function, two initial guesses, and a tolerance. (x) x 5. Write a function file for the bisection method to find the roots of a function. Test your code on the following function. Determine what your inputs and outputs should be. (x) x
Solution
%Matlab code here save as function
function y = secant(a,b,tol)
f=inline(\'cos(x)-x\');
c = (a*f(b) - b*f(a))/(f(b) - f(a));
iter=0;
while abs(f(c)) > tol
a = b;
b = c;
c = (a*f(b) - b*f(a))/(f(b) - f(a));
iter = iter + 1;
if(iter == 1000)
break;
end
end
display([\'Root is x = \' num2str(c)]);
y = c;
------------------------------------------------
Run from command window like this
>> secant (0,pi/2,0.001)
Root is x = 0.73957
ans =
0.7396
--------------------------------------
Only 1 question per post will be answered please
