matlab. the root=( value_of_root) In this assignments you will write a script an
ID: 3665967 • Letter: M
Question
matlab. the root=( value_of_root) In this assignments 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. Include 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 Goals: Writing numerical analysis software The use of anonymous functions I/O to the disks Reading assignment: Recommended reading from MATLAB help documentation. Str2func fopen fprintf fclose Note: you should write the output to a file in your current directory.Explanation / Answer
%secant function
function y = secant(f,a,b,tol)
%calculating initial root value
c = (a*f(b) - b*f(a))/(f(b) - f(a));
%until tolerance consition satisfied
while abs(f(c)) > tol
a = b;
b = c;
%calculating root
c = (a*f(b) - b*f(a))/(f(b) - f(a));
temp = temp + 1;
if(temp == 10)
break;
end
end
%returns root
y = c;
end
%calling function
str = '@(x)(x.^2 + x -9)';
fhVal = str2func(str)
root = secant(fhVal,2,3.5,0.001)]
%writing results to file
fileName = fopen('result.txt','w');
fprintf(fileName,' Root is %6.6f ',root);
%closing file
fclose(fileName);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.