[MatLab] Motion of Particle in a spatially varying electromagnetic field

Click For Summary

Discussion Overview

The discussion revolves around simulating the motion of a particle in a spatially varying electromagnetic field using MatLab. Participants explore the challenges of solving second-order differential equations related to the particle's motion under the influence of electric and magnetic fields, and the effectiveness of their coding approaches.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant describes the setup involving electric and magnetic fields and presents the equations of motion for the particle.
  • Concerns are raised about MatLab's ability to solve second-order differential equations directly, leading to attempts to reformulate them into first-order systems.
  • Another participant suggests that MatLab can solve systems of first-order differential equations and discusses their approach to reformulating the equations.
  • There are mentions of issues with code performance, including slow execution and potential infinite loops during simulation.
  • A participant expresses a need for confirmation of their code's accuracy and offers to share it for review.
  • One participant indicates they have resolved their coding issues and suggests the thread could be closed.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of their approaches to solving the equations and the performance of their code. Some participants indicate they have found solutions, while others continue to seek assistance, suggesting that the discussion remains unresolved in terms of consensus on the best approach.

Contextual Notes

Participants have not fully clarified the assumptions behind their equations or the specific conditions under which their simulations operate. There are unresolved issues regarding the performance and reliability of the code implementations.

1Keenan
Messages
99
Reaction score
4
I have to simulate the motion of particle in an electromagnetic device which deflects particle using electric and magnetic field.
Those field are arranged in such a way that the electric field is inside the magnetic one. Moreover the electrodes begin at the center of the magnetic field ad their end is a bit outside the coils.
The motion equations are:
1) d^2x(z)/dz^2 = qE/mv^2
2) d^2y(z)/dz^2 = qB/mv

The problem is that MatLab cannot solve second order differential equation, thus I have tried to use the code:

Code:
%%Initial drift
rhsE1=@(z,x)[x(2); 0];
[zaE1, xaE1] = ode45(rhsE1, [0 69], [0 0]);
%%inside E field
  E=1;
  rhsE =@(z,x)[x(2); (q/m)*(E/v^2)];
  [zaE, xaE] = ode45(rhsE, [70 130], [0 0]);

For the drift after the field I have calculated the derivative of the trajectory and used it as initial condition for solving the eqaution of motion in the drift sector.
I have also write a similar code for the magnetic field.
The problem is that solving the equation separately doesn't seem to me correct because one solution could be longer than the other...

I have also tryed to solve the lorentz force using the code:
Code:
%%%%
f = @(t,y) [y(4:6); (q_over_m).*cross(y(4:6),B)+(q_over_m).*E];
[t,y] = ode23t(f,tspan,y0);
%%%

and some "while" loop like:

Code:
%%%
while (z>= length at which B begins ||  z<= length atwhich the electric field begins)
     B=[1 0 0]';
     E = [0 0 0]';
   solve the equation
  z=y(end,3);
end
%%%%%

and so on...

The problem is that the code is incredibly slow and sometimes, I think, it cannot get out of a loop.

I think I have used bad approaches to solve the problem, someone can give me a clue?
 
Physics news on Phys.org
more than 100 people have red this thread but I still get no answer.
Am I doing something wrong?
I really need some help because I am stuck with this simulation.
 
Matlab CAN however solve a system of first order diff eqns... if only there was a way to take a system of 2nd order diff eqns to a system of 1st order eqns...
 
AIR&SPACE said:
Matlab CAN however solve a system of first order diff eqns... if only there was a way to take a system of 2nd order diff eqns to a system of 1st order eqns...

Yes, that's what I did with the code:
Code:
%%%%
f = @(t,y) [y(4:6); (q_over_m).*cross(y(4:6),B)+(q_over_m).*E];
[t,y] = ode23t(f,tspan,y0);
%%%

It is supposed to solve the system
dx/dt=vx
dy/dt=vy
dz/dt=vz
dvx/dt= (Lorentz Force)x
...

I have found what was wrong in my code, it is able to track a particle all the way long from the source to the detector and inside the field... I hope...
The trajectory seems to be reasonable, I would need someone to double check it for confirmation.
Is there someone available to test the code for me?
I can post it here or send in private, doesn't matter.
 
Ok, I have done with the code.
If the moderators want to close this thread for me it is ok.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K