I'm trying to simulate a particle in an electric and magnetic fields, but numerically instead of analytically. This is basically solving the equation
q⋅(p′×B)+q⋅E=mp″,
where p(t) is the position in (x,y,z) coordinates.
After viewing a few topics on this site, I've got a good idea on how to get the solution using NDSolve
, but my program gets stuck, and doesn't come up with anything.
b = {1, 0, 0};
e = {0, 0, 1};
q = 1;
m = 1;
sol = NDSolve[ {q*e + q*Cross[D[pos[t], t], b] == m D[pos[t], {t, 2}],
pos[0] == {0, 0, 0}, (D[pos[t], t] /. t -> 0) == {0, 0, 0}},
pos, {t, 0, 1}];
ParametricPlot3D[Evaluate[pos[t] /. sol], {t, 0, 1}];
It is also worth mentioning that if you remove the qâ‹…E term, the calculation is finished, but nothing shows up in the plot.
Answer
The main problem is that your pos
is not seen as a 3D vector.
The cross product is therefore interpreted as a scalar:
q*Cross[D[pos[t], t], b]
when adding this to the vector q.e this 'scalar' term is added to each of the vector components:
q*e + q*Cross[D[pos[t], t], b]
This won't work, instead do:
b = {1, 0, 0};
e = {0, 0, 1};
q = 1;
m = 1;
Define pos
as a 3D vector. Also take more time than a single second:
ClearAll[pos]
pos[t_] = {px[t], py[t], pz[t]};
sol = NDSolve[
{
q*e + q*Cross[D[pos[t], t], b] == m D[pos[t], {t, 2}],
pos[0] == {0, 0, 0},
(D[pos[t], t] /. t -> 0) == {0, 0, 0}
}, pos[t], {t, 0, 20}]
ParametricPlot3D[Evaluate[pos[t] /. sol], {t, 0, 20}]
Comments
Post a Comment