0% found this document useful (0 votes)
58 views8 pages

Bisection Codes

The document defines a function called bisect_04 that implements the bisection method to find the root of a function within a given interval. It takes in the function, left endpoint, right endpoint, and maximum number of iterations as inputs. It then iteratively calculates the midpoint of the interval, evaluates the function there, and narrows the interval based on the sign of the function value until convergence within a specified tolerance or max iterations is reached.

Uploaded by

henny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views8 pages

Bisection Codes

The document defines a function called bisect_04 that implements the bisection method to find the root of a function within a given interval. It takes in the function, left endpoint, right endpoint, and maximum number of iterations as inputs. It then iteratively calculates the midpoint of the interval, evaluates the function there, and narrows the interval based on the sign of the function value until convergence within a specified tolerance or max iterations is reached.

Uploaded by

henny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

function c = bisect_01;

a = input('Please Enter the left endpoint, a: ');


b = input('Please Enter the right endpoint, b: ');
disp(' ')
fprintf('i
disp(' ')

f(a)

f(b)

f(c)

c\n')

format long
n = 50;
epsilon = 1e-08;
fa = 1+2*a+3*a^2+4*a^3;
fb = 1+2*b+3*b^2+4*b^3;
if sign (fa) == sign (fb)
disp(' ')
error(sprintf('No root within [%f %f] bracket\n',a,b))
disp(' ')
end
for k=1:n
c=a+(b-a)/2;
fc= 1+2*c+3*c^2+4*c^3;
fprintf('%3i %14.8f %14.8f %14.8f %12.8f

%12.8f

%12.8f\n',k,fa,fb,fc,a,b,c)

if abs(fc)<=epsilon | abs(b-a)<epsilon
disp(' ')
fprintf('Approximation to the root is %12.9f \n',c)
fprintf('Convergent achieved after %3i iterations \n',k)
break
end
if sign (fc) == sign (fb)
b = c; fb = fc;
else

end

end

a = c; fa = fc;

if k==n
disp(' ')
fprintf('WARNING: Desired Accuracy was not reach after %i iterations !\n',k)
fprintf('Increase the number of Iteration !!!')
disp(' ')
end

function c = bisect_02;
a = input('Please Enter the left endpoint, a: ');
b = input('Please Enter the right endpoint, b: ');
disp(' ')
fprintf('i
disp(' ')

f(a)

f(b)

f(c)

c\n')

format long
n = 50;
epsilon = 1e-08;
fa = f(a);
fb = f(b);
if sign (fa) == sign (fb)
disp(' ')
error(sprintf('No root within [%f %f] bracket\n',a,b))
disp(' ')
end
for k=1:n
c=a+(b-a)/2;
fc= f(c);
fprintf('%3i %14.8f %14.8f %14.8f %12.8f

%12.8f

%12.8f\n',k,fa,fb,fc,a,b,c)

if abs(fc)<=epsilon | abs(b-a)<epsilon
disp(' ')
fprintf('Approximation to the root is %12.9f \n',c)
fprintf('Convergent achieved after %3i iterations \n',k)
break
end
if sign (fc) == sign (fb)
b = c; fb = fc;

else

a = c; fa = fc;

end
if k==n
disp(' ')
fprintf('WARNING: Desired Accuracy was not reach after %i iterations !\n',k)
fprintf('Increase the number of Iteration !!!')
disp(' ')
end
end
% Subroutine
function fx = f(x)
fx = 1+2*x+3*x^2+4*x^3;
return

function c = bisect_03(a,b);
fprintf('i
disp(' ')

f(a)

f(b)

f(c)

c\n')

format long
n = 50;
epsilon = 1e-08;
fa = f(a);
fb = f(b);
if sign (fa) == sign (fb)
disp(' ')
error(sprintf('No root within [%f %f] bracket\n',a,b))
disp(' ')
end
for k=1:n
c=a+(b-a)/2;
fc= f(c);
fprintf('%3i %14.8f %14.8f %14.8f %12.8f

%12.8f

%12.8f\n',k,fa,fb,fc,a,b,c)

if abs(fc)<=epsilon | abs(b-a)<epsilon
disp(' ')
fprintf('Approximation to the root is %12.9f \n',c)
fprintf('Convergent achieved after %3i iterations \n',k)
break
end
if sign (fc) == sign (fb)
b = c; fb = fc;
else
a = c; fa = fc;
end

if k==n
disp(' ')
fprintf('WARNING: Desired Accuracy was not reach after %i iterations !\n',k)
fprintf('Increase the number of Iteration !!!')
disp(' ')
end
end
% Subroutine
function fx = f(x)
fx = 1+2*x+3*x^2+4*x^3;
return

function c = bisect_04(func,a,b,n);
if nargin<4, n=30; end
fprintf('i
disp(' ')

f(a)

f(b)

f(c)

c\n')

format long
epsilon = 1e-08;
fa = feval(func,a);
fb = feval(func,b);
if sign (fa) == sign (fb)
disp(' ')
error(sprintf('No root within [%f %f] bracket\n',a,b))
disp(' ')
end
for k=1:n
c=a+(b-a)/2;
fc= feval(func,c);
fprintf('%3i %14.8f %14.8f %14.8f %12.8f

%12.8f

%12.8f\n',k,fa,fb,fc,a,b,c)

if abs(fc)<=epsilon | abs(b-a)<epsilon
disp(' ')
fprintf('Approximation to the root is %12.9f \n',c)
fprintf('Convergent achieved after %3i iterations \n',k)
break
end
if sign (fc) == sign (fb)
b = c; fb = fc;
else
a = c; fa = fc;

end
if k==n
disp(' ')
fprintf('WARNING: Desired Accuracy was not reach after %i iterations !\n',k)
fprintf('Increase the number of Iteration !!!')
disp(' ')
end
end

You might also like