Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>You've expressed the initial equation incorrectly in your code in two important ways</strong>: </p> <p>First, note that the equation only expresses <strong>relative</strong> distortions; that is, in the equation, if <code>x[i]==x[i-1]==x[i+1]</code> then <code>x"[i]=0</code> regardless of how far <code>x[i]</code> is from zero. In your code, you're measuring the absolute distance from an equilibrium for each particle. That is, the equation fixes the row of oscillators only at the boundary, like a single spring held at the ends, but you're simulating a whole set of little springs, each fixed at some "equilibrium" point.</p> <p>Second, your terms like <code>osc[i].acc+=+k*(osc[i-1].loc-osc[i-1].bloc);</code> don't make much sense. Here you're setting <code>osc[i].acc</code> based solely on the <strong>absolute</strong> position of the particle next to it, and not on the <strong>relative</strong> position between the two.</p> <p>This second problem is probably why you're gaining energy, but that's only a side effect of doing an incorrect simulation, not necessarily implying that your simulation is producing errors. Currently there's no evidence that you need to change from the simple Euler simulation (as Nathan suggested). First get the equation correct, and then get fancy with the simulation method if you need to.</p> <p>Instead, write the equation for <strong>relative</strong> displacements, ie, with terms like <code>osc.loc[i]-osc.loc[i-1]</code>.</p> <p><strong>comment:</strong> I rarely like to comment on others' answers to a question I've answered, but both Nathan and ja72 are focusing on things that just aren't the main issue. <strong>First</strong> get your simulation equations correct, <strong>then</strong>, maybe, worry about fancier approaches than Euler, and the order of updating your terms, etc, if you ever need to. For a simple, linear, first order equation, especially with a little damping, the direct Euler method works fine if the time step is small enough, so get it working like this first.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload