====== Differences ====== This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
gibson:teaching:fall-2012:math445:hw1-solns [2012/10/05 07:20] gibson created |
gibson:teaching:fall-2012:math445:hw1-solns [2012/10/08 12:36] (current) gibson |
||
---|---|---|---|
Line 145: | Line 145: | ||
<code> | <code> | ||
- | % argh, my code for this on my laptop at home; will fill in later | + | function xcenter = bisectsearch(f, xleft, xright) |
+ | % function xcenter = bisectsearch(f, xleft, xright) | ||
+ | % find a zero of f between xleft and xright using bisection search algorithm | ||
+ | |||
+ | eps = 1e-13; % stopping condition: abs(f(xcenter)) < eps | ||
+ | |||
+ | fleft = f(xleft); | ||
+ | fright = f(xright); | ||
+ | |||
+ | % should put in check that fleft and fright have opposite signs | ||
+ | |||
+ | xcenter = 0.5*(xleft + xright); | ||
+ | fcenter = f(xcenter); | ||
+ | |||
+ | % repeat until f(xcenter) is close enough to zero | ||
+ | while (abs(fcenter) > eps) | ||
+ | |||
+ | if (sign(fleft) == sign(fcenter)) | ||
+ | % replace the left stuff with the center stuff; | ||
+ | fleft = fcenter; | ||
+ | xleft = xcenter; | ||
+ | else | ||
+ | % replace the right stuff with the center stuff; | ||
+ | fright = fcenter; | ||
+ | xright = xcenter; | ||
+ | end | ||
+ | |||
+ | xcenter = 0.5*(xleft + xright); | ||
+ | fcenter = f(xcenter); | ||
+ | end | ||
+ | |||
+ | end | ||
</code> | </code> | ||