# DSolve running forever

I have no idea how to speed this up. Does maple do a better job at solving this?

• The code in the question runs immediately if you add Assumptions -> k/m > 0 to DSolve – Coolwater Nov 25 '17 at 20:56
• @Coolwater nice observation. M was stuck integrating this, since it was under sqrt and by saying k/m>0 bypasses possibility of complex numbers. – Nasser Nov 25 '17 at 21:01

I do not know what you mean here. When I run (with $\omega=\sqrt{k/m}$):

DSolve[{y''[t] + \[Omega]^2 y[t] == 1/m Sin[\[Omega] t],
y'[0] == 1, y[0] == 0}, y, t]


MMA (ver 11.2, macOS 10.13.1) provides this quite fast:

$\text{Function}\left[\{t\},\frac{4 m \omega \sin (t \omega )+2 \sin (t \omega )-2 t \omega \cos (t \omega )-2 \sin (t \omega ) \cos ^2(t \omega )+\sin (2 t \omega ) \cos (t \omega )}{4 m \omega ^2}\right]$

am I missing something?

• You are not missing anything. It is the sqrt which makes M not able to integrate it. Mathematica does not know vibration theory, so it did not know that natural frequency is sqrt(k/m). It is stuck trying to evaluate the integral with sqrt in it. Using w^2=k/m bypass this whole issue. Good workaround btw. – Nasser Nov 25 '17 at 20:58

It seems the problem is that Mathematica is having hard time doing the integration when finding the particular solution. Using answer here, you can see those integrals in the particular solution.

odeH = m*y''[t] + k y[t];
rhs = Sin[Sqrt[k/m] t];
{yh, yp} = hAndp[odeH, rhs, y, t];
solution = yh + yp


You can see these integrals are not evaluated, and I think this is where Mathematica is stuck.

Rubi can solve these, giving

int1=Int[Cos[(Sqrt[k] t)/Sqrt[m]] Sin[Sqrt[k/m] t],t];
int1E=(Limit[int1,t->z]-Limit[int1,t->0]);
int1E=int1E/.z->t


int2=Int[Sin[Sqrt[k/m] t] Sin[(Sqrt[k] t)/Sqrt[m]],t];
int2E=(Limit[int2,t->z]-Limit[int2,t->0]);
int2E=int2E/.z->t


Now you have the full solution after plugging the above

C[1]*Cos[(Sqrt[k]*t)/Sqrt[m]] + C[2]*Sin[(Sqrt[k]*t)/Sqrt[m]] +
((-Cos[(Sqrt[k]*t)/Sqrt[m]])*(Sin[(Sqrt[k/m] - Sqrt[k]/Sqrt[m])*t]/(2*(Sqrt[k/m] - Sqrt[k]/Sqrt[m])) -
Sin[(Sqrt[k/m] + Sqrt[k]/Sqrt[m])*t]/(2*(Sqrt[k/m] + Sqrt[k]/Sqrt[m]))) +
(1/(2*(Sqrt[k/m] - Sqrt[k]/Sqrt[m])) + 1/(2*(Sqrt[k/m] + Sqrt[k]/Sqrt[m])) - Cos[(Sqrt[k/m] - Sqrt[k]/Sqrt[m])*t]/
(2*(Sqrt[k/m] - Sqrt[k]/Sqrt[m])) - Cos[(Sqrt[k/m] + Sqrt[k]/Sqrt[m])*t]/(2*(Sqrt[k/m] + Sqrt[k]/Sqrt[m])))*
Sin[(Sqrt[k]*t)/Sqrt[m]])/(Sqrt[k]*Sqrt[m])


Now you can find C[1] and C[2] from initial conditions.

Btw, Here is Maple solution, which matches what I have above

Update

To see where DSolve was stuck, one can use this trace method (thanks to Michael E2) shown here how-does-mathematica-solve-a-certain-differential-equation

ClearAll[y,t,m,k]
ode=m y''[t]+ k y[t]==Sin[Sqrt[k/m] t];
ic={y[0]==0,y'[0]==1};
Block[{DSolveprint=Print},
Trace[DSolve[{ode,ic},y[t],t],_Integrate,
TraceInternal->True]
]
`

This shows it is stuck at this line

Here is full output