フィルターのクリア

Indexing through an array

1 回表示 (過去 30 日間)
Austin Shipley
Austin Shipley 2020 年 11 月 17 日
コメント済み: Austin Shipley 2020 年 11 月 17 日
This program is designed to approximate cubed roots based on user input values of the number they want to approximate and their estimate. The loop iterates until the error is less than 1e-9.
I am trying to store all of my estimate values into a vector array and plot them on a graph, but I keep getting an error saying that my index values exceed the array elements. Any clarification about how to resolve this issue would be greatly appreciated.
num = input('Enter a number you would like to find the cubed root of: ')
est = input('Enter your initial estimate: ')
err = abs(num - est.^3)
est(1) = est
k = 2;
while err > 1e-9
est(k+1) = (1/3)*(2.*est(k)+num./est(k.^2));
err = abs(num - est(k.^3));
k = k + 1;
end
plot(1:length(est),est)
fprintf('The cubed root of %g is approximately %.3f',num,est)

採用された回答

David Hill
David Hill 2020 年 11 月 17 日
num = input('Enter a number you would like to find the cubed root of: ');
est = input('Enter your initial estimate: ');
err = abs(num - est^3);
k = 2;
while err > 1e-9
est(k) = (1/3)*(2*est(k-1)+num./(est(k-1)^2));
err = abs(num - est(k)^3);
k = k + 1;
end
plot(1:length(est),est);
fprintf('The cubed root of %g is approximately %.3f',num,est(end));
  1 件のコメント
Austin Shipley
Austin Shipley 2020 年 11 月 17 日
That worked perfectly! Thank you so much! I never really thought to tweak the array indices since that was a provided formula in my homework.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by