0% found this document useful (0 votes)
806 views

MATLAB Code For Bisection Method

The document describes the bisection method for finding the root of a nonlinear function. It defines the function f(x), initializes the interval [a,b], and iterates by choosing the midpoint c between a and b. It evaluates f(c) and either replaces the upper b or lower a bound based on the sign of f(c). It iterates until the function value at c is less than the tolerance or the maximum number of iterations is reached. It then prints the iteration number, root sequence, and function values.

Uploaded by

Zia Jan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
806 views

MATLAB Code For Bisection Method

The document describes the bisection method for finding the root of a nonlinear function. It defines the function f(x), initializes the interval [a,b], and iterates by choosing the midpoint c between a and b. It evaluates f(c) and either replaces the upper b or lower a bound based on the sign of f(c). It iterates until the function value at c is less than the tolerance or the maximum number of iterations is reached. It then prints the iteration number, root sequence, and function values.

Uploaded by

Zia Jan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

% Solution of nonlinear equations

% Bisection method
clear all, close all, clc
format short
f = @(x) x.^3+4*x.^2-10;
a=1; b=2;
max_I=20;% maximum number of iterations
tol=10^(-5);
fa=feval(f,a); % Initial guess
fb=feval(f,b);
if fa*fb>0,

disp('Wrong choice')
end

for k=1:max_I
c=(a+b)/2;
C(k)=c;
fc=feval(f,c);
Fc(k)=fc;
if fc==0,break
end
if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end
if abs(fc)<tol,break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values


fc\n')
[(1:k);C(1:k);Fc(1:k)]'

You might also like