Sequence of Bisect implementations
Loading...
Searching...
No Matches
bisect_vis.m File Reference

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
 

Function Documentation

◆ abs()

guarantee entry into the loop while abs ( fc  )
Here is the caller graph for this function:

◆ axis()

axis ( )

◆ char()

f char ( )
virtual
Here is the caller graph for this function:

◆ disp() [1/2]

else disp ( "no root found"  )

◆ disp() [2/2]

disp ( )

◆ f()

close all define function and intervals syms x f ( x  )

◆ fplot()

fplot ( f  )

◆ fprintf() [1/5]

fprintf ( 'Equation to solve:%  s = 0. \n',
char(f  
)

◆ fprintf() [2/5]

fprintf ( 'linear:root:%f after %i iterations. \n'  ,
rootLinear(end)  ,
length(rootLinear  
)

◆ fprintf() [3/5]

fprintf ( 'mid point:root:%f after %i iterations. \n'  ,
rootApprox(end)  ,
length(rootApprox  
)

◆ fprintf() [4/5]

fprintf ( 'quad:root:%f after %i iterations. \n'  ,
rootQuad(end)  ,
length(rootQuad  
)

◆ fprintf() [5/5]

fprintf ( 'switch method to bisect at\n'  [%f, %f],
a  ,
b   
)

◆ if()

if ( )
pure virtual

◆ semilogy() [1/4]

semilogy ( abs(rootApprox-rootApprox(end))  ,
'- *m'  ,
'MarkerSize'  ,
10   
)

◆ semilogy() [2/4]

hold on semilogy ( max(eps/10, abs(rootLinear-rootLinear(end)))  ,
'- *b ,
'MarkerSize'  ,
10   
)

◆ semilogy() [3/4]

semilogy ( max(eps/10, abs(rootQuad-rootQuad(end)))  ,
'-or'  ,
'MarkerSize'  ,
10   
)

◆ semilogy() [4/4]

Evaluate the function at the approximate root and plot the result semilogy ( rootApprox  ,
f_val  ,
' *b ,
'MarkerSize'  ,
10   
)

◆ subs() [1/2]

elseif subs ( func  ,
x  ,
a   
)

◆ subs() [2/2]

end if subs ( func  ,
x  ,
midpoint   
)

◆ title()

title ( "Iteration history"  )

◆ xlabel() [1/2]

xlabel ( "iteration k"  )

◆ xlabel() [2/2]

xlabel ( "x"  )

◆ ylabel()

ylabel ( "|x^\ast-x_k|"  )

◆ ~isempty()

if ~isempty ( cr  )

Variable Documentation

◆ a

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 b = 2

Definition at line 6 of file bisect_vis.m.

◆ break

Exact root found break

Definition at line 100 of file bisect_vis.m.

◆ c

c c = (a + b) / 2

Definition at line 81 of file bisect_vis.m.

◆ clc

clc

Definition at line 2 of file bisect_vis.m.

◆ clear

clear

Definition at line 2 of file bisect_vis.m.

◆ cr

cr = getRoot(dd,a,b)

Definition at line 87 of file bisect_vis.m.

◆ dd

dd =[1 a a^2

Definition at line 86 of file bisect_vis.m.

◆ eps

Call the bisection method to find the root of the function eps

Definition at line 31 of file bisect_vis.m.

◆ f

Call the bisection method to find the root of the function f

Definition at line 30 of file bisect_vis.m.

◆ f0

graphics f0 =figure()

Definition at line 17 of file bisect_vis.m.

◆ f3

f3 =figure()

Definition at line 49 of file bisect_vis.m.

◆ false

Call the bisection method to find the root of the function false

Definition at line 31 of file bisect_vis.m.

◆ fb

fb =func(b)

Definition at line 72 of file bisect_vis.m.

◆ fc

else end fc = 2*eps

Definition at line 67 of file bisect_vis.m.

◆ linear

linear

Definition at line 58 of file bisect_vis.m.

◆ method

Define the tolerance for the bisection method

Definition at line 28 of file bisect_vis.m.

◆ method_loc

method_loc = method

Definition at line 73 of file bisect_vis.m.

◆ midpoint

else midpoint = double(a-(b-a)*func(a)/(func(b)-func(a)))

Definition at line 160 of file bisect_vis.m.

◆ on

grid on

Definition at line 59 of file bisect_vis.m.

◆ quadratic

quadratic

Definition at line 58 of file bisect_vis.m.

◆ rootApprox

rootApprox = Bisect(fnum, a, b, eps, 0)

Definition at line 32 of file bisect_vis.m.

◆ rootLinear

rootLinear = Bisect(fnum, a, b, eps, true)

Definition at line 37 of file bisect_vis.m.

◆ rootQuad

rootQuad = Bisect(fnum, a, b, eps, 2)

Definition at line 44 of file bisect_vis.m.

◆ TFAC

TFAC =2

Definition at line 74 of file bisect_vis.m.

◆ x

elseif x

Definition at line 169 of file bisect_vis.m.

◆ xvec

function xvec
Initial value:
= Bisect(func,a,b,eps,method)
% f(a) > = 0 >= f(b)
% Initialize the bisection method: Co-pilot
xvec = []
double Bisect(const double a, const double b, const double eps)
Definition Bisect6.cpp:124
Call the bisection method to find the root of the function eps
Definition bisect_vis.m:31
Define the tolerance for the bisection method
Definition bisect_vis.m:29
a
Definition bisect_vis.m:6
b
Definition bisect_vis.m:6
Call the bisection method to find the root of the function f
Definition bisect_vis.m:31
function xvec
Definition bisect_vis.m:63

Definition at line 63 of file bisect_vis.m.