# Introduction to Vectors and NDSolve

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 == 1, mx == 0}
sol1 = NDSolve[eqn, {mx[t], my[t]}, {t, 0, 20}]
mxx[t_] = mx[t] /. sol1[];
myy[t_] = my[t] /. sol1[];
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 == {1, 0, 0}}
sol1 = NDSolve[eqn, {m[t]}, {t, 0, 10}]
mm[t] = m[t] /. sol1[];
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?

• Try defining mm this way: mm[t_] = m[t] /. sol1[] and compare mm[t] with mm. (An InterpolatingFunction evaluates only on a numeric input.) Dec 5, 2014 at 21:17

Example using vector notation:

s = NDSolve[{m'[t] == Cross[m[t], {0, 0, 1}], m == {0, 1, 0}},  m, {t, 0, 1}]
Plot[Evaluate[m[t] /. s], {t, 0, 1}] s = NDSolve[{m'[t] == Cross[m[t], {Cos@t, Sin@t, 1}], m == {1, 1, 0}}, m, {t, 0, 10}];
ParametricPlot3D[m[t] /. s, {t, 0, 10}] • thanks, this and the hundreds of other comments I've been reading in the past few weeks helped me understand this useful software. Dec 6, 2014 at 2:38
• 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? Dec 6, 2014 at 16:23
• @axsvl77 You mean the trajectory? tray2D[t_] := m[t] /. s[] // Most; ParametricPlot[tray2D[t], {t, 0, 1}] Dec 6, 2014 at 16:31
• Thanks! Then tray1D[t_] := m[t]/.s[] // First to get just one D. Dec 6, 2014 at 16:47
• @axsvl77 Yep, that's it Dec 6, 2014 at 17:09