# Optimization with a constraint given by a differential equation

I have the following differential equation $$\ddot\theta(t) = -k\sin{\omega t}\sin{\theta(t)} \quad \text{where} \quad \theta(0)=\theta_0, \dot\theta(0)=v_0$$ where $\omega$ is a known constant and $k$ is a positive parameter, the optimal value of which I am trying to find out. Here is what I mean by the optimal value of $k$: $$k_{optimal} = \arg \max_k \frac{1}{T}\int_0^T|\dot\theta(t)|dt$$ where $T > 0$ is fixed. I am not sure if it makes sense to let $T \rightarrow \infty$ (I don't know if the integral would have a limit) but if possible, this should be the way to go rather than a finite $T$.

I tried the following approach. First I fix $T$ and I use the objective function $$\frac{1}{T}\int_0^T\dot\theta(t)^2dt$$ instead of the one with the absolute value. I take the partial derivative with respect to $k$ to obtain $$\frac{\partial}{\partial k}\left(\frac{1}{T}\int_0^T\dot\theta(t)^2dt\right) = \frac{2}{T}\int_0^T\dot\theta(t)\frac{\partial \dot\theta}{\partial k}dt$$ To calculate $\frac{\partial \dot\theta}{\partial k}$ I make use of the differential equation constraint. Namely,

$$\frac{\partial }{\partial t}\left(\frac{\partial \dot\theta}{\partial k}\right) = \frac{\partial}{\partial k}\left(\frac{\partial \dot\theta}{\partial t}\right)=\frac{\partial }{\partial k}\ddot\theta$$

Even though $\frac{\partial }{\partial k}\ddot\theta$ seemed nice in the beginning, I realized I have to include the term $\frac{\partial }{\partial k}\theta$, which messes things up. This is more or less where I am stuck. Any help/pointer would be appreciated.

I don't know if this could lead to somewhere:

$$\ddot\theta(t) = -k\sin{\omega t}\sin{\theta(t)}$$ Multiply by $\dot\theta$:

$$\ddot\theta(t)\dot\theta(t) = -k\sin{\omega t}\sin{\theta(t)}\dot\theta(t)$$

Which is:

$$\frac{1}{2}\dfrac{d}{dt}\left(\dot\theta^2\right)=-k\sin{\omega t}\dfrac{d}{dt}\left(\cos{\theta}\right)$$

$$\frac{1}{2}\dfrac{d}{dt}\left(\dot\theta^2\right)+k\sin{\omega t}\dfrac{d}{dt}\left(\cos{\theta}\right)=0$$

We add the term $k\omega\cos{(\omega t)\cos\theta}$:

$$\frac{1}{2}\dfrac{d}{dt}\left(\dot\theta^2\right)+k\sin{\omega t}\dfrac{d}{dt}\left(\cos{\theta}\right)+k\omega\cos{(\omega t)\cos\theta}=k\omega\cos{(\omega t)\cos\theta}$$

Which is:

$$\frac{1}{2}\dfrac{d}{dt}\left(\dot\theta^2\right)+\dfrac{d}{dt}\left(k\cos{(\omega t)\cos\theta}\right)=k\omega\cos{(\omega t)\cos\theta}$$

Reordering:

$$\dfrac{d}{dt}\left(\frac{1}{2}\dot\theta^2+k\cos{(\omega t)\cos\theta}\right)=k\omega\cos{(\omega t)\cos\theta}$$

Integrate:

$$\frac{1}{2}\dot\theta^2+k\cos{(\omega t)}\cos{\theta}-\frac{1}{2}v_0^2-k\cos{\theta_0}=\int_0^t k\omega\cos{\omega s}\theta(s)ds$$