Save values in a matrix using a non-integer index

조회 수: 1 (최근 30일)
Theodoros Pardalakis
Theodoros Pardalakis 2016년 2월 28일
댓글: Theodoros Pardalakis 2016년 3월 1일
Hello all! i have a major problem in matlab and although i read all the answers couldn't solve this. my code is:
%Moments calculation along beam
%Vertical forces
Oy = 1260.1; %[KN]
N1 = 210.5; %[KN]
N2 = 252.8; %[KN]
N3 = 289; %[KN]
N4 = 316.8; %[KN]
N5 = 191; %[KN]
%Position where the forces are implemented
x1 = 16.68; %[m]
x2 = 16.78; %[m]
x3 = 17.89; %[m]
x4 = 16.93; %[m]
x5 = 17.15; %[m]
x6 = 0.936; %[m]
%Moment at start position
Mo = -64758.212; %[KNm]
%Calculation
for x=0:0.1:85.43
if x == 0
M = Mo;
elseif x <= x1
M = Mo + Oy*x;
elseif x1 < x <= (x1+x2)
M = Mo + Oy * x - N1*x1;
elseif (x1+x2) < x <= (x1+x2+x3)
M = Mo + Oy*x - N1*(x1+x2) - N2*x2;
elseif (x1+x2+x3) < x <= (x1+x2+x3+x4)
M = Mo + Oy*x - N1*(x1+x2+x3) - N2*(x2+x3) - N3*x3;
elseif (x1+x2+x3+x4) < x <= (x1+x2+x3+x4+x5)
M = Mo + Oy*x - N1*(x1+x2+x3+x4) - N2*(x2+x3+x4) - N3*(x3+x4) - N4*x4;
end
end
i want to save all the moments (M) in a matrix in order to use it in another equation but M(x) is incorrect as x is a non-integer number. Any help would be really appreciated!!!!
  댓글 수: 4
Steven Lord
Steven Lord 2016년 3월 1일
FYI this doesn't do what you think it does.
x1 < x <= (x1+x2)
  • If x1+x2 is greater than or equal to 1 this is always true.
  • If x1+x2 is greater than or equal to 0 but less than 1, this is ~(x1 < x).
  • If x1+x2 is less than 0 this is always false.
You need to use something like this to do what you want:
(x1 < x) & (x <= (x1+x2))
Theodoros Pardalakis
Theodoros Pardalakis 2016년 3월 1일
Steven Lord THANK YOU!!!!!!!!!!!!!!! i hadn't see your answer and that was the problem for getting wrong values. YOU SAVED ME! i really appreciate this. THANK YOU

댓글을 달려면 로그인하십시오.

채택된 답변

the cyclist
the cyclist 2016년 2월 29일
편집: the cyclist 2016년 2월 29일
Here's one way:
%Calculation
x = 0:0.1:85.43;
NX = numel(x);
M = zeros(NX,1);
for xi = 1:NX
if x(xi) == 0
M(xi) = Mo;
elseif x(xi) <= x1
M(xi) = Mo + Oy*x(xi);
elseif x1 < x(xi) <= (x1+x2)
M(xi) = Mo + Oy * x(xi) - N1*x1;
elseif (x1+x2) < x(xi) <= (x1+x2+x3)
M(xi) = Mo + Oy*x(xi) - N1*(x1+x2) - N2*x2;
elseif (x1+x2+x3) < x(xi) <= (x1+x2+x3+x4)
M(xi) = Mo + Oy*x(xi) - N1*(x1+x2+x3) - N2*(x2+x3) - N3*x3;
elseif (x1+x2+x3+x4) < x(xi) <= (x1+x2+x3+x4+x5)
M(xi) = Mo + Oy*x(xi) - N1*(x1+x2+x3+x4) - N2*(x2+x3+x4) - N3*(x3+x4) - N4*x4;
end
end
  댓글 수: 2
Theodoros Pardalakis
Theodoros Pardalakis 2016년 3월 1일
Thank you for the reply.. i tried something similar with this and works fine :)
the cyclist
the cyclist 2016년 3월 1일
Be sure to see Steven Lord's comment. I didn't notice that potential problem with your code.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

the cyclist
the cyclist 2016년 2월 29일
You can also do the whole calculation in a vectorized fashion, by replacing the conditional statement like this:
MM = Mo + (x<=x1).*Oy.*x - (x<=(x1+x2)).*N1*x1 ... % and so on

Theodoros Pardalakis
Theodoros Pardalakis 2016년 3월 1일
Hello again! After i run the code several times with various changes and trying all the solutions you all proposed i have another isue. Matlab gives me wrong results. By wrong i mean that if i solve each equation of the code by hand i get another result(the correct one). For example at this code the diagram of the moments shouldn't have positive values but Matlab gives me also positive values after a specific loop. can anyone help me please? Do i use the elseif correct? I have to progress my report and i am stuck at this so long....
%Moments calculation along beam %Vertical forces Oy = 1260.1; %[KN] N1 = 210.5; %[KN] N2 = 252.8; %[KN] N3 = 289; %[KN] N4 = 316.8; %[KN] N5 = 191; %[KN] %Position where the forces are implemented x1 = 16.68; %[m] x2 = 16.78; %[m] x3 = 17.89; %[m] x4 = 16.93; %[m] x5 = 17.15; %[m] x6 = 0.936; %[m] R1 = 16.68; R2 = 33.46; R3 = 51.35; R4 = 68.28; R5 = 85.43; %Moment at start position Mo = -64758.212; %[KNm] %Calculation x=0:0.01:85.43; for l=1:length(x) if x(l) == 0 M(l) = Mo; elseif 0 < x(l) <= R1 M(l) = Mo + Oy*x(l); elseif R1 < x(l) <= R2 M(l) = Mo + Oy*x(l) - N1*(x(l)-R1); elseif R2 < x(l) <= R3 M(l) = Mo + Oy*x(l) - N1*(x(l)-R1) - N2*(x(l)-R2); elseif R3 < x(l) <= R4 M(l) = Mo + Oy*x(l) - N1*(x(l)-R1) - N2*(x(l)-R2) - N3*(x(l)-R3); elseif R4 < x(l) <= R5 M(l) = Mo + Oy*x(l) - N1*(x(l)-R1) - N2*(x(l)-R2) - N3*(x(l)-R3) - N4*(x(l)-R4); end end
  댓글 수: 2
Theodoros Pardalakis
Theodoros Pardalakis 2016년 3월 1일
%Moments calculation along beam
%Vertical forces
Oy = 1260.1; %[KN]
N1 = 210.5; %[KN]
N2 = 252.8; %[KN]
N3 = 289; %[KN]
N4 = 316.8; %[KN]
N5 = 191; %[KN]
%Position where the forces are implemented
x1 = 16.68; %[m]
x2 = 16.78; %[m]
x3 = 17.89; %[m]
x4 = 16.93; %[m]
x5 = 17.15; %[m]
x6 = 0.936; %[m]
R1 = 16.68;
R2 = 33.46;
R3 = 51.35;
R4 = 68.28;
R5 = 85.43;
%Moment at start position
Mo = -64758.212; %[KNm]
%Calculation
x=0:0.01:85.43;
for l=1:length(x)
if x(l) == 0
M(l) = Mo;
elseif 0 < x(l) <= R1
M(l) = Mo + Oy*x(l);
elseif R1 < x(l) <= R2
M(l) = Mo + Oy*x(l) - N1*(x(l)-R1);
elseif R2 < x(l) <= R3
M(l) = Mo + Oy*x(l) - N1*(x(l)-R1) - N2*(x(l)-R2);
elseif R3 < x(l) <= R4
M(l) = Mo + Oy*x(l) - N1*(x(l)-R1) - N2*(x(l)-R2) - N3*(x(l)-R3);
elseif R4 < x(l) <= R5
M(l) = Mo + Oy*x(l) - N1*(x(l)-R1) - N2*(x(l)-R2) - N3*(x(l)-R3) - N4*(x(l)-R4);
end
end
the cyclist
the cyclist 2016년 3월 1일
Did you carefully read and understand Steven Lord's comment? It is almost certainly the case that your if-else statements are not doing what you think.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by