4%% define
function and intervals
6%
f(
x) = sin(
x) - 0.5 *
x;
a = 0.01;
b = 2;
7%
f(
x) = sqrt(sin(
x)) - 0.5 *
x;
a = 0.01;
b = 2;
8f(
x) = sqrt(sin(
x)) - 0.5 *
x^6;
a = 0.01;
b = 2;
9%
f(
x) = -atan(100*(
x-1.23456789));
a = 0.1;
b = 2;
10%
f(
x) = (1.234567-
x)*(
x+0.987654);
a = 1;
b = 2;
22% '$f(
a,
b,
c)=',latex(
f(
a,
b,
c)),'$'
27fnum=matlabFunction(
f);
28% Define the tolerance for the bisection
method: Co-pilot
30%% Call the bisection
method to find the root of the function
f: Co-pilot
33fprintf('Equation to solve: %s = 0. \n',
char(
f));
36%% Linear improved bisection
39fprintf('Equation to solve: %s = 0. \n',
char(
f));
50% Evaluate the function at the approximate root and plot the result
65% Initialize the bisection
method: Co-pilot
67fc = 2*
eps; % guarantee entry into the loop
70 Visualize_Interval_subdivision(func,
a,
b)
102 b =
c; % Root is in the left half
104 a =
c; % Root is in the right half
111function r = getRoot(
dd,
a,
b)
112r = roots(
dd(end:-1:1));
115 disp("!! 2 roots !!")
119function Visualize_Interval_subdivision(func,
a,
b)
121fa=func(
a);
fb=func(
b);
128dd =[1
a a^2; 1
b b^2; 1 c2 c2^2]\[fa;
fb;
double(func(c2))];
133 disp("no root found")
137plot([
a,
b],[fa,
fb],'sg')
139plot(c0,func(c0),'*m')
140plot(c1,func(c1),'db')
141plot(c2,func(c2),'or')
143x = linspace(
a,
b,101);
145plot([
a,
b],[fa,
fb],'-
b')
146y = polyval(
dd(end:-1:1),
x);
148plot([
a,
b],[0,0],'-k')
149title("Visualize 3 strategies in bisection step")
156% Initialize the bisection
method: Co-pilot
158while (
b -
a) / 2 >
eps
173 a =
midpoint; % Root is in the right half
double g(const double x)
Calculates function .
double Bisect(const double a, const double b, const double eps)
guarantee entry into the loop while abs(fc) >
fprintf('Equation to solve:%s=0. \n', char(f))
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
disp(['mid point:Approximate root:', num2str(rootApprox)])
ylabel("|x^\ast-x_k|") legend("midpoint"
axis([a, b,-1, 2]) hold on plot([a
Call the bisection method to find the root of the function eps
end switch method_loc case midpoint c
if(1==method &&(-fa/fb >TFAC||-fb/fa >TFAC) &&length(xvec)< 4) % improve linear method_loc=0
Define the tolerance for the bisection method
Call the bisection method to find the root of the function false
Evaluate the function at the approximate root and plot the result semilogy(rootApprox, f_val, ' *b', 'MarkerSize', 10)
title("Iteration history")
end if subs(func, x, midpoint)
xlabel("x") legend("f(x)") %% fnum
Call the bisection method to find the root of the function f
Linear improved bisection rootLinear