Question: Answer with MatLab Code *MatLab Code to Modify function [root,f_root,k]=falsePosBiesecker(f,a,b,maxIters) tolX=1e-12; tolF=1e-14; cOld=a; fa=f(a); fb=f(b); aCount=0; bCount=0; for k=1:maxIters c= (a*fb - b*fa)/(fb-fa); fc =
Answer with MatLab Code
Problem 2: falsePos Yourname.m Modify the existing function to accelerate convergence when the algorithm gets "stuck" on one the bracketing endpoints. Specifically, if the left endpoint a is replaced is changed 3 times in a row, then artificially lower f(b). Likewise, if the left endpoint b is replaced is changed 3 times in a row, then artificially lower f(a). There are several ways to do this, but here is one method that will work: Before The Loop: Define shrinkFactor = 0.1; Initialize Counters: aCount=0; bCount=0; In the loop: When Deciding on the Bracket Update: If a is replaced, also increment aCount by 1 & set bCount=0; If b is replaced, increment bCount by 1, set aCount=0; Right before the end of the loop: If aCount reaches 3, set fb = shrink Factor*fb and reset aCount=0; If bCount reaches 3, set fa = shrinkFactor* fa and reset bCount=0; Solving exp(x) =2 using the modified False Position algorithm. Your output should look mine. Notice how after iteration 3, F(b) was shrunk by a factor of 10 because a was replaced 3 times in a row. >> f = @(x) exp (x) - 2; >> r = false PosBiesecker (f,0,6,20) Iter i a= 0.0000000000000000 b= 6.0000000000000000 c= 0.0149094699410675 F(a)= -1.00e+00 F(b)= 4.01e+02 F(C)= -9.85e-01; --> Replace a Iter 2 a= 0.0149094699410675 b= 6.0000000000000000 c= 0.0295590368049233 F(a) - - 9.85e-01 F(b) = 4.01e+02 F(C)= -9.70e-01; --> Replace a Iter 3 a= 0.0295590368049233 b= 6.0000000000000000 c= 0.0439510439107312 F(a) = -9.70e-01 F(b) = 4.01e+02 F(C) = -9.55e-01; --> Replace a Iter a= 0.0439510439107312 b= 6.0000000000000000 c= 0.1823627359212497 F(a) = -9.55e-01 F(b) = 4.01e+01 F(C) = -8.00e-01; --> Replace a Iter 5 a= 0.1823627359212497 b= 6.0000000000000000 c= 0.2960290930014636 F(a) = -8.00e-01 F(b) = 4.01e+01 F(C)= -6.55e-01; --> Replace a Iter 6 a= 0.2960290930014636 b= 6.0000000000000000 c= 0.3876724609798022 F(a) = -6.55e-01 F(b) = 4.01e+01 F(C) = -5.26e-01; --> Replace a Iter 7 a= 0.3876724609798022 b= 6.0000000000000000 c= 1.0383650261806638 F(a)= -5.26e-01 F(b)= 4.01e+00 F(c) = 8.25e-01; --> Replace b Iter 8 a= 0.3876724609798022 b- 1.0383650261806638 c= 0.6412230409194683 F(a) = -5.26e-01 F(b) = 8.25e-01 F(C) = -1.01e-01; --> Replace a Iter 9 a= 0.6412230409194683 b= 1.0383650261806638 c= 0.6846345281725860 F(a)= -1.01e-01 F(b) = 8.25e-01 F(C)= -1.70e-02; --> Replace a Iter 10 a= 0.6846345281725860 b= 1.0383650261806638 c= 0.6917604531721222 F(a)= -1.70e-02 F(b) = 8.25e-01 F(C)= -2.77e-03; --> Replace a Iter 11 a= 0.6917604531721222 b= 1.0383650261806638 c= 0.7030312984369009 F(a)= -2.77e-03 F(b) = 8.25e-02 F(C)= 1.99e-02; --> Replace b Iter 12 a= 0.6917604531721222 b= 0.7030312984369009 c= 0.6931403369851086 F(a)= -2.77e-03 F(b) = 1.99e-02 F(c) = -1.37e-05; --> Replace a Iter 13 a= 0.6931403369851086 b= 0.7030312984369009 c= 0.6931471467942724 F(a)= -1.37e-05 F(b) = 1.99e-02 F(c) = -6.75e-08; --> Replace a Iter 14 a= 0.6931471467942724 b= 0.7030312984369009 c= 0.6931471803933483 F(a)= -6.75e-08 F(b) = 1.99e-02 F(C)= -3.33e-10; --> Replace a Iter 15 Iter 15 a= 0.6931471803933483 b= 0.7030312984369009 c= 0.6931471820510979 F(a) = -3.33e-10 F(b)= 1.99e-03 F(C)= 2.98e-09; --> Replace b Iter 16 a= 0.6931471803933483 b= 0.6931471820510979 c= 0.6931471805599453 F(a)= -3.33e-10 F(b)= 2.98e-09 F(c)= 0.002+00; Terminating on Iteration 16: f(c) near 0
*MatLab Code to Modify
function [root,f_root,k]=falsePosBiesecker(f,a,b,maxIters)
tolX=1e-12;
tolF=1e-14;
cOld=a;
fa=f(a);
fb=f(b);
aCount=0;
bCount=0;
for k=1:maxIters
c= (a*fb - b*fa)/(fb-fa);
fc = f(c);
% printing iteration summary
fprintf('Iter %d ',k);
fprintf(' a=%20.16f b=%20.16f c=%20.16f ',a,b,c);
fprintf(' F(a)= %4.2e F(b)= %4.2e F(c)= %5.2e; ',fa,fb,fc);
% decide whether to stop due to small F(x)
if abs(fc)
fprintf(' -------------------------------------- ');
fprintf('Terminating on Itertion %d: f(c) near 0 ',k);
break;
end
% decide whether to stop due little change in estimated root
if abs(c - cOld)/(abs(c)+1e-99)
fprintf(' -------------------------------------- ');
fprintf('Terminating on Itertion %d: Small Change in c ',k);
break;
end
% if not stopping, then update the bracket
if fc*fa > 0
a=c; fa=fc; report='--> Replace a'; aCount=0; bCount=bCount+1;
else % fc*fb > 0
b=c; fb=fc; report='--> Replace b'; bCount=0; aCount=aCount+1;
end
fprintf('%s ',report); % reporting the bracket update
cOld=c; % keep track of previous c in order to compare to the new estimate
end
% assign output values
root=c;
f_root=fc;
end


Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
