Hello!
I’ve encountered a problem with loss of pattern information after using “function-producing” Derivative function.
Assume I have a scalar-valued function defined on vectors:
H[r_?VectorQ] := r.r
(evidently, r is assumed to be a three-dimentional vector, but id does not matter)
And then I would like to solve a differential equation like dr / dt == grad(H). Note that I would like to keep the vectorial notation i.e. the solution must be a vector-valued function. I’ve tried the following:
NDSolveValue[{D[r[t], t] == Table[Derivative[xspec][H][r[t]], {xspec, IdentityMatrix[3]}],
r[0] == {1, 1, 1}}, r, {t, 0, 100}]
This returns an interpolated function, but it does not handle the derivatives of H properly. For example, if in the output of Derivative appeared term like #1[[1]] then it accepts r[t] as argument and evaluates simply to t and if there’s #1[[2]] it throws a message that this part does not exist. It seems like Derivative loses argument check and the returned function does not formally require vectorial input anymore.
So, the question is: can I somehow tell WL that this derivative is still a function of a vector?
Hello Nikolay,
maybe one should give the system a definition of r[t] first; I would do it like so:
h[r_?VectorQ] := r.r
r[t_] := {\[FormalX][t], \[FormalY][t], \[FormalZ][t]}
NDSolveValue[{D[r[t], t] == Grad[h[r[t]], r[t]], r[0] == {1, 1, 1}}, r[t], {t, 0, 100}]
Then you still can write you differential equation in terms of r. Does that meet your requirements?
Hello!
Thank you for your answer - the concept of formal parameters is new for me and I shall study it more carefully. And yes, it is a sort of solution though not precisely what I sought. Thank you once again!
P.S. My initial goal was actually to obtain a solution in form of one InterpolatingFunction object which would accept t as a variable and return a point in 3D space. But it looks I’d better deal with three interpolations in a list.
Something like this?
sol = NDSolveValue[{D[r[t], t] == D[r[t]^2, r[t]], r[0] == {1, 1, 1}},
r, {t, 0, 1}]
sol[1]
Thanks for your answer! Unfortunately, this is not a suitable solution since particular form of H is not restricted to squared norm of r[t] - I used it just as an example of scalar-valued function of vector argument. The problem is exactly that I want to create a general solver which will automatically handle any choice of H[r_?VectorQ].
You can do a lot this way:
sol = NDSolveValue[{D[r[t], t] ==
(r[t].r[t])^-1 RotationMatrix[Pi/2, {0, 0, 1}].r[t],
r[0] == {1, 1, 1}}, r, {t, 0, 1}]
sol[1]