Question
Complete with MatLab *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);
Complete with MatLab
*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) 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
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: a Count=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 Count by 1, set aCount=0; Right before the end of the loop: If acount reaches 3, set fb = shrinkFactor*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; >> I falsePosBiesecker (1,0,6,20) Iter 1 0.0000000000000000 b 6.0000000000000000 = 0.0149094699410675 P(a) -1.00+00F (b) = 4.01e+02 F(c)= -9.85e-01; --> Replace a Iter 2 0.0149094699410675 b= 6.0000000000000000 0.0295590368049233 F(a)= -9.85e-01 F(b)= 4.0le+02 F(c)= -9.70e-01; --> Replace a Iter 3 0.029559036B049233b 6.000000000000000D 0.0439510439107312 P(a)= -9.70e-01 F(b)= 4.01e+02 F(c)= -9.55e-01; --> Replace a Iter 4 am 0.0439510439107312b 6.DODOO DODODDODOOD - 0.1823627359212497 (a)= -9.55e-01 F(b)= 4.0le+01 F(c)= -8.00-01; --> Replace a Iter 5 a 0.1823627359212497 b= 6.DODOODODOODODOOD - 0.2960290 930014636 F(a)-8.00e-01 F(b) = 4.0le+01 Fic)= -6.55e-01; --> Replace a Iter 6 - 0.2960290930014636 b 6.DODOODODOO DODOOD = 0.3876724609798022 F(a)= -6.55e-01 F(b)= 4.01e+01 Fic)= -5.26e-01; --> Replace a Iter 7 - 0.3876724609798022 b= 6.000ODOODODOO 0000 = 1.0383650261806638 (a) = -5.26e-01 F(b)= 4.01e+00 F (c) = 8.25e-01; --> Replace b Iter 8 a 0.3876724609798022 b 1.0383650261806638 = 0.6412230409194683 P(a) -5.26e-01 F(b) = 8.25e-01 FC)= -1.01e-011 --> Replace a Iter 9 am 0.6412230409194683 - 1.0383650261806638 = 0.6846345281725860 Pa -1.01e-01 F(b) 8.25e-01 FC) = -1.70e-02, --> Replace a Iter 10 0.6846345281725860 b 1.0383650261806638 0.6917604531721222 (a) -1.70e-02 F(b) 8.25-01 PC) -2.77e-03) --> Replace a Iter 11 0.6917604531721222 b 1.0383650261806630 0.7030312984369009 (a) -2.77e-03 F(b)= 8.25e-02 FC)= 1.996-02; --> Replace b Iter 12 a 0.6917604531721222 b= 0.7030312984369009 = 0.6931403369851086 (a) -2.77-03 F(b)= 1.99-02 Fic)= -1.37e-05; --> Replace a Iter 13 0.6931403369851086 0.7030312984369009 0 .6931471467942724 (a) -1.376-05 F(b)= 1.99-02 Fic)= -6.75e-08; --> Replace a Iter 14 - 0.6931471467942724 b 0.7030312984369009 c 0.6931471803933483 (a) -6.750-08 F(b) = 1.99-02 Pc)= -3.33e-10; --> Replace a Iter 15 - 0.6931471803933483 b 0.7030312984369009 0 .6931471820510979 (a)= -3.33e-10 F(b)= 1.99e-03 F(c)= 2.98-09; --> Replace b Iter 16 - 0.6931471803933483 b 0.6931471820510979 0.6931471805599453 (a)= -3.33e-10 F(b)= 2.98e-09 Fic)= 0.00e+00; Terminating on Iteration 16: f(c) near O 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: a Count=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 Count by 1, set aCount=0; Right before the end of the loop: If acount reaches 3, set fb = shrinkFactor*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; >> I falsePosBiesecker (1,0,6,20) Iter 1 0.0000000000000000 b 6.0000000000000000 = 0.0149094699410675 P(a) -1.00+00F (b) = 4.01e+02 F(c)= -9.85e-01; --> Replace a Iter 2 0.0149094699410675 b= 6.0000000000000000 0.0295590368049233 F(a)= -9.85e-01 F(b)= 4.0le+02 F(c)= -9.70e-01; --> Replace a Iter 3 0.029559036B049233b 6.000000000000000D 0.0439510439107312 P(a)= -9.70e-01 F(b)= 4.01e+02 F(c)= -9.55e-01; --> Replace a Iter 4 am 0.0439510439107312b 6.DODOO DODODDODOOD - 0.1823627359212497 (a)= -9.55e-01 F(b)= 4.0le+01 F(c)= -8.00-01; --> Replace a Iter 5 a 0.1823627359212497 b= 6.DODOODODOODODOOD - 0.2960290 930014636 F(a)-8.00e-01 F(b) = 4.0le+01 Fic)= -6.55e-01; --> Replace a Iter 6 - 0.2960290930014636 b 6.DODOODODOO DODOOD = 0.3876724609798022 F(a)= -6.55e-01 F(b)= 4.01e+01 Fic)= -5.26e-01; --> Replace a Iter 7 - 0.3876724609798022 b= 6.000ODOODODOO 0000 = 1.0383650261806638 (a) = -5.26e-01 F(b)= 4.01e+00 F (c) = 8.25e-01; --> Replace b Iter 8 a 0.3876724609798022 b 1.0383650261806638 = 0.6412230409194683 P(a) -5.26e-01 F(b) = 8.25e-01 FC)= -1.01e-011 --> Replace a Iter 9 am 0.6412230409194683 - 1.0383650261806638 = 0.6846345281725860 Pa -1.01e-01 F(b) 8.25e-01 FC) = -1.70e-02, --> Replace a Iter 10 0.6846345281725860 b 1.0383650261806638 0.6917604531721222 (a) -1.70e-02 F(b) 8.25-01 PC) -2.77e-03) --> Replace a Iter 11 0.6917604531721222 b 1.0383650261806630 0.7030312984369009 (a) -2.77e-03 F(b)= 8.25e-02 FC)= 1.996-02; --> Replace b Iter 12 a 0.6917604531721222 b= 0.7030312984369009 = 0.6931403369851086 (a) -2.77-03 F(b)= 1.99-02 Fic)= -1.37e-05; --> Replace a Iter 13 0.6931403369851086 0.7030312984369009 0 .6931471467942724 (a) -1.376-05 F(b)= 1.99-02 Fic)= -6.75e-08; --> Replace a Iter 14 - 0.6931471467942724 b 0.7030312984369009 c 0.6931471803933483 (a) -6.750-08 F(b) = 1.99-02 Pc)= -3.33e-10; --> Replace a Iter 15 - 0.6931471803933483 b 0.7030312984369009 0 .6931471820510979 (a)= -3.33e-10 F(b)= 1.99e-03 F(c)= 2.98-09; --> Replace b Iter 16 - 0.6931471803933483 b 0.6931471820510979 0.6931471805599453 (a)= -3.33e-10 F(b)= 2.98e-09 Fic)= 0.00e+00; Terminating on Iteration 16: f(c) near OStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started