Bisection Method MATLAB Program with Output

MATLAB program for finding real root of non-linear equation using Bisection Method with Output

Bisection Method MATLAB program


% Clearing Screen
clc

% Setting x as symbolic variable
syms x;

% Input Section
y = input('Enter non-linear equations: ');
a = input('Enter first guess: ');
b = input('Enter second guess: ');
e = input('Tolerable error: ');

% Finding Functional Value
fa = eval(subs(y,x,a));
fb = eval(subs(y,x,b));

% Implementing Bisection Method
if fa*fb > 0 
    disp('Given initial values do not bracket the root.');
else
    c = (a+b)/2;
    fc = eval(subs(y,x,c));
    fprintf('\n\na\t\t\tb\t\t\tc\t\t\tf(c)\n');
    while abs(fc)>e
        fprintf('%f\t%f\t%f\t%f\n',a,b,c,fc);
        if fa*fc< 0
            b =c;
        else
            a =c;
        end
        c = (a+b)/2;
        fc = eval(subs(y,x,c));
    end
    fprintf('\nRoot is: %f\n', c);
end

Bisection Method MATLAB Output


Enter non-linear equations: cos(x) - x * exp(x)
Enter first guess: 0
Enter second guess: 1
Tolerable error: 0.00001


a		b		c		f(c)
0.000000	1.000000	0.500000	0.053222
0.500000	1.000000	0.750000	-0.856061
0.500000	0.750000	0.625000	-0.356691
0.500000	0.625000	0.562500	-0.141294
0.500000	0.562500	0.531250	-0.041512
0.500000	0.531250	0.515625	0.006475
0.515625	0.531250	0.523438	-0.017362
0.515625	0.523438	0.519531	-0.005404
0.515625	0.519531	0.517578	0.000545
0.517578	0.519531	0.518555	-0.002427
0.517578	0.518555	0.518066	-0.000940
0.517578	0.518066	0.517822	-0.000197
0.517578	0.517822	0.517700	0.000174
0.517700	0.517822	0.517761	-0.000012
0.517700	0.517761	0.517731	0.000081
0.517731	0.517761	0.517746	0.000035
0.517746	0.517761	0.517754	0.000011

Root is: 0.517757

Recommended Reading

  1. Bisection Method Algorithm
  2. Bisection Method Pseudocode
  3. Bisection Method Using C
  4. Bisection Method Using C++
  5. Bisection Method Using MATLAB
  6. Bisection Method Online Calculator