Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment you will write a script and a function to implement the secan

ID: 3664358 • Letter: I

Question

In this assignment you will write a script and a function to implement the secant algorithm. The function will implement the secant algorithm and return the root of the function. The script will define the required data and send the data to the function. Included in the required data there should be a string that defines the required function as an anonymous function. The string can be converted into a function handle using MATLAB's str2func function. After the root is returned from the secant function is should be written to a data file on disk with 6 decimal places. The data file should read. The root = (value of root) For your function use f(x) = 2-2 log(x) - square root x

Explanation / Answer

function y = secant()
%reading function
a=input('Enter function f(x):','s');
f=inline(a)
%reading initial guess
x(1)=input('enter intial guess: ');
x(2)=input('enter next guess: ');
%Reading tolerance
tolerance=input('enter tolerance: ');
i=2;
epsilon = 1e10;
c=0
%until our tolerance met.. calculating root value
while (abs(epsilon) > tolerance)
x(i+1)=x(i)-f(x(i))*(x(i)-x(i-1))/(f(x(i))-f(x(i-1)));
c = x(i+1)
epsilon = x(i+1)-x(i);
i = i+1;
x(i)
end
%printing results
display(['Value of Root = ' num2str(c)]);
end

%calling function
secant()