Reputation: 3109
These days I am trying to redo shock spectrum of single degree of freedom system using Sympy. The problem can reduce to find maximum value of a function. Following are two cases I cannot figure out how to do.
The first one is
tau,t,t_r,omega,p0=symbols('tau,t,t_r,omega,p0',positive=True)
h=expand(sin(omega*(t-tau)))
f=simplify(integrate(p0*tau/t_r*h,(tau,0,t_r))+integrate(p0*h,(tau,t_r,t)))
The final goal is to obtain maximum absolute value of f
(The variable is t
). The direct way is
df=diff(f,t)
sln=solve(simplify(df),t)
simplify(f.subs(t,sln[1]))
Here is the result, I tried many ways, but I can not simplify any further.
Therefore, I tried another way. Because I need the maximum absolute value and the location where abs(f)
is maximum happens at the same location of square of f
, we can calculate square of f
first.
df=expand_trig(diff(expand(f)**2,t))
sln=solve(df,t)
simplify(f.subs(t,sln[2]))
It seems the answer is almost the same, just in another form.
The expected answer is a sinc
function plus a constant as following:
Therefore, the question is how to get the final presentation.
The second one may be a little harder. The question can be reduced to find the maximum value of f=sin(pi*t/t_r)-T/2/t_r*sin(2*pi/T*t)
, in which t_r
and T
are two parameters. The maximum located at different peak when the ratio of t_r
and T
changes. And I do not find a way to solve it in Sympy. Any suggestion? The answer can be represented in following figure.
Upvotes: 6
Views: 12001
Reputation: 91580
The problem is the log(exp(I*omega*t_r/2))
term. SymPy is not reducing this to I*omega*t_r/2
. SymPy doesn't simplify this because in general, log(exp(x)) != x
, but rather log(exp(x)) = x + 2*pi*I*n
for some integer n
. But in this case, if you replace log(exp(I*omega*t_r/2))
with omega*t_r/2
or omega*t_r/2 + 2*pi*I*n
, it will be the same, because it will just add a 2*pi*I*n
inside the sin
.
I couldn't figure out any functions that force this simplification, but the easiest way is to just do a substitution:
In [18]: print(simplify(f.subs(t,sln[1]).subs(log(exp(I*omega*t_r/2)), I*omega*t_r/2)))
p0*(omega*t_r - 2*sin(omega*t_r/2))/(omega**2*t_r)
That looks like the answer you are looking for, except for the absolute value (I'm not sure where they should come from).
Upvotes: 5