Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Simulation of charged particle in static magnetci field

Subject: Simulation of charged particle in static magnetci field

From: simar mohar

Date: 9 Jun, 2011 09:03:05

Message: 1 of 2

I made this code but it does not give correct result probably due to quantisation error .. any suggestions how can i fix it??

I want to have a simulation if this kind http://www.youtube.com/watch?v=a2_wUDBl-g8

% 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

Subject: Simulation of charged particle in static magnetci field

From: Roger Stafford

Date: 9 Jun, 2011 17:40:05

Message: 2 of 2

"simar mohar" <simar.i3r@gmail.com> wrote in message <isq289$fe7$1@newscl01ah.mathworks.com>...
> I made this code but it does not give correct result probably due to quantisation error .. any suggestions how can i fix it??
>
> I want to have a simulation if this kind http://www.youtube.com/watch?v=a2_wUDBl-g8
>
> % 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
- - - - - - - - -
  Your computation of 'dv' is in error in my opinion. You have equated the particle acceleration from the electric field to its change in velocity. It ought to involve 'dt' - the change in velocity equals acceleration multiplied by the time elapsed.

  When you make that correction, with the values you have given to the initial velocity, the magnetic field, the charge, and the mass, you will have to wait an extremely long period of time to observe any appreciable change of velocity - perhaps several million years. I would suggest a serious review of both the mass and the charge parameters as well as 'dt' that would be suitable.

  Also you should seriously consider using matlab's 'ode' functions. After all, you are dealing with an ordinary differential equation here, and that is what they are very good at solving.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us