Complete the following question using MATLAB: In this assignment you will write
ID: 3666887 • Letter: C
Question
Complete the following question using MATLAB: 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 xExplanation / Answer
%Following are the contents of script file:
function y = secant(str,x(1),x(2),tolerance);
str = 2-2*log(x)-x^0.5
x(1)=input('enter first guess: ');
x(2)=input('enter second guess: ');
tolerance=input('enter tolerance: ');
save(dataFilename,y,'-append'); %appends the root value obtained in y, to the data file
%Convert your script file to a function so that input parameters can then be passed into the function.
function y = secantAlgo(str,x(1),x(2),tolerance) %function for implementing secant algorithm
function y = secant(str,x(1),x(2),tolerance); %Conversion
fh = str2func(str); %converting string to function
iteration=0;
i=2;
eps = 1e6; %For obtaining 6-digit precision.
while (abs(eps) > tolerance) %secant logic
x(i+1) = x(i)- f(x(i)) *(x(i)-x(i-1)) / (f(x(i)) - f(x(i-1)) );
eps = x(i+1)-x(i);
i = i+1;
x(i)
end
return x(i); %returning root
end
%Create a data file with name "dataFilename" and write following contents in it:
The root = %now the 6 decimal root value obtained from above will be appended here.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.