Solution
Solution
REPORT ON:
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
FLOW-CHART
START
NO
f(a)*f(b)<0
YES
For i=1:n
If f(x0)*f(x1)<0
NO YES
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
STOP
SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA SUBARNA
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
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
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