I mplement a MATLAB function secantBisection . Implement a MATLAB function secan
ID: 664397 • Letter: I
Question
Implement a MATLAB function secantBisection
. Implement a MATLAB function secantBisection of the form
function p=secantBisection(f,a,b,tol,N)
combining Secant method and the Bisection method according to the following procedure:
1. start with p = a; p1 = (a + b)/2;
2. Attempt a secant update step: p = p f(p) p1p f(p1)f(p) ;
3. If p is outside of [a, b], set p = p1;
4. If f(p)f(b) < 0, set a = p; otherwise set b = p;
5. Terminate if |f(p)| < tol;
6. Repeat from step 1, until maximal number of iteration is reached.
Run your function secantBisection using
f(x) = sin x e x on the interval [2, 12]: f = @(x) sin(x)-exp(-x); x = secantBisection(f,2,12,1e-6,100);
Present the result in a table showing for each iteration the method used (secant or bisection), a, b, p, and f(p).
Explanation / Answer
function root = bisect(f,a,b,tol,N)
%
% BISECT(f,a,b,tol,N)
% f string giving function name
% a,b such that f(a)*f(b) < 0
% tol positive tolerance
% N number of steps
% check input data
fa = feval(f,a);
fb = feval(f,b);
if ( fa*fb > 0 )
fprintf('input interval does not bracket ');
return
end
if ( tol <= 0 )
fprintf('tolerance must be positive ');
return
end
% compute midpoint and evaluate function there
f(a) = p
p1 = (a + b)/2;
f(p1) = feval(f,p1);
p = (p-(f(p)*p1)-p*(f(p1)-f(p)))
If (p > fa * fb)
p = p1;
f(p) = f(p1);
% main loop
while 1
If(f(p)*f(b) < 0)
a = p;
f(a) = f(p);
else
b = p;
f(b) = f(p);
return
end
if (f(p) < tol)
return
end
x : 9.452
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.