Numerical second order pde solver

Click For Summary
SUMMARY

The discussion centers on implementing a numerical second-order partial differential equation (PDE) solver using WPF with C# and XAML. The user is attempting to simulate a particle's motion in a gravitational field, but encounters issues where the particle moves in a straight line instead of responding to the applied forces. Key components of the code include the calculation of velocity and position using acceleration functions defined for each spatial dimension. The user plans to implement the Runge-Kutta 4th order (RK4) method for improved accuracy.

PREREQUISITES
  • Understanding of numerical methods for solving differential equations
  • Familiarity with WPF (Windows Presentation Foundation) in C#
  • Knowledge of vector mathematics and physics, particularly in the context of motion and forces
  • Experience with debugging techniques in C# to inspect variable states
NEXT STEPS
  • Implement the Runge-Kutta 4th order (RK4) method for more accurate particle motion simulation
  • Learn about debugging techniques in Visual Studio to inspect variable states during execution
  • Research numerical integration methods such as the trapezoidal rule for comparison
  • Explore optimization techniques for performance improvements in WPF applications
USEFUL FOR

Developers working on physics simulations, particularly those using C# and WPF, as well as anyone interested in numerical methods for solving differential equations.

DivergentSpectrum
Messages
149
Reaction score
15
Edit:whoops wrong forum mods please move

2nd edit: I just had dinner then got back on the computer, input some points and saw a beautiful elipse.(complete with a fascinating flower petal design due to inaccuracies) Weird lol! No idea why it wasnt working before Now to implement RK4 bwahahaha

im writing a program in WPF(an amalgam of c# and xaml) that tracks the position of a particle after a certain amount of time.
the particle is placed in a force field, where force is a function of position and velocity.
I used a crude numerical method, which seemed pretty obvious to me but for some reason it won't work. the particle appears to travel in a straight line as if there was no force.

the number of increments and the final time to be calculated for is decided by the user, and the variable dt is calculated from that.
vx,vy,vz are the velocity vector
ax,ay,az are functions, and also the acceleration vector.

heres the relevant code. I am pretty sure the problem is in the first block.



Code:
      while (i < n - 1)
        {
    

            vx[i+1] = vx[i] + dt * ax(x[i], y[i], z[i], vx[i], vy[i], vz[i]);
            vy[i + 1] = vy[i] + dt * ay(x[i], y[i], z[i], vx[i], vy[i], vz[i]);
            vz[i + 1] = vz[i] + dt * az(x[i], y[i], z[i], vx[i], vy[i], vz[i]);


            x[i + 1] = x[i] + vx[i + 1] * dt;
            y[i + 1] = y[i] + vy[i + 1] * dt;
            z[i + 1] = z[i] + vz[i + 1] * dt;

            

            i++;
        }



for verifiability, I am using a gravitational field, also for simplicity i have eliminated force from the equation and I am working with acceleration alone(ill add that later)
here i have methods define the acceleration vectors:

Code:
     //acceleration vectors x componentvvvvvvv
        static double ax(double x, double y, double z, double vx, double vy, double vz)
        {
            double ax =  10000*(-x / Math.Pow(Math.Sqrt(x * x + y * y + z * z), 3));
            return ax;
        }
        //acceleration vectors x component^^^^^^


        //acceleration vectors y componentvvvvvvv
        static double ay(double x, double y, double z, double vx, double vy, double vz)
        {
            double ay =  10000*(-y / Math.Pow(Math.Sqrt(x * x + y * y + z * z), 3));
            return ay;
        }
        //acceleration vectors y component^^^^^^

        //acceleration vectors z componentvvvvvvv
        static double az(double x, double y, double z, double vx, double vy, double vz)
        {
            double az =  10000*(-z / Math.Pow(Math.Sqrt(x * x + y * y + z * z), 3));
            return az;

        }
   //acceleration vectors z component^^^^^^
 
Last edited:
Technology news on Phys.org
If you don't want to do RK4, you could try one trapezoidal corrector step:

Code:
    vx[i+1] = vx[i] + dt * ax(x[i], y[i], z[i], vx[i], vy[i], vz[i]);
    /* ... */
     x[i+1] =  x[i] + dt * vx[i];
    /* ... */
    vx[i+1] = vx[i] + .5 * dt * (ax(x[i], y[i], z[i], vx[i], vy[i], vz[i]) +
              ax(x[i+1], y[i+1], z[i+1], vx[i+1], vy[i+1], vz[i+1]));
     x[i+1] =  x[i] + .5 *dt * (vx[i]  + vx[i+1]);

update - see that you plan on using rk4, so no need to implement the corrector step method.
 
Last edited:
The best way to find the problem this would be to print out all the variables for one time step (or inspect them with your IDE's debugger, if you can believe what it's telling you!) and check the values with a calculator.

The code looks sensible, but if this is a snippet from a system that is built with tools that many people here have probably never used, the cause could be almost anything.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
14
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K