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 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

VBBV
VBBV am 30 Mär. 2021
Bearbeitet: VBBV am 30 Mär. 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 Kommentare

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by