9
$\begingroup$

I want to use NDSolve for a complicated equation that contains vectors. I am still new to Mathematica, and have really enjoyed what I have learned thus far. Concerning this question, I have already studied and tested a few examples here, and here, but am still unable to grasp some portion of the proper nomenclature solve my equation using vectors in Mathematica. As an example, this is the Bloch Equation:

$$\frac{\partial\mathbf{M}}{\partial t}=\gamma\left[\mathbf{M} \times \mathbf{B}\right] $$

Here, $\mathbf{M}$ and $\mathbf{B}$ are vectors in Cartesian coordinates, $\mathbf{B}=B_x\hat{i}+B_y\hat{j}+B_z\hat{k}$ and $\mathbf{M}=M_x\hat{i}+M_y\hat{j}+M_z\hat{k}$.

I worked out a simple example, without vectors, with $\mathbf{B}=\hat{k}$:

eqn = {mx'[t] == my[t], my'[t] == -mx[t], my[0] == 1, mx[0] == 0}
sol1 = NDSolve[eqn, {mx[t], my[t]}, {t, 0, 20}]
mxx[t_] = mx[t] /. sol1[[1]];
myy[t_] = my[t] /. sol1[[1]];
ParametricPlot[{mxx[t], myy[t]}, {t, 0, totaltime}]

And this works just fine, but I have Mathematica 9.0.1, so I should be able to do it with vectors without performing the cross product by hand and entering it in. So here I take a shot at it:

H[x_, y_, z_] := {x, y, z};
eqn = {m'[t] == Cross[m[t], H[0, 0, 1]], m[0] == {1, 0, 0}}
sol1 = NDSolve[eqn, {m[t]}, {t, 0, 10}]
mm[t] = m[t] /. sol1[[1]];
Length[mm[t]]

But for the life of me, can't figure out what to do next. I have tried to plot this in a bunch of ways, but nothing seems to give me anything. The Length of my vector is just one. I am uncertain if I am using vectors in NDSolve wrong, or something else?

$\endgroup$
1
  • $\begingroup$ Try defining mm this way: mm[t_] = m[t] /. sol1[[1]] and compare mm[t] with mm[0]. (An InterpolatingFunction evaluates only on a numeric input.) $\endgroup$
    – Michael E2
    Commented Dec 5, 2014 at 21:17

1 Answer 1

10
$\begingroup$

Example using vector notation:

s = NDSolve[{m'[t] == Cross[m[t], {0, 0, 1}], m[0] == {0, 1, 0}},  m, {t, 0, 1}]
Plot[Evaluate[m[t] /. s], {t, 0, 1}]

Mathematica graphics

s = NDSolve[{m'[t] == Cross[m[t], {Cos@t, Sin@t, 1}], m[0] == {1, 1, 0}}, m, {t, 0, 10}];
ParametricPlot3D[m[t] /. s, {t, 0, 10}]

Mathematica graphics

$\endgroup$
5
  • 1
    $\begingroup$ thanks, this and the hundreds of other comments I've been reading in the past few weeks helped me understand this useful software. $\endgroup$ Commented Dec 6, 2014 at 2:38
  • $\begingroup$ For the 2-D Plot above, how can I plot just the one direction vs time? For the example above, it would be $M_x(t)$, a sin wave. I can do it for a list, but is it possible for the interpolating function? $\endgroup$ Commented Dec 6, 2014 at 16:23
  • $\begingroup$ @axsvl77 You mean the trajectory? tray2D[t_] := m[t] /. s[[1]] // Most; ParametricPlot[tray2D[t], {t, 0, 1}] $\endgroup$ Commented Dec 6, 2014 at 16:31
  • $\begingroup$ Thanks! Then tray1D[t_] := m[t]/.s[[1]] // First to get just one D. $\endgroup$ Commented Dec 6, 2014 at 16:47
  • $\begingroup$ @axsvl77 Yep, that's it $\endgroup$ Commented Dec 6, 2014 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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