I need to find the instantaneous phase and instantaneous frequency for a rotating system. I am having difficulty making the math work with interpolating functions.
First, here is a problem without interpolating functions that I can make work:
phase[t_] := ArcTan[Cos[2*t + .1 t^2], Sin[2*t + .1 t^2]]
Plot[phase[t], {t, 0, 20}]
frequency = D[phase[t], t];
Plot[frequency, {t, 0, 20}, AxesOrigin -> {0, 0}]
In this first example, Mathematica is able to take the derivative of the phase to yield frequency. Now, in 3 dimensions with the Bloch equation:
eqn = {m'[t] == -2*Cross[m[t], {0, 0, 1}], m[0] == {1, 0, 0}};
sol1 = NDSolve[eqn, {m}, {t, 0, 20}] ;
mm[t_] := m[t] /. sol1[[1]];
phase[t_] := ArcTan[First[mm[t]], Last[Most[mm[t]]]];
Plot[phase[t], {t, 0, 20}]
freq[t_] := D[ArcTan[First[mm[t]], Last[Most[mm[t]]]], t];
Plot[freq[t], {t, 0, 20}]
This phase plot looks about the same as the first example. So why can't Mathematica take the derivative? Here is the error Mathematica spits out:
General::ivar: 0.0004085714285714286` is not a valid variable. >>
General::ivar: 0.4085718367346939` is not a valid variable. >>
General::ivar: 0.8167351020408163` is not a valid variable. >>
General::stop: Further output of General::ivar will be suppressed during this calculation. >>
I believe This question might be similar, and this and this both provided a lot of insight, but I've tried every variation I can think of, and can't figure out how to pass this through the derivative.
What to do next? Is it just my getting caught on the notation?
Edit: Original Question About Interpolating Functions Contained Set-Delay Errors; Expanding Question to Focus on Interpolated Function.
With this code, there are no errors. However, there is also no output:
eqn = {m'[t] == -2*Cross[m[t], {0, 0, 1}], m[0] == {1, 0, 0}};
sol1 = NDSolve[eqn, {m}, {t, 0, 20}] ;
mm = m /. sol1[[1]];
phase = ArcTan[First[mm], Last[Most[mm]]]
Plot[phase, {t, 0, 20}]
freq = D[ArcTan[First[mm]], Last[Most[mm]], t];
Plot[freq, {t, 0, 20}]
It is as though nothing happens. The plot thickens when I execute the next code; it shows that Mathematica can perform the the First
of the mm
and also the derivative of mm
. However, with this arrangement it can't perform ArcTan
.
eqn = {m'[t] == -2*Cross[m[t], {0, 0, 1}], m[0] == {1, 0, 0}};
sol1 = NDSolve[eqn, {m}, {t, 0, 20}] ;
mm[t_] = m[t] /. sol1[[1]];
Plot[First[mm[t]], {t, 0, 20}] (*This Plot Works*)
dmm = D[mm[t], t];
Plot[First[dmm], {t, 0, 20}](*This Plot Works*)
phase = ArcTan[First[mm[t]], Last[Most[mm[t]]]]
Last::nolast: InterpolatingFunction[{{0.,20.}},{4,3,1,{329},{4},0,0,0,0,Automatic},
{{0.,<<49>>,<<279>>}},{{{1.,0.,0.},{0.,2.,0.}},{{1.,0.000177626,0.},
{-0.000355252,2.,0.}},<<47>>,{{-0.972807,0.231619,0.},{-0.463238,-1.94561,0.}},
<<279>>},{Automatic}][] has a length of zero and no last element. >>
So my troubleshooting tells me that it is possible to take first of an interpolated function, and it is also possible to take the derivative of the interpolated function, but when it encounters them together inside the ArcTan, it isn't able to correctly call the correct direction of the interpolating function vector.