C++ Model of a charged particle in a magnetic field

In summary, the conversation discusses a research project in which the goal is to model a charged particle in a magnetic field using a C++ program. The individual has attempted to solve the problem by integrating via RK4 and using equations for force, velocity, and radius. However, they have encountered difficulties with the B field not changing the speed of the particle and have tried various methods, including using a constant velocity. The conversation ends with the individual expressing frustration and asking for help.
  • #1
Dissident
1
0

Homework Statement


Im taking my first physics class and We have to do a research project. The project can be anything we want. I decided to make a c++ program to model a charged particle in a magnetic field. When I first researched the problem I't seemed straight forward enough. The force on a particle in a magnetic field is givin by F = q(v X B). I thought I could solve for acceleration and then integrate via RK4 to produce a velocity for time t+delta t. Then using the previous velocity and the acceleration form the Lorentz force I would get the next position of the particle using x = v*t + 1/2 * a * t^2. I thought I could do this in a loop to generate a data file witch I could then graph using gnuplot.

The problem is that the force caused my the B field will not change the speed of the particle only the direction. When I integrate numerical the speed changes. If I try to normalize the new velocity and scale by the previous velocity the radius of the path does not line up with a the radius I calculate by hand. I have tried RK4 and the trapezoid method. I am now thinking about modeling the particle and a constant circular motion around a drift point, but I cannot for the life of me figure out how to model constant circular motion in three dimentions.

I have hit a wall and cannot get this to work.


Homework Equations


F = ma
F = qv X B
F = mv^2 / r
r = v^2 / a
r = mv / |q|B

The Attempt at a Solution



I don't think I should post 1000+ lines of code. Here is my latest attempt at integration. I am using the mpf_t form the GNU MP library which I have wrapped in a C++ class. hpv3d_t is a vector class that uses the wrapped mpf_t.
hpv3d_t k1, k2, k3, k4;
k1 = ((eField + velocity % bField) * chargeDivMass) * deltaTime;
k2 = ((eField + (velocity + k1 * 0.5) % bField) * chargeDivMass) * deltaTime;
k3 = ((eField + (velocity + k2 * 0.5) % bField) * chargeDivMass) * deltaTime;
k4 = ((eField + (velocity + k3) % bField) * chargeDivMass) * deltaTime;
velocity = velocity + (k1 + k2 * 2.0 + k3 * 2.0 + k4) * (deltaTime / 6);

the eField is <0,0,0> for now
 
Physics news on Phys.org
  • #2
and the bField is <0, 0, B>. I have tried using a constant velocity but that doesn't seem to work either.
 

Related to C++ Model of a charged particle in a magnetic field

What is the C++ Model of a charged particle in a magnetic field?

The C++ Model of a charged particle in a magnetic field is a programming code that simulates the behavior of a charged particle in a magnetic field. It uses the principles of C++ programming language to calculate the trajectory and velocity of a charged particle in a given magnetic field.

What are the main components of the C++ Model of a charged particle in a magnetic field?

The main components of the C++ Model of a charged particle in a magnetic field include the particle's mass, charge, initial position and velocity, the strength and direction of the magnetic field, and the time interval for which the simulation is run.

How accurate is the C++ Model of a charged particle in a magnetic field?

The accuracy of the C++ Model of a charged particle in a magnetic field depends on the quality of the input parameters and the precision of the calculations. With proper input and well-written code, the model can provide accurate results.

What are some real-world applications of the C++ Model of a charged particle in a magnetic field?

The C++ Model of a charged particle in a magnetic field has various practical applications, such as in particle accelerators, magnetic resonance imaging (MRI) machines, and research on plasma physics and fusion energy.

Is the C++ Model of a charged particle in a magnetic field suitable for educational purposes?

Yes, the C++ Model of a charged particle in a magnetic field can be used for educational purposes to help students understand the behavior of charged particles in a magnetic field and gain hands-on experience with programming and simulations.

Similar threads

  • Programming and Computer Science
2
Replies
36
Views
3K
  • Introductory Physics Homework Help
Replies
31
Views
1K
Replies
5
Views
388
  • Introductory Physics Homework Help
Replies
1
Views
940
  • Introductory Physics Homework Help
Replies
3
Views
821
  • Programming and Computer Science
Replies
15
Views
2K
  • Introductory Physics Homework Help
Replies
3
Views
767
  • Introductory Physics Homework Help
Replies
2
Views
1K
  • Introductory Physics Homework Help
Replies
12
Views
248
  • Introductory Physics Homework Help
Replies
12
Views
1K
Back
Top