Why is my code a matrix and not a double

4 vues (au cours des 30 derniers jours)
Zach Harrison
Zach Harrison le 16 Avr 2021
Commenté : David Fletcher le 16 Avr 2021
Why is E_n a 4x4 matrix? I want it to be a single value.
Initial Conditions:
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];
E = zeros(4,7);
error = 10;
i = 2;
j = 2;
while j <= 4
while i <= 7
if M(i,j) > -pi && M(i,j) < 0 || M(i,j) > pi
E_n = M(i,j) - e;
elseif M(i,j) < pi && M(i,j) > 0
E_n = M(i,j) + e;
elseif M(i,j) == 0 || M(i,j) == 180
E_n = M(i,j);
end
while error >= 10^-6
E_n_plus_1 = E_n + ((M(i,j) - E_n + e(j) * sin(E_n)) / (1 - e(j) * cos(E_n)));
E_n = E_n_plus_1;
error = E_n / E_n_plus_1;
end
E(i,j) = E_n_plus_1;
i = i + 1;
end
j = j + 1;
end

Réponses (2)

David Fletcher
David Fletcher le 16 Avr 2021
Modifié(e) : David Fletcher le 16 Avr 2021
The probable source of your error is the first two conditions in the if block - they result in E_n being a 4x1 vector since e is a 4x1 vector. Only the last condition in the if block will result in E_n being a scaler.
...
while j <= 4
while i <= 7
if M(i,j) > -pi && M(i,j) < 0 || M(i,j) > pi
E_n = M(i,j) - e; % <-------------------------------------------- 4x1
elseif M(i,j) < pi && M(i,j) > 0
E_n = M(i,j) + e; % <-------------------------------------------- 4x1
elseif M(i,j) == 0 || M(i,j) == 180
E_n = M(i,j); % Scaler
end
while error >= 10^-6
E_n_plus_1 = E_n + ((M(i,j) - E_n + e(j) * sin(E_n)) / (1 - e(j) * cos(E_n)));
E_n = E_n_plus_1;
error = E_n / E_n_plus_1;
end
E(i,j) = E_n_plus_1;
i = i + 1;
end
j = j + 1;
end

Robert Brown
Robert Brown le 16 Avr 2021
1 e = [0.01;
2 0.1;
3 0.5;
4 0.9];
5while j <= 4
6 while i <= 7
7 if M(i,j) > -pi && M(i,j) < 0 || M(i,j) > pi
8 E_n = M(i,j) - e;
9 elseif M(i,j) < pi && M(i,j) > 0
10 E_n = M(i,j) + e;
In lines 1-4, you define e as a 4x1 matrix
if you execute line 8 or line 10, E_n becomes a 4x1 matrix.
Your first loop executes line 10, and E_n = M(2,2) + e
>> M(2,2)
ans =
0.5236
e =
0.01
0.1
0.5
0.9
E_n =
0.5336
0.6236
1.0236
1.4236
So e, a 4x1 vector, automatically expands your answer (E_n) into a 4x1 vector
Your first loop executes line 10, and E_n = M(2,2) + e
Perhaps you wanted to use a single value from e, to keep E_n as a scalar value?
Did you want to add e(j) instead of just e?
  4 commentaires
Zach Harrison
Zach Harrison le 16 Avr 2021
Thank you, I believe that helped. But now I have a different problem so I updated the code and I posted another question. https://www.mathworks.com/matlabcentral/answers/804346-code-not-working-something-with-while-loop
David Fletcher
David Fletcher le 16 Avr 2021
You are not resetting your inner loop back to the initial i value - so the inner loop will only ever run the first time

Connectez-vous pour commenter.

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by