9

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?

Astor Florida
  • 1,326
  • 7
  • 21
  • 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.) – Michael E2 Dec 05 '14 at 21:17

1 Answers1

10

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

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453