a) Type up and save the function “secant” below exactly as is and save it among
ID: 669697 • Letter: A
Question
a) Type up and save the function “secant” below exactly as is and save it among all your MATLAB work in your Mosaic account. Then type in this line in the main MATLAB command window (you will have to be in the same directory as the secant function):
root = secant (@(x) x^2+3x-1, 1, 0, 0.00001, 50)
Describe in your own words what you got here. When will this program stop the iterations? Name the two conditions specifically.
b) Now change the function in the line above from x^2+3x-1 to 35000-857x+40x^1.5. What solution did you get from this program? What solution did you get from your manual secant calculations? Are they the same? Why?
MATLAB:
function root = secant (func, xrold, xr, es, maxit)
if nargin<5, maxit=50; end
if nargin<4, es=0.001; end
iter=0;
while (1)
xrn=xr-func(xr)*(xrold-xr)/(func(xrold)-func(xr));
iter=iter+1;
if xrn~=0, ea=abs((xrn-xr)/xrn)*100; end
if ea<=es | iter >= maxit, break, end
xrold=xr;
xr=xrn;
end
root=xrn;
Explanation / Answer
(a)
The solution I got is 0.30278
The iterations stop when the 2 consecutive solutions we got have a difference less than or equal to 0.00001
or the maximum number of iterations is more than or equal to 50.
(b)
The solution I get is 65.690
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.