if function not working

4 visualizaciones (últimos 30 días)
Cameron Aldred
Cameron Aldred el 24 de Abr. de 2017
Comentada: KL el 25 de Abr. de 2017
Hi, I'm trying to simulate an earthquake signal by adding 12 sine waves together (A,B,C...L). Each sine waves needs to start and stop at different times, and I assumed I can do this by creating an if function for each sine wave individually - however I cant get it to work properly. When I run the code and plot (t,earthquake) the graph shows nothing which suggests all the sine waves are zero the whole time :(
So I'm guessing there is a problem with the if functions?
Here is my code, would appreciate help if someone can tell me what's wrong with it. Thanks!!
close all;clc;
%%EARTHQUAKE = SUM OF 12 SINE WAVES A,B,C,D,E,F,G,H,I,J,K,L:
SCALER = 1.7;
%%INPUTS
%S = acceleration(g) T = period(s) start --------> end
Sa = 0.12; Ta = 0.1; A_start = 0; A_end = 22;
Sb = 0.15; Tb = 0.13; B_start = 1; B_end = 18;
Sc = 0.16; Tc = 0.14; C_start = 2; C_end = 16;
Sd = 0.165; Td = 0.15; D_start = 3; D_end = 15;
Se = 0.17; Te = 0.17; E_start = 4; E_end = 14;
Sf = 0.166; Tf = 0.2; F_start = 3; F_end = 19;
Sg = 0.14; Tg = 0.4; G_start = 2; G_end = 28;
Sh = 0.12; Th = 0.4; H_start = 1; H_end = 31;
Si = 0.1; Ti = 0.5; I_start = 0; I_end = 40;
Sj = 0.04; Tj = 1; J_start = 0; J_end = 20;
Sk = 0.025; Tk = 1.5; K_start = 0; K_end = 45;
Sl = 0.02; Tl = 2; L_start = 0; L_end = 45;
%%IMPLEMENTING SINE WAVE DURATIONS
if t>A_start
A = Sa*SCALER*sin(t* 2*pi/Ta);
elseif t>A_end
A = Sa*SCALER*sin(t* 2*pi/Ta);
else
A = 0;
end
if t>B_start
B = Sb*SCALER*sin(t* 2*pi/Tb);
elseif t>Bend
B = Sb*SCALER*sin(t* 2*pi/Tb);
else
B = 0;
end
if t>C_start
C = Sc*SCALER*sin(t* 2*pi/Tc);
elseif t>C_end
C = Sc*SCALER*sin(t* 2*pi/Tc);
else
C = 0;
end
if t>D_start
D = Sd*SCALER*sin(t* 2*pi/Td);
elseif t>D_end
D = Sd*SCALER*sin(t* 2*pi/Td);
else
D = 0;
end
if t>E_start
E = Se*SCALER*sin(t* 2*pi/Te);
elseif t>E_end
E = Se*SCALER*sin(t* 2*pi/Te);
else
E = 0;
end
if t>F_start
F = Sf*SCALER*sin(t* 2*pi/Tf);
elseif t>F_end
F = Sf*SCALER*sin(t* 2*pi/Tf);
else
F = 0;
end
if t>G_start
G = Sg*SCALER*sin(t* 2*pi/Tg);
elseif t>G_end
G = Sg*SCALER*sin(t* 2*pi/Tg);
else
G = 0;
end
if t>H_start
H = Sh*SCALER*sin(t* 2*pi/Th);
elseif t>H_end
H = Sh*SCALER*sin(t* 2*pi/Th);
else
H = 0;
end
if t>I_start
I = Si*SCALER*sin(t* 2*pi/Ti);
elseif t>I_end
I = Si*SCALER*sin(t* 2*pi/Ti);
else
I = 0;
end
if t>Jstart
J = Sj*SCALER*sin(t* 2*pi/Tj);
elseif t>J_end
J = Sj*SCALER*sin(t* 2*pi/Tj);
else
J = 0;
end
if t>K_start
K = Sk*SCALER*sin(t* 2*pi/Tk);
elseif t>K_end
K = Sk*SCALER*sin(t* 2*pi/Tk);
else
K = 0;
end
if t>L_start
L = Sl*SCALER*sin(t* 2*pi/Tl);
elseif t>L_end
L = Sl*SCALER*sin(t* 2*pi/Tl);
else
L = 0;
end
%%SUM & PLOT
earthquake = A+B+C+D+E+F+G+H+I+J+K+L;
plot(t,earthquake)
grid on
  1 comentario
Stephen23
Stephen23 el 24 de Abr. de 2017
There is no problem with if. You just did not read the documentation, in particular this line: "An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false"
Think about what that means if the expression is non-scalar: what does that mean if there are two elements (answer: they both have to be true).
In any case doing this using if statements is going to be pointlessly complicated: you should simply use indexing and vectorized code.

Iniciar sesión para comentar.

Respuesta aceptada

KL
KL el 24 de Abr. de 2017
Editada: KL el 24 de Abr. de 2017
Hi,
Assuming t is a vector, which it should be, you should use indexing to compare it in the if conditions, for example,
if t(i,1)>=A_start && t(i,1)<=A_end
% code
else
% code
end
what exactly is the difference between if and elseif in your code?
  4 comentarios
Stephen23
Stephen23 el 25 de Abr. de 2017
Editada: Stephen23 el 25 de Abr. de 2017
Or learn how to use MATLAB efficiently by using logical idnexing:
>> t = (0:0.01:2)';
>> A = zeros(size(t));
>> idx = t>=0 & t<=1;
>> A(idx) = 2*sin(2*pi*t(idx));
>> A(~idx) = 0;
Why waste your life writing ugly loops as if MATLAB was some ugly low-level language like C++ ?
KL
KL el 25 de Abr. de 2017
oh yeah, that's indeed the better way. Thanks Stephen.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Earthquake Engineering en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by