I'm trying to make a simulation of an electron that goes through a magnetic and electric field perpendicular to each other and to the velocity of the electron but I'm running in some troubles with the Mathematica language. I'm a beginner at Mathematica language and I ended up probably using too much procedural programming and I would like suggestions on how I can improve it using real Mathematica language. For those who saw this question before I edited it, the second problem was fixed and it was a simple logic mistake, so I took it of the question.
vel = {0.05*300000000, 0, 0}; (* velocity *)
pos = {0, 0, 0}; (* position *)
acel = {0, 0, 0}; (* acceleration *)
ce = {0, -5000, 0}; (* electric field *)
cm = {0, 0, -0.0003}; (* magnetic field *)
dt = 0.000000000001;
l = 0.05; (* length of the region where there are magnetic and electric fields *)
me = 9.10938*10^-31; (* electrons mass *)
q = -1.60217657*10^-19; (* elementary charge *)
data = Table[0, {i, 1000000}, {j, 2}];
For[count = 1, pos[[1]] <= 1.5*l, count++,
If[Abs[pos[[1]]] <= l, acel = q/me*(ce + (vel\[Cross]cm)), acel = {0, 0, 0}];
pos = pos + vel*dt + (acel*dt^2)/2; vel = vel + acel*dt;
data[[count, 1]] = pos[[1]]; data[[count, 2]] = pos[[2]]
];
For[r = 100000; s = 0.00001, r > 0.001, r = (r/10); s = (s/10),
While[Abs[vel[[2]]] > r, cm[[3]] = cm[[3]] - s;
vel = {0.05*300000000, 0, 0};
pos = {0, 0, 0};
data = Table[0, {i, 1000000}, {j, 2}];
For[count = 1, pos[[1]] <= 1.5*l, count++,
If[Abs[pos[[1]]] <= l, acel = q/me*(ce + (vel\[Cross]cm)), acel = {0, 0, 0}];
pos = pos + vel*dt + (acel*dt^2)/2; vel = vel + acel*dt;
data[[count, 1]] = pos[[1]]; data[[count, 2]] = pos[[2]]
];
];
];
I know that there are duplicate variable declarations and redundant statements in the code, its because I pasted everything together to post here, but it was originally apart.