0
$\begingroup$

I am currently trying to write a code to implement the so-called shooting method (I've given a brief description of it below, for those unfamiliar) for second order ODE's in the form

$${\mathrm{d}^2y\over\mathrm{d}t^2} = f\left(t,y,{\mathrm{d}y\over\mathrm{d}t}\right),$$

which have given boundary conditions at $y(t_0) = y_0$ and $y(t_1) = y_1$ for $t_0 < t_1$.

Notice that we do not know $y'(t_0) = v_0$, so we make a "guess" at its value and see if it satisfies the second boundary condition. Hence we are "shooting" an arrow (our initial guess for velocity) and seeing if it hits the "target" (our second boundary condition.) If it doesn't, we adjust our aim (our initial guess) and try again."

We have a function, $y(t)$ given by the equation of motion, with a constant initial velocity $v_0$, and a different function $y_\text{shot}(t)$ which describes the trajectory of the same particle in a different situation (i.e. for different $v_0$.) Therefore, $y_\text{end}(t)$ describes the point at which $y(t_1)$ and $y_\text{shot}(t_1,v_0)$ have the same value at the same time, $t_1$. So, if we can find the point where both equations have the same height at the same time, we can find the initial velocity of both equations.

Mathematica code

My code is as follows:

ClearAll["Global`*"]

yShot[v0_, t0_, t1_, y0_, y1_] := 
  NDSolve[{y''[t] == f[t, y[t], y'[t]], y[t0] == y0, y'[t0] == v0}, 
   y, {t, t0, t1}];

(*Define function*)
f[t_, y_, v_] = -9.81 - 0.1 v;

(*Boundary conditions*)
t0 = 0;
t1 = 1;
y0 = 0;
y1 = 20;

v0guess = 30;

yend[v0_] := y[t1] /. yShot[v0, t0, t1, y0, y1][[1]]

Show[
 Plot[yend[v0], {v0, 0, v0guess}],
 Plot[y1, {v0, 0, v0guess}]
 ]

FindRoot[yend[v0] - y1 == 0, {v0, v0guess - 10, v0guess}]

which produces a grah showing the intersection of the two functions, as expected, and the following error message from both NDSolve and FindRoot:

NDSolve::ndinnt: Initial condition v0 is not a number or a rectangular
array of numbers. >>

$$\vdots$$

ReplaceAll::reps: {y''[t]==-9.81-0.1 y'[t],y[0]==0,y'[0]==v0} is
neither a list of replacement rules nor a valid dispatch table, and so
cannot be used for replacing. >>

$$\vdots$$

FindRoot::nlnum: "The function value of... is not a list of numbers
with dimensions {1} at {v0} = {0.`}. >>

Can anyone see why this error message is being produced? It only seems to become a problem when FindRoot is involved.

$\endgroup$
  • $\begingroup$ BTW: ParametricNDSolve[] would be a better choice for implementing shooting. $\endgroup$ – J. M. is away Dec 10 '15 at 7:44
1
$\begingroup$

This is a duplicate. You need to tell your functions that the arguments are numeric.

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.