フィルターのクリア

Code not working, something with while loop

1 回表示 (過去 30 日間)
Zach Harrison
Zach Harrison 2021 年 4 月 16 日
コメント済み: Zach Harrison 2021 年 4 月 16 日
I think the error in my code is in the while loop, I don't think i and/or j are increasing their value.
E_n is the value that will give my my answer. I want i and j to start at 1, but when I do, E_n is 0 for all values. Then when I change the starting conditions, only a few numbers are non-zero, and they are all the same value. This is why I think my while loops are not working, which leads to my code not checking every element in the matrix M.
M_0 = [0, 30, 90, 135, 180, 200, 350;
0, 30, 90, 135, 180, 200, 350;
0, 30, 90, 135, 180, 200, 350;
0, 30, 90, 135, 180, 200, 350];
M = M_0 .* (pi / 180);
e = [0.01;
0.1;
0.5;
0.9];
% Initial Calculations
E = zeros(4,7);
error = 10;
i = 1;
j = 1;
while j <= 4
while i <= 7
if M(j,i) > -pi && M(j,i) < 0
E_n = M(j,i) - e(j);
elseif M(j,i) > pi
E_n = M(j,i) - e(j);
elseif M(j,i) < pi && M(j,i) > 0
E_n = M(j,i) + e(j);
else
E_n = M(j,i);
end
while error >= 10^-6
E_n_plus_1 = E_n + ((M(j,i) - E_n + e(j) * sin(E_n)) / (1 - e(j) * cos(E_n)));
error = abs(E_n - E_n_plus_1);
E_n = E_n_plus_1;
end
E(j,i) = E_n_plus_1;
i = i + 1;
end
j = j + 1;
end
M_deg = reshape(M_0,28,1);
E_deg = reshape(E,28,1);
e1 = [0.01;0.01;0.01;0.01;0.01;0.01;0.01;
0.1;0.1;0.1;0.1;0.1;0.1;0.1;
0.5;0.5;0.5;0.5;0.5;0.5;0.5;
0.9;0.9;0.9;0.9;0.9;0.9;0.9];
table(M_deg,e1,E_deg)
  1 件のコメント
Zach Harrison
Zach Harrison 2021 年 4 月 16 日
編集済み: Zach Harrison 2021 年 4 月 16 日
Update: i and j values are 8 and 5 respectively and the end of the code so I guess that part works fine

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

採用された回答

David Fletcher
David Fletcher 2021 年 4 月 16 日
編集済み: David Fletcher 2021 年 4 月 16 日
The counter for the inner i loop never gets reset back to it's initial value, so it will only run on the first iteration of the j loop. You will incidentally have the same problem with the error loop, as that never gets re-initialised after the loop either
  3 件のコメント
David Fletcher
David Fletcher 2021 年 4 月 16 日
...and also the error variable sice it's that loop that updates the E_n values
Zach Harrison
Zach Harrison 2021 年 4 月 16 日
Thank you, that worked

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by