Você está na página 1de 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