Reputation: 155
I'm trying to write a simple piece of code to define a half-sine pulse of a vibration shaker, to later transform into a shock response. I'm just trying to define a Sine function inside an interval, and outside that interval the function needs to be Zero:
Time = np.linspace(0,0.01,110)
Amplitude = 70
Period = 0.002
Function = 0.0
if Time.all() <= Period:
Function = Amplitude * np.sin(np.pi * Time/Period)
else:
Function = 0.0
The problem is that it looks that Python ignores the if statement, and plots me a full Sine function until for all the values of Time.
I'm sorry if this is a very newbie question, but I cannot find a solution to this.
Many thanks in advance.
Upvotes: 1
Views: 2078
Reputation: 5784
The output of Time.all()
is False
, how do you want to compare it with <= 0.002
? You either need to loop over your arrays (not recommended) or use a boolean index mask to apply zeros to your Function
:
Time = np.linspace(0, 0.01, 110)
Amplitude = 70
Period = 0.002
Function = 0.0
Function = Amplitude * np.sin(np.pi * Time / Period)
# apply setting to zero where the boolean mask is True
# the boolean mask is True, where the condition Time > Period is True
Function[Time > Period] = 0.
Besides that there are a few more "stylistic" issues in your code. According to PEP8 styleguide, only classes should start with uppercases. Variables should always be lowercase. This is the reason for your (probably) quite strange code colouring in your IDE.
Upvotes: 1