Simulation of charged particle in matlab
29 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
simar
le 9 Juin 2011
Commenté : Walter Roberson
le 21 Jan 2019
I made this code but it does not give correct result probably due to quantisation error .. any suggestions how can i fix it??
% script that simulates a moving particle with some initial velocity in a
% magnetic field B
v0 = [5 0 0]; %initial velocity
B = [0 0 -5]; %magnitude of B
m = 5; % mass
q = 1; % charge on particle
r0 = [0 0 0]; % initial position of particle
t = 0;
% Now we want to find the next velocity as the particle enters the magnetic
% field and hence its new position
r = r0;
v = v0;
dt = 0.00000000000000000001;
figure
xlim([-25 25])
ylim([-25 25])
hold on
for n = 1:100
%plot it
plot(r(1),r(2),'*');
%pause
% update time
t = t+dt;
% new position r
dr = v*dt;
r = r + dr;
%find new velocity
dv = (q/m) * cross(v,B);
v = v + dv;
end
3 commentaires
Jan
le 10 Juin 2011
"Does not give correct results" is not a useful description of the problem. How could we know, what the correct result is to find the error?!
Anyhow, "dt = 0.00000000000000000001" is ridiculous small. Be aware than 1+dt==1!
Réponse acceptée
Andrew Newell
le 10 Juin 2011
There is nothing wrong with the physics. I think it is just the numerical stability of your code. The MATLAB ODE suite handles this much better:
v0 = [5 0 0.1]'; %initial velocity
B = [0 0 -5]'; %magnitude of B
m = 5; mass
q = 1; charge on particle
r0 = [0 -5 0]'; initial position of particle
tspan = [0 100];
The code below solves the system of equations
x' = vx
y' = vy
z' = vz
vx' = (q/m) (v x B)_x
vy' = (q/m) (v x B)_y
vz' = (q/m) (v x B)_z
The approach is to treat the position and velocity as independent variables.
y0 = [r0; v0];
f = @(t,y) [y(4:6); (q/m)*cross(y(4:6),B)];
[t,y] = ode45(f,tspan,y0);
plot3(y(:,1),y(:,2),y(:,3))
9 commentaires
Walter Roberson
le 16 Jan 2019
The second parameter to f, namely y, will be a column vector, so y(4:6) will be a column vector.
cross(y(4:6), B) will be a column vector.
E is a row vector. Row vector plus column vector was an error before R2016b, but as of R2016b started being treated similarly to bsxfun. So your code is like
f = @(t,y) [y(4:6); (q/m)*( bsxfun(@plus, E, cross(y(4:6),B) ))];
The 1 x 3 row vector plus 3 x 1 column vector will produce a 3 x 3 result.
You are then trying to use [;] between a 3 x 1 from y, and the 3 x 3 from the remainder of the computation. That is going to fail because of the mismatch on the number of columns.
Solution: change your definition to
E = [0 15000 0].' ;
Plus de réponses (6)
Ivan van der Kroon
le 10 Juin 2011
You probably want to have something like
fun=@(t,x) [x(4:6);cross(x(4:6),q/m*B)];
t=linspace(0,tend,1e3);
[t,x]=ode45(fun,t,[r0;v0]);
For the movie read about getframe:
doc getframe
Matt Tearle
le 10 Juin 2011
Without knowing the math behind what you're trying to simulate, I don't know if it's a problem in the way you've coded up the equations or if it's an issue of numerical implementation. But either way the culprit is in the line
dv = (q/m)*cross(v,B);
If you increase the number of loops or increase dt, you'll see that you get an outward spiral of points. If you keep a track of norm(dv), you'll see that this is because the magnitude of dv grows exponentially. So with even an absurdly small dt like you have, dr will eventually explode (and r with it). As a result, you'll see nothing for a while (a single point at the origin), then a very quick spiral outwards, before norm(r) just blows up to Inf.
IOW, you have a classic runtime error: MATLAB is doing exactly what you asked it to do. Just not, it seems, what you want it to do :)
5 commentaires
Andrew Newell
le 10 Juin 2011
The I Ching says it best:
Heaven is full of electrons
Spiralling round field lines.
Thus, the superior man studies
Maxwell's equations.
simar
le 16 Juin 2011
2 commentaires
Bjorn Gustavsson
le 16 Juin 2011
Calculation of particle trajectories in magnetic fields is a tricky problem. Even for your case with a constant B all but one of the ODE-solvers of matlab fails. I ran Andrew Newell's example but multiplied the end time with 100, then you'll see that there is variation of the gyro radius with time - and it shouldn't be.
One problem with particle motions in magnetic fields is that the particle energy should be conserved - for your case it is as simple as conservation of the kinetic energy:
(lets use "'" for time derivatives)
v' = q/m*cross(v,B) ->
dot(v,v') = q/m*dot(v,cross(v,B)) = 0,
since cross(v,B) is perpendiculat to B.
integrate once with respect to time and you get:
v^2/2 = Const.
The only ODE-solver that conserves that is ode23t, the others don't, some increase v^2 some decrease. Search for Störmer or Verlet integration for more information.
Andrew Newell
le 16 Juin 2011
The documentation for the ODE suite (http://www.mathworks.com/help/techdoc/ref/ode23.html) identifies the algorithm for each function.
Suraj Tamrakar
le 20 Juil 2017
Modifié(e) : Suraj Tamrakar
le 20 Juil 2017
Could someone help me clarify the math for the parallel and perp velocity component .
v_para = (dot(v,B)/norm(B))*(B/norm(B));
I don't quite get this formula used above. This is quite unfamiliar expression to me. Thank you
0 commentaires
Mathew Orman
le 21 Jan 2019
You have wrong physics and your simulation should show gain of kinectic energy while charge is accelerated by electric force field...
https://www.youtube.com/watch?v=lhldn0ef138&feature=youtu.be
1 commentaire
Walter Roberson
le 21 Jan 2019
Samir comments:
The reason for the increase in kinetic energy is numerical errors in the simulation. By the way, dynamo of my bicycle works just fine.
Voir également
Catégories
En savoir plus sur Numerical Integration and Differential Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!