How to change values from negative to positive and vicer versa in a loop
8 ビュー (過去 30 日間)
古いコメントを表示
Dear Everyone;
I have created a for loop to compute certain values. Within the loop if Mw, Mx, My and Mz comes out of as a postive number I want to change it to a negative; and if Mw, Mx, My and Mz comes out of as a negative number I want to change it to a positive.
I have tried to do this with "If Statements", but the set of values come out as all positive or all negative.
Here is my code:
clear, clc, close all
% Distribution factor
DF = [0.4444 0.5556 0.5714 0.4286];
% Starting bending moments
M1 = (-25 + 20)*DF(1);
M2 = (-25 + 20)*DF(2);
M3 = (-20 + 4.67)*DF(3);
M4 = (-20 + 4.67)*DF(4);
for i = 1:8
% carry over
cf1 = M1/2;
cf2 = M2/2;
cf3 = M3/2;
% First distribution
Mw(i) = (0 + cf3)*DF(1)
Mx(i) = (0 + cf3)*DF(2)
My(i) = (0 + cf2)*DF(3)
Mz(i) = (0 + cf2)*DF(4)
% If Mw, Mx, My and Mz is positive change to a negative value
if Mw(i) > 0
Mw(i) = -(Mw(i))
% If Mw, Mx, My and Mz is negative change to a positive value
elseif Mw(i) < 0
Mw(i) = abs(Mw(i))
end
M1 = Mw(i);
M2 = Mx(i);
M3 = My(i);
M4 = Mz(i);
As you can see I have only attempted this with Mw. This is just to practise. Once I get the correct results I will include Mx, My and Mz.
Can someone help please?
採用された回答
Voss
2025 年 3 月 16 日
clear, clc, close all
% Distribution factor
DF = [0.4444 0.5556 0.5714 0.4286];
% Starting bending moments
M1 = (-25 + 20)*DF(1);
M2 = (-25 + 20)*DF(2);
M3 = (-20 + 4.67)*DF(3);
M4 = (-20 + 4.67)*DF(4);
for i = 1:8
% carry over
cf1 = M1/2;
cf2 = M2/2;
cf3 = M3/2;
% First distribution
Mw(i) = -(0 + cf3)*DF(1);
Mx(i) = -(0 + cf3)*DF(2);
My(i) = -(0 + cf2)*DF(3);
Mz(i) = -(0 + cf2)*DF(4);
M1 = Mw(i);
M2 = Mx(i);
M3 = My(i);
M4 = Mz(i);
end
Mw
Mx
My
Mz
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!