SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
NEPAL ENGINEERING COLLEGE
(AFFILIATED TO POKHARA UNIVERSITY)
Changunarayan, Bhaktapur
REPORT ON:
Root of Nonlinear Equation Using Bisection Method
SUBMITTED BY: SUBMITTED TO:
NAME: SUBARNA KHADKA(020-624) ELE AND ELX
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
Experiment No: - 2 TITLE:-
Root of Nonlinear Equation Using Bisection Method
OBJECTIVE:-
To implement and calculate the root using the bisection method on MATLAB
and C-programming.
THEORY:-
The bisection method, which is alternatively called binary chopping or interval
halving, is one type of incremental search method in which the interval is always
divided in half. If a function changes sign over an interval, the function value at
the midpoint is evaluated. The location of the root is then determined as lying
at the midpoint of the subinterval within which the sign change occurs. The
process is repeated to obtain refined estimates. An equation(𝑥) = 0, has at least
one root between guess 1 (x1) and guess 2 (x2) if𝑓(𝑥1) ∗ 𝑓(𝑥2) < 0.
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
Algorithm: 1. Decide initial values x1 and x2 such that f(x1)*f(x2)
< 0.
2. Compute 𝑥0 = 𝑥1+𝑥2 2 and 𝑓(𝑥0)
3. If 𝑓(𝑥0) ∗ 𝑓(𝑥1) < 0 then
Set 𝑥2 = 𝑥0
else
Set 𝑥1 = 𝑥0
4. If absolute value of 𝑓(𝑥0) is less than or equal to given limit,
then 𝑟𝑜𝑜𝑡 = 𝑥0
Display the value of root.
Go to step 5
Else
Go to step 2
5. Stop
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
FLOW-CHART
START
Defining the function f(x)
Input from user as x1 and x2
NO
f(a)*f(b)<0
YES
For i=1:n
Calculate x0 and f(x0)
If f(x0)*f(x1)<0
NO YES
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
x1=x0 ,x2 =x2 X2=x0 ,x1 =x1
Print the root
STOP
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
Question: Implement above algorithm in MATLAB to calculate a
root of the following equations
a) 𝑥2 – 4x-10=0
Using C-programming
Syntax:-
#include <stdio.h>
#include<conio.h>
#include <math.h>
double func(double x) {
return x*x-4*x-10;
}
double bisection(double a, double b, double epsilon) {
double c;
int iterations = 0;
printf("Iteration\t a\t\t b\t\t c\t\t f(c)\n");
while (1) {
c = (a + b) / 2.0;
printf("%8d\t%10.6f\t%10.6f\t%10.6f\t%10.6f\n",
iterations, a, b, c, func(c));
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
if (func(c) == 0.0 || fabs(b - a) < epsilon) {
break;
}
if (func(c) * func(a) < 0) {
b = c;
} else {
a = c;
}
iterations++;
if (iterations > 100) {
printf("Maximum iterations exceeded.\n");
break;
}
}
return c;
}
int main() {
printf("SUBARNA SUBARNA KHADKA KHADKA 020 020 624
624");
double a, b, epsilon;
double root;
printf("Enter the value of a:");
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
scanf("%lf",&a);
printf("Enter the value of b:");
scanf("%lf",&b);
epsilon = 0.00001;
root = bisection(a, b, epsilon);
printf("Approximate root: %lf\n", root);
return 0;
getch();
}
OUTPUT:
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
MATLAB CODE:
f = @(x) x^2-4*x-10;
a=input('Enter the first guess value,a:');
b=input('Enter the first guess value,b:');
n=25;
e=0.000001;
fprintf('iter\t a\t\t b\t\t c\t\t f(c)\t\t error\n')
if f(a)*f(b)<0
for i=1:n
c=(a+b)/2;
err=min(abs(c-a),abs(c-b));
fprintf('%d\t %4f\t %4f\t %4f\t%4f\t %4f\n',i,a,b,c,f(c),err)
if(err<e)
disp(['Root=' num2str(c)])
break
end
if f(c)*f(a)<0
b=c;
elseif f(c)*f(b)<0
a=c;
end
end
else
disp('No root between given interval')
end
disp('SUBARNA SUBARNA KHADKA KHADKA 020 624 020 624')
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
OUTPUT:
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
b) 𝒙𝒔𝒊𝒏(𝒙) + 𝐜𝐨𝐬(𝒙) = 0
C PROGRAMMING CODE:
#include <stdio.h>
#include<conio.h>
#include <math.h>
double func(double x) {
return x*sin(x)+cos(x);
}
double bisection(double a, double b, double epsilon) {
double c;
int iterations = 0;
printf("Iteration\t a\t\t b\t\t c\t\t f(c)\n");
while (1) {
c = (a + b) / 2.0;
printf("%8d\t%10.6f\t%10.6f\t%10.6f\t%10.6f\n",
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
iterations, a, b, c, func(c));
if (func(c) == 0.0 || fabs(b - a) < epsilon) {
break;
}
if (func(c) * func(a) < 0) {
b = c;
} else {
a = c;
}
iterations++;
if (iterations > 100) {
printf("Maximum iterations exceeded.\n");
break;
}
}
return c;
}
int main() {
double a, b, epsilon;
double root;
printf("Enter the value of a:");
scanf("%lf",&a);
printf("Enter the value of b:");
scanf("%lf",&b);
epsilon = 0.00001;
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
root = bisection(a, b, epsilon);
printf("Approximate root: %lf\n", root);
printf("SUBARANA SUBARNA KHADKA KHADKA 624 624");
return 0;
getch();
}
OUTPUT:
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
MATLAB CODE:
f = @(x) x.*sin(x)+cos(x);
a=input('Enter the first guess value,a:');
b=input('Enter the first guess value,b:');
n=40;
e=0.0001;
fprintf('iter\t a\t\t b\t\t c\t\tf(c)\t\t error\n')
if f(a)*f(b)<0
for i=1:n
c=(a+b)/2;
err=min(abs(c-a),abs(c-b));
fprintf('%d\t %4f\t %4f\t %4f\t%4f\t %4f\n',i,a,b,c,f(c),err)
if(err<e)
disp(['Root=' num2str(c)])
break
end
if f(c)*f(a)<0
b=c;
elseif f(c)*f(b)<0
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
a=c;
end
end
else
disp('No root between given interval')
end
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
OUTPUT:
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
Description:-
From above program on both c and matlab it was clear that
root are same using any of c-programming or matalb and also
from the graph. So, using above program we can find the root.
Conclusion:-
Hence, from above we can implement and calculate the root
using the bisection method on Matlab and Cprogramming