Вы находитесь на странице: 1из 2

% Bisection Method to find the roots of f(x) = x^5 + x^3 + 4x^2 - 3x - 2

xleft = -2; xright = -1;


% input: xleft, xright = left and right brackets of the root
% n = (optional) number of iterations, default n = 15
% output x = estimate of the root
% margin<3,
n = 25; % default number of iterations
a = xleft; b = xright; % copy original bracket to local variables
fa = a^5 + a^3 + 4*a^2 - 3*a - 2; % initial value
fb = b^5 + b^3 + 4*b^2 - 3*b - 2; % initial value
fprintf('k z xmid b f(xmid)\n')
for k = 1:n
xm = a + 0.5*(b-a); % Minimize roundoff in the Midpoint
fm = xm^5 + xm^3 + 4*xm^2 - 3*xm - 2; % f(x) at midpoint
fprintf('%3d %12.8f %12.8f %12.8f %12.3e\n', k, a, xm, b, fm);
if sign(fm)==sign(fa)
a = xm; fa = fm;
else
b = xm; fb = fm;
end
end


%%
% f(x) = x^3 - 7*x^2 + 14*x - 6
xleft = 0; xright = 1;
n = 1; % default number of iterations
a = xleft; b = xright; % copy original bracket to local variables
fa = a^3-7*a^2+14*a-6; % initial value
fb = b^3-7*b^2+14*b-6; % initial value
k = 0;
fm = 1;
fprintf('k a xmid b f(xmid)\n');
while abs(fm)>=0.0000001 & k<=50
k = k+1;
xm = a + 0.5*(b-a); % Minimize roundoff in the Midpoint
fm = xm^3-7*xm^2+14*xm-6; % f(x) at midpoint
fprintf('%3d %12.8f %12.8f %12.8f %12.3e\n', k, a, xm, b, fm);
if sign(fm)==sign(fa)
a = xm; fa = fm;
else
b = xm; fb = fm;
end
end













%%
%%
% f(x) = x^3 - 7*x^2 + 14*x - 6
xleft = 0; xright = 1;
n = 30; % default number of iterations
a = xleft; b = xright; % copy original bracket to local variables
fa = a^3 - 7*a^2 + 14*a - 6; % initial value
fb = b^3 - 7*b^2 + 14*b - 6; % initial value
fprintf('k a xmid b f(xmid)\n');
for k = 1:n
xm = a + 0.5*(b-a); % Minimize roundoff in the Midpoint
fm = xm^3 - 7*xm^2 + 14*xm - 6; % f(x) at midpoint
fprintf('%3d %12.8f %12.8f %12.8f %12.3e\n', k, a, xm, b, fm);
if sign(fm)==sign(fa)
a = xm; fa = fm;
else
b = xm; fb = fm;
end
end




%%
% Bisection Method to find the roots of f(x) = x^3 + 0.165*x^2 + 3.993*10^-4
xleft = -1; xright = 0;
% input: xleft, xright = left and right brackets of the root
% n = (optional) number of iterations, default n = 15
% output x = estimate of the root
% margin<3,
n = 20; % default number of iterations
a = xleft; b = xright; % copy original bracket to local variables
fa = a^3 + 0.165*a^2 + 3.993*10^-4; % initial value
fb = b^3 + 0.165*b^2 + 3.993*10^-4; % initial value
fprintf('k z xmid b f(xmid)\n')
for k = 1:n
xm = a + 0.5*(b-a); % Minimize roundoff in the Midpoint
fm = xm^3 + 0.165*xm^2 + 3.993*10^-4; % f(x) at midpoint
fprintf('%3d %12.8f %12.8f %12.8f %12.3e\n', k, a, xm, b, fm);
if sign(fm)==sign(fa)
a = xm; fa = fm;
else
b = xm; fb = fm;
end
end

Вам также может понравиться