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 vues (au cours des 30 derniers jours)
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

Réponse acceptée

VBBV
VBBV le 30 Mar 2021
Modifié(e) : VBBV le 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 commentaires
VBBV
VBBV le 30 Mar 2021
Welcome. Please accept the answer if it worked

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by