How to change values from negative to positive and vicer versa in a loop
5 次查看(过去 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!