1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Write a program to simluate multi object

  1. Mar 20, 2008 #1
    1. The problem statement, all variables and given/known data
    i want to simulate the object in space with c++


    2. Relevant equations
    i use a very small time interval to calculate the force and velocity.
    i get wrong answer that the total energy in the system are increased.
    And the objects are on the wrong locus.

    Obj.Force = -g*m_1*m_2/(r*r);
    Obj.Velocity += Obj.Force/mass *time;

    3. The attempt at a solution
     
  2. jcsd
  3. Mar 20, 2008 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You didn't give details on what you did, so I am guessing a bit here.
    • How many objects do you have?
      You need to address all pairs of objects.
    • The gravitational force exerted on object 1 by object 2 is better written as
      [tex]\mathbf F = -\,\frac {G m_1 m_2}{||\mathbf r_{2\to1}||^3||} \mathbf r_{2\to1}[/tex]
    • Object 1 exerts an equal but opposite force on object 2.
      Do you have all masses moving in your simulation?
    • If you simply compute the position and velocity at some time [itex]t+\Delta t[/itex] as [itex]\mathbf r(t+\Delta t) = \mathbf r(t) + \Delta t \mathbf v(t)[/itex] and [itex]\mathbf v(t+\Delta t) = \mathbf v(t) + \Delta t \mathbf a(t)[/itex] where [itex]a(t)[/itex] is the acceleration per Newton's laws you will get bad results. This is called the Euler method for propagating a differential equation.
      Euler integration typically yields lousy results. You need to use a better integrator. Look into the verlet method, the velocity verlet (or Heun's method), and Runge-Kutta integration.
     
  4. Mar 20, 2008 #3
    Think you.
    I found those methods in web.
    i can't apply those method into my program
    Can anyone help me?
     
  5. Mar 21, 2008 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You need to supply information on what you have done yourself before we can help you any further. And why to you claim you can't apply those methods to your program? Did you try?
     
  6. Mar 21, 2008 #5
    [i]
    This is my program that corrected.
    i change the highlighted code. it is correct?

    [QUOTE]
    Physic::Vector3D Physic::Object::gForce(const Physic::Object & param)
    {
    long double r;
    Vector3D temp = this->delta(param);
    r = !temp;
    return temp.toNV()*gravityCon*this->mass*param.mass/(r*r*r);
    }[/QUOTE]
    temp.toNv() is return -(temp)[/i]
     
    Last edited: Mar 21, 2008
  7. Mar 21, 2008 #6

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    What kind of results do you get?

    Some comments:
    • As a general rule, it is a bad idea to use malloc in C++. Use new() instead.
    • Your code is hard to read. Use spaces.
    • What are you doing here: "r = !temp;"
     
  8. Mar 21, 2008 #7
    ! = return sqrt(x*x+y*y+ z*z)
    malloc is not good but i only found this method for undetemined size of array
     
  9. Mar 22, 2008 #8

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Write a program to simluate multi object
  1. Writing Lagrangian (Replies: 2)

Loading...