isection(f,[1 1.5], %3D
Not a graded question
The required function is given below.
function c= bisection(f,interval,es)
a = interval(1);
b = interval(2);
if f(a)*f(b) >= 0
error('Use a bracketing interval')
end
c = mean([a,b]);
err = 1;
while err > es
if f(a)*f(c) < 0
b = c;
else
a = c;
end
c = mean([a,b]);
err = abs(c-b);
end
end
Output
>> f = @(x) x.^3-2;
>> bisection(f,[1,1.5],0.05)
ans =
1.2812
>> bisection(f,[0,1],0.05)
Error using bisection (line 5)
Use a bracketing interval
clear; clc; close all;
% Description: Calculates the root of a given function using bisection
% method
f = @(x) 2*x.*cos(2*x) - (x-2).^2;
fplot(f);
set(gca,'XAxisLocation','origin')
set(gca,'YAxisLocation','origin')
xlim([0 4])
ylim([-5 5])
xlabel('x')
ylabel('f(x)')
grid on
c = bisection(f,[2,3],1e-6);
hold on
plot(c,f(c),'rx')
fprintf('Root: %.4f\n',c)
c = bisection(f,[3,4],1e-6);
plot(c,f(c),'rx')
fprintf('Root: %.4f\n',c)
Output
Root: 2.3707
Root: 3.7221
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images