zivo
zivo

Reputation: 120

Sympy, How to get the solution of an ODE in a certain form

Let's say we have this ODE :

19.6f(t) + 8.0\frac{d^2}{dt^2}f(t)=0

The solution of this equation normally is

f(t)=C_1sin(7\sqrt{5}t/10)+C_2cos(7\sqrt{5}t/10)

Which is what sympy will give me obviously, though I need the solution in this form instead :

f(t)=Acos(7\sqrt{5}t/10 + \phi)

With the constants being A and \phi.

The goal of this is to study the dephasing of the system in question.

Upvotes: 1

Views: 90

Answers (2)

asmeurer
asmeurer

Reputation: 91500

In this case, C1 = -A*sin(phi) and C2 = A*cos(phi) (you can work this out by looking at the identity cos(x + y) = -sin(x)*sin(y) + cos(x)*cos(y)).

So to do the simplification, replace the constants:

In [19]: A, C1, C2, phi = symbols('A C1 C2 phi')

In [20]: dsolve(9.6*f(t) + 8.0*f(t).diff(t, t), f(t))
Out[20]:
             ⎛√30⋅t⎞         ⎛√30⋅t⎞
f(t) = C₁⋅sin⎜─────⎟ + C₂⋅cos⎜─────⎟
             ⎝  5  ⎠         ⎝  5  ⎠

In [21]: dsolve(9.6*f(t) + 8.0*f(t).diff(t, t), f(t)).subs({C1: -A*sin(phi), C2: A*cos(phi)})
Out[21]:
                     ⎛√30⋅t⎞               ⎛√30⋅t⎞
f(t) = - A⋅sin(φ)⋅sin⎜─────⎟ + A⋅cos(φ)⋅cos⎜─────⎟
                     ⎝  5  ⎠               ⎝  5  ⎠

In [22]: trigsimp(dsolve(9.6*f(t) + 8.0*f(t).diff(t, t), f(t)).subs({C1: -A*sin(phi), C2: A*cos(phi)}))
Out[22]:
            ⎛    √30⋅t⎞
f(t) = A⋅cos⎜φ + ─────⎟
            ⎝      5  ⎠

Upvotes: 1

serbap
serbap

Reputation: 93

In the end, solving

C_1*sin(x)+C_2cos(x) = A*cos(x + \phi),

if C_1 and C_2 are known is just a geometric problem. Start with

cos(x) = sin(x+\frac{\pi}{2}).

I suggest asking this question in https://mathoverflow.net/. If you ask, please link the question here. I am curious about the answer.

Upvotes: 0

Related Questions