Say I have the following ODE: $\frac{d^2x}{dt^2} = k \cdot f(x,t)$ where $k$ is an unknown constant and I'm trying to solve for $k$ knowing that at $x = x_f \rightarrow \frac{dx}{dt} = v_f$. Also the initial conditions are: $t = 0 \rightarrow x = 0 \,\, \& \,\, \frac{dx}{dt} = 0$.
Furthermore, I'm attempting to solve this numerically rather than analytically.
So far I've come up with the following:
Let $\frac{d^2x}{dt^2} = \frac{dv}{dt}$ therefore we can now apply Euler's method (bear in mind that $\frac{dx}{dt} = v$):
$v_{n+1} = v_n + k \cdot f(x,y) \cdot \Delta t$
$x_{n+1} = x_n + v_n \cdot \Delta t$
$t_{n+1} = t_n + \Delta t$
where $\Delta t \approx 0$
The initial conditions have already been stated in the first paragraph. Having defined the recurrence relationship we can now iterate and plot a graph of $x(t)$. If I look at the value of $v$ at $x = x_f$ I clearly get a different result depending on the value of $k$. So the point is to find that value for $k$ such that when $x = x_f \rightarrow v = v_f$.
The "simple" solution would be trial and error, i.e, to try different values for $k$. If you exceed $v_f$ decrease $k$, otherwise, increase it and keep doing so until you're satisfied with a good enough number.
This method is the one I'm trying to avoid and I'm seeking for something a lot quicker and smarter. Any ideas?
Thanks
You can adopt Newton's method to find the value of $k$. Basically you have a function $v_{x_f}(k)$ which is the final velocity at $x_f$. You want to solve the equation $v_{x_f}f(k)-v_f=0$. Recall that Newton's method states that $$k_{n+1}=k_n-\frac{v_{x_f}f(k_n)}{v'_{x_f}f(k_n)}$$ Depending on $f$, you might be able to find the derivative in the denominator exactly; otherwise, you will have to approximate the derivative given $k_n$ and $k_n+\delta$ for a small $\delta$.