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 ビュー (過去 30 日間)
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

採用された回答

VBBV
VBBV 2021 年 3 月 30 日
編集済み: VBBV 2021 年 3 月 30 日
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 件のコメント
VBBV
VBBV 2021 年 3 月 30 日
Welcome. Please accept the answer if it worked

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by