2
$\begingroup$

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}]

phase vs t freq vs time

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}]

enter image description here

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.

$\endgroup$
0

2 Answers 2

1
$\begingroup$

A work-around I can think out:

Clear[a, b]
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]];
m1 = mm /. {a_, b_, c_} -> a;
m2 = mm /. {a_, b_, c_} -> b;

freq[t_] = D[ArcTan[m1[t], m2[t]], t];

Plot[freq[t], {t, 0, 20}, AxesOrigin -> {0, 0}]

enter image description here

If you have difficulty in understanding, check the InputForm of mm. Also, you may be interested in this and this post.

$\endgroup$
1
  • $\begingroup$ This makes a ton of sense - obvious now that I examine it. This was my problem all along. Unfortunately this link doesn't cover substitution rules for vectoral interpolating functions. In retrospect, however, it is obvious. Thank you very much. $\endgroup$ Commented Dec 18, 2014 at 5:54
1
$\begingroup$

I'm sure this is a duplicate but my search terms are not on target. The problem has nothing to do with NDSolve.

freq[t_] := D[Exp[t], t];
Plot[freq[t], {t, 0, 20}]

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. >>

These occur because freq[0.0004085714285714286]` evaluates to

D[Exp[0.0004085714285714286`], 0.0004085714285714286`]

and one cannot take the derivative with respect to 0.0004085714285714286`.

There are various workarounds. Here's another

freq[t0_] := D[Exp[\[FormalT]], \[FormalT]] /. \[FormalT] -> t0;
$\endgroup$
11
  • $\begingroup$ Where does the 0.0004085714285714286 come from? Where can I read about this? $\endgroup$ Commented Dec 17, 2014 at 14:15
  • $\begingroup$ @axsvl77 It is the first number that Plot plugs into freq[t]. If you search the site, you should be able to find related posts. Ignore the fact that I was unsuccessful at finding them. It's common issue with D, which I've seen several times on the site. $\endgroup$
    – Michael E2
    Commented Dec 17, 2014 at 14:19
  • $\begingroup$ @axsvl77 Found it: mathematica.stackexchange.com/questions/1301/… $\endgroup$
    – Michael E2
    Commented Dec 17, 2014 at 14:23
  • $\begingroup$ Well, I'm afraid this doesn't solve OP's problem. The true reason is that the mm outputs a list while it is one InterpolatingFunction, so list manipulations like First, Most etc. won't work anymore! $\endgroup$
    – xzczd
    Commented Dec 17, 2014 at 14:47
  • $\begingroup$ @xzczd I did not notice that part. I would say, however, that the true reason is for the General::ivar messages cited is what I point out. But solving it still leave the problem you point out. $\endgroup$
    – Michael E2
    Commented Dec 17, 2014 at 17:55

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