Problem 4 (programming): Create a MATLAB function named mysecant.m to estimate t
ID: 3718903 • Letter: P
Question
Problem 4 (programming): Create a MATLAB function named mysecant.m to estimate the root for any arbitrary function f with initial guesses 20 and z, and maximum absolute error ?. The function also takes in the maximum number of iterations, max.iter, to limit the iteration cycle. Use mysecant.m template posted in homework 4 folder on TritonED as a guide to implement the method. You can construct the function in your own way. The function should return the approximated root n and the number of iterations n. Use the function mysecant.m and set max iter to be 100 in the following exercises root in p4a and the corresponding number of iterations in p4b. p4c and the corresponding number of iterations in p4d. p4e and the corresponding number of iterations in p4f. (a, b) Find the root of f(x) e 2 -2 with o 1, 2, and E 10-3. Put the (c, d) Find the root of f(z) = z19 with z0-1, x,-0.5 and ? = 10-8. Put the root in (e, f) Find the root of f() with o1,0.5 and e 10-8. Put the root inExplanation / Answer
PLEASE REFER BELOW CODE
PLEASE CREATE mysecant.m AND PASTE BELOW CODE
function [root,iter] = mysecant(f,x0,x1,eps,max_iter)
iter = 0;
x(1) = x0;
x(2) = x1;
for i=3:max_iter
x(i) = x(i-1) - (f(x(i-1)))*((x(i-1) - x(i-2))/(f(x(i-1)) - f(x(i-2))));
iter=iter+1;
if abs((x(i)-x(i-1))/x(i))*100 < eps
root=x(i);
break
end
end
end
PLEASE CREATE test.m AND PASTE BELOW CODE
close all
clear all
clc
%1)
f = @(x) exp(x) - 2 * x - 2;
max_iter = 100;
x0 = 1;
x1 = 2;
eps = 10^-3;
[p4a,p4b] = mysecant(f,x0,x1,eps,max_iter)
%2)
f = @(x) x^19;
max_iter = 100;
x0 = 1;
x1 = 0.5;
eps = 10^-3;
[p4c,p4d] = mysecant(f,x0,x1,eps,max_iter)
%3)
f = @(x) x.^29;
max_iter = 100;
x0 = 1;
x1 = 0.5;
eps = 10^-3;
[p4e,p4f] = mysecant(f,x0,x1,eps,max_iter)
PLEASE REFER BELOW OUTPUT
p4a =
1.6783
p4b =
6
p4c =
0.5000
p4d =
1
p4e =
0.5000
p4f =
1
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.