I am interested in solving a problem that takes the following form. I solve, using some numerical method such as RK4, an ODE of the form
$$ r(v)L(v;\{a_i\})+r^{\prime\prime}(v)=0\,, $$
where $L(v;\{a_i\})$ is a function of $v$ and some parameters $\{a_i\}_{i=0,\ldots,N-1}$. I know the initial conditions $r(1)=1$ and $r^\prime(1)=0$.
For each set of $\{a_i\}$, I compute $r(v)$, together with an integral of the form
$$ Z(\{a_i\})=\int_0^1{\rm d}v\,r(v)^2B(v;\{a_i\})\,, $$
where, once again, $B(v;\{a_i\})$ depends on $v$ and on the $\{a_i\}$. I would like to find the $\{a_i\}$ that maximise $Z(\{a_i\})$, restricted to configurations for which $r(v)>0$. Any idea how to do this? I tried using the in-built routines NMaximize together with NDSolve, but could not find a way to tell NMaximize to restrict to solutions $r(v)$ for which $r(v)$ is positive. Ideally, I want to take $N$ to be rather large, like $N=100$.
For example, take $L(v;\{a_i\})=[\partial_v \Phi(v;\{a_i\})]^2$ and $B(v;\{a_i\})=\Phi(v;\{a_i\})^2$ with
$$ \Phi(v;\{a_i\})=\sum_{i=0}^{N-1}a_iv^i,\quad \text{where}\quad a_i\in\mathbb{R}\,. $$

