I am lost. I need to be able to show the iterations to find the root with given a and b values. Values for a and b should be user inputted. Convergence tolerance of abs(b-a) < 0.0001. fprint used to output the results from each iteration.

2 views (last 30 days)
clc;
close all;
f= @(x)x.^3 + 12*x.^2 - 100*x -6;
a = -1.0;
b = 0.0;
tol=0.0001;
err=abs(b-a);
if f(a)*f(b)>0
disp('Enter valid interval')
else
p = (a + b)/2;
err = abs(a-b);
while err >tol
if f(a)*f(p)<0
b = p;
else
a = p;
end
p = (a + b)/2;
err = abs(b-a);
end
fprintf('\n The root is %4.3f ',a);
end

Accepted Answer

VBBV
VBBV on 30 Mar 2021
Edited: VBBV on 30 Mar 2021
clc;
close all;
f= @(x) x.^3 + 12*x.^2 - 100*x -6;
%a = -1.0;
%b = 0.0;
a = input('enter value for a: ');
b = input('enter value for b: ');
tol=0.0001;
err=abs(b-a);
if f(a)*f(b)>0
disp('Enter valid interval')
else
p = (a + b)/2;
err = abs(a-b);
while err >tol
if f(a)*f(p)<0
b = p;
else
a = p;
end
p = (a + b)/2;
err = abs(b-a)
end
fprintf('\n The root is %4.3f ',a)
end
  6 Comments

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!