|
Sequence of Bisect implementations
|
Go to the source code of this file.
Functions | |
| close all define function and intervals syms x | f (x) |
| fplot (f,[a, b]) | |
| axis ([a, b,-1, 2]) hold on plot([a | |
| f | char () |
| xlabel ("x") legend("f(x)") %% fnum | |
| fprintf ('Equation to solve:%s=0. \n', char(f)) | |
| disp (['mid point:Approximate root:', num2str(rootApprox)]) | |
| fprintf ('mid point:root:%f after %i iterations. \n', rootApprox(end), length(rootApprox)) | |
| fprintf ('linear:root:%f after %i iterations. \n', rootLinear(end), length(rootLinear)) | |
| fprintf ('quad:root:%f after %i iterations. \n', rootQuad(end), length(rootQuad)) | |
| Evaluate the function at the approximate root and plot the result | semilogy (rootApprox, f_val, ' *b', 'MarkerSize', 10) |
| semilogy (abs(rootApprox-rootApprox(end)), '- *m', 'MarkerSize', 10) | |
| hold on | semilogy (max(eps/10, abs(rootLinear-rootLinear(end))), '- *b', 'MarkerSize', 10) |
| semilogy (max(eps/10, abs(rootQuad-rootQuad(end))), '-or', 'MarkerSize', 10) | |
| title ("Iteration history") | |
| xlabel ("iteration k") | |
| ylabel ("|x^\ast-x_k|") legend("midpoint" | |
| guarantee entry into the loop while | abs (fc) > |
| if (1==method &&(-fa/fb >TFAC||-fb/fa >TFAC) &&length(xvec)< 4) % improve linear method_loc=0 | |
| fprintf ('switch method to bisect at[%f, %f]\n', a, b) | |
| if | ~isempty (cr) c |
| else | disp ("no root found") end otherwise disp("Unknown method! Choose from [0 |
| end if | subs (func, x, midpoint) |
| elseif | subs (func, x, a) *subs(func |
Variables | |
| clear | |
| clc | |
| a = 0.01 | |
| b = 2 | |
| graphics | f0 =figure() |
| Define the tolerance for the bisection | method |
| Call the bisection method to find the root of the function | f |
| Call the bisection method to find the root of the function | eps |
| Call the bisection method to find the root of the function | false |
| rootApprox = Bisect(fnum, a, b, eps, 0) | |
| Linear improved bisection | rootLinear = Bisect(fnum, a, b, eps, true) |
| rootQuad = Bisect(fnum, a, b, eps, 2) | |
| f3 =figure() | |
| linear | |
| quadratic | |
| grid | on |
| function | xvec |
| fc = 2*eps | |
| fb =func(b) | |
| method_loc = method | |
| TFAC =2 | |
| end switch method_loc case midpoint | c = (a + b) / 2 |
| dd =[1 a a^2 | |
| cr = getRoot(dd,a,b) | |
| Exact root found | break |
| elseif fa *fc< 0 b=c;% Root is in the left half else a=c;% Root is in the right half end xvec=[xvec, c];% Store the midpointendend%%function r=getRoot(dd, a, b) r=roots(dd(end:-1:1));r=r(a<=r &r<=b);if length(r)==2 disp("!! 2 roots !!") endend%%function Visualize_Interval_subdivision(func, a, b)%fa=func(a);fb=func(b);c0=(a+b)/2;% midpointc1=a-(b-a) *fa/(fb-fa);% linear% c2=c0;% quadratic via midpointc2=c1;% quadratic via lineardd=[1 a a^2;1 b b^2;1 c2 c2^2]\[fa;fb;double(func(c2))];cr=getRoot(dd, a, b);if ~isempty(cr) c2=cr(1);else disp("no root found") endfigure() plot([a, b], [fa, fb], 'sg') hold onplot(c0, func(c0),' *m') plot(c1, func(c1), 'db') plot(c2, func(c2), 'or')%x=linspace(a, b, 101);plot(x, func(x),'-g') plot([a, b], [fa, fb],'-b') y=polyval(dd(end:-1:1), x);plot(x, y,'-r') plot([a, b], [0, 0],'-k') title("Visualize 3 strategies in bisection step") legend("given data","midpoint","linear","quadratic") end%%function xvec=Bisect_org(func, a, b, eps, linear)% Initialize the bisection method:Co-pilotxvec=[];while(b - a)/2 > eps if linear | midpoint = double(a-(b-a)*func(a)/(func(b)-func(a))) |
| elseif | x |
| guarantee entry into the loop while abs | ( | fc | ) |

| axis | ( | ) |
|
virtual |

| else disp | ( | "no root found" | ) |
| disp | ( | ) |
| fplot | ( | f | ) |
| fprintf | ( | 'linear:root:%f after %i iterations. \n' | , |
| rootLinear(end) | , | ||
| length(rootLinear) | |||
| ) |
| fprintf | ( | 'mid point:root:%f after %i iterations. \n' | , |
| rootApprox(end) | , | ||
| length(rootApprox) | |||
| ) |
|
pure virtual |
| semilogy | ( | abs(rootApprox-rootApprox(end)) | , |
| '- *m' | , | ||
| 'MarkerSize' | , | ||
| 10 | |||
| ) |
| hold on semilogy | ( | max(eps/10, abs(rootLinear-rootLinear(end))) | , |
| '- *b' | , | ||
| 'MarkerSize' | , | ||
| 10 | |||
| ) |
| Evaluate the function at the approximate root and plot the result semilogy | ( | rootApprox | , |
| f_val | , | ||
| ' *b' | , | ||
| 'MarkerSize' | , | ||
| 10 | |||
| ) |
| title | ( | "Iteration history" | ) |
| xlabel | ( | "iteration k" | ) |
| xlabel | ( | "x" | ) |
| ylabel | ( | "|x^\ast-x_k|" | ) |
| Call the bisection method to find the root of the function a = 0.01 |
Definition at line 6 of file bisect_vis.m.
| b b = 2 |
Definition at line 6 of file bisect_vis.m.
| Exact root found break |
Definition at line 100 of file bisect_vis.m.
Definition at line 81 of file bisect_vis.m.
| clc |
Definition at line 2 of file bisect_vis.m.
| clear |
Definition at line 2 of file bisect_vis.m.
Definition at line 87 of file bisect_vis.m.
Definition at line 86 of file bisect_vis.m.
| Call the bisection method to find the root of the function eps |
Definition at line 31 of file bisect_vis.m.
| Call the bisection method to find the root of the function f |
Definition at line 30 of file bisect_vis.m.
| graphics f0 =figure() |
Definition at line 17 of file bisect_vis.m.
| f3 =figure() |
Definition at line 49 of file bisect_vis.m.
| Call the bisection method to find the root of the function false |
Definition at line 31 of file bisect_vis.m.
| fb =func(b) |
Definition at line 72 of file bisect_vis.m.
| else end fc = 2*eps |
Definition at line 67 of file bisect_vis.m.
| linear |
Definition at line 58 of file bisect_vis.m.
| Define the tolerance for the bisection method |
Definition at line 28 of file bisect_vis.m.
| method_loc = method |
Definition at line 73 of file bisect_vis.m.
Definition at line 160 of file bisect_vis.m.
| grid on |
Definition at line 59 of file bisect_vis.m.
| quadratic |
Definition at line 58 of file bisect_vis.m.
| TFAC =2 |
Definition at line 74 of file bisect_vis.m.
| elseif x |
Definition at line 169 of file bisect_vis.m.
| function xvec |
Definition at line 63 of file bisect_vis.m.