The document describes using the bisection method to find the roots of different functions. It initializes the left and right bounds of the root, calculates the function values at the bounds, then iteratively calculates the midpoint between the bounds and determines if the function value at the midpoint is positive or negative to update the bounds. This process repeats for a set number of iterations to converge on an estimate of the root. The document provides examples finding the roots of f(x) = x^5 + x^3 + 4x^2 - 3x - 2, f(x) = x^3 - 7x^2 + 14x - 6, and f(x) = x^3 + 0.165x^2
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
65 views2 pages
Bisection Method
The document describes using the bisection method to find the roots of different functions. It initializes the left and right bounds of the root, calculates the function values at the bounds, then iteratively calculates the midpoint between the bounds and determines if the function value at the midpoint is positive or negative to update the bounds. This process repeats for a set number of iterations to converge on an estimate of the root. The document provides examples finding the roots of f(x) = x^5 + x^3 + 4x^2 - 3x - 2, f(x) = x^3 - 7x^2 + 14x - 6, and f(x) = x^3 + 0.165x^2
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 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