Index exceeds matrix dimensions using for and while loop

clc
clear
close
%define variables
Re = [2500:1:500000];
n = numel (Re);
t(1)= 0;
e = 1;
f = 1;
for i = 1:n
g(i+1) = 4*log10(Re(i)*sqrt(f))-0.4-(1/sqrt(f));
while e > 0.000005
i = i+1;
t(i+1) = f(i) + (4*log10(Re*sqrt(f(i)))-0.4-(1/sqrt(f(i))))/((2/(f(i)*log(10)))+(1/(2*f(i)^(2/3))))
e = abs(t(i+1) - t(i))
end
end
It gives index exceeds matrix dimensions error. I checked each raw of code, but I cannot see it

2 件のコメント

Torsten
Torsten 2022 年 4 月 30 日
f(i) (at least for i>1) does not exist.
Onur ATALAY
Onur ATALAY 2022 年 5 月 1 日
what should it be?

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

回答 (1 件)

Anirudh Vajpeyi
Anirudh Vajpeyi 2022 年 4 月 30 日

0 投票

while e > 0.000005
i = i+1;
t(i+1) = f(i) + (4*log10(Re*sqrt(f(i)))-0.4-(1/sqrt(f(i))))/((2/(f(i)*log(10)))+(1/(2*f(i)^(2/3))))
e = abs(t(i+1) - t(i))
end
This loop is basically an infinite loop and also 'f' has just one value. So, not sure what exactly do you want to achieve.

2 件のコメント

Onur ATALAY
Onur ATALAY 2022 年 4 月 30 日
Develop a function that uses Newton Raphson methods to solve for f given a user-supplied value of Re between 2,500 and 500,000. Design the function so that it ensures that the absolute error in the result is 𝜖 < 0.000005.
Onur ATALAY
Onur ATALAY 2022 年 4 月 30 日
Exact equation 1/sqrt(f) = 4*log10(Re*sqrt(f))-0.4

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2022 年 4 月 30 日

コメント済み:

2022 年 5 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by