Please solve the following in MATLAB Please add the comments
Please solve the following in MATLAB. Please add the comments in each line and provide the coding window and the command window. Try to make the codes as universal as possible. Write a function file for the secant method to find the squareroot s of a function. Test your code for the following function. Your inputs should be the function, two initial guesses, and a tolerance. Cos(x) = x
Solution
Program:
f=@(x) cos(x);
a=0;
b=1;
bisection(f,a,b)
Function Program:
function bisection(f,a,b)
for i=1:100
c=(a+b)/2;
if f(c)>0
b=c;
else a=c;
end
end
a=1; b=2; p=c;
for i=1:100
c=(a+b)/2;
er(i)=f(c)-f(p);
if f(c)>0
b=c;
else a=c;
end
end
fprintf(\'Root of given equation is %f\',c)
plot(er);
title(\'Plot of error\')
xlabel(\'Number of iterations\')
ylabel(\'Error\')
grid on;
end
