POV-Ray : Newsgroups : povray.off-topic : I think I did something wrong with RK4... Server Time
1 Oct 2024 11:28:46 EDT (-0400)
  I think I did something wrong with RK4... (Message 11 to 20 of 22)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 2 Messages >>>
From: John VanSickle
Subject: Re: I think I did something wrong with RK4...
Date: 8 Apr 2008 07:41:41
Message: <47fb59f5@news.povray.org>
Chambers wrote:
> John VanSickle wrote:
>> The last time I did a physics simulator (springs and weights), I found 
>> that calculating the change in acceleration, and figuring that into 
>> the new location and velocity data, greatly reduced the error that 
>> accumulates.
> 
> That's why I'm calculating forces :)

Well, yeah.  But whereas force goes to the second derivative of location 
over time, it's also helpful to calculate the third derivative over 
time, and figure that into the updates for both position and velocity.

The differential calculus behind it isn't that hard, and provides a 
better solution than dampening to the wild oscillations produced by sims 
that only go to the second derivative over time.

Regards,
John


Post a reply to this message

From: Chambers
Subject: Re: I think I did something wrong with RK4...
Date: 12 Apr 2008 16:34:00
Message: <48011cb8@news.povray.org>
OK, so I did all of that, and I *still* have the problem of the forces 
in RK4 appearing to be a different scale from the forces using the 
Newtonian method.

Shouldn't I be able to just swap out the two, choosing Newtonian for 
speed, or RK4 for accuracy, and see relatively equal* results?


*Obviously not exactly equal, else there's no point.  And also 
obviously, the errors inherent in Newtonian will compound over time, 
meaning the two sets of results will diverge.

-- 
...Ben Chambers
www.pacificwebguy.com


Post a reply to this message

From: scott
Subject: Re: I think I did something wrong with RK4...
Date: 13 Apr 2008 04:39:04
Message: <4801c6a8$1@news.povray.org>
> OK, so I did all of that, and I *still* have the problem of the forces in 
> RK4 appearing to be a different scale from the forces using the Newtonian 
> method.
>
> Shouldn't I be able to just swap out the two, choosing Newtonian for 
> speed, or RK4 for accuracy, and see relatively equal* results?

Yes, if your time-step is small enough for Newton to work roughly correctly. 
Do you still get the same difference if you reduce the time step by a factor 
of 100?  If you do, then it's a coding error, if you don't then it's simply 
the fact the Newton is not doing very well at your previous time step 
amount.

Did you try a simple system first, eg constant force or very soft spring 
from one point?  With a constant force you should be able to calculate the 
exact velocity and position by hand and compare with what your algorithm 
gives out.


Post a reply to this message

From: Chambers
Subject: Re: I think I did something wrong with RK4...
Date: 13 Apr 2008 14:32:56
Message: <480251d8$1@news.povray.org>
scott wrote:
>> OK, so I did all of that, and I *still* have the problem of the forces 
>> in RK4 appearing to be a different scale from the forces using the 
>> Newtonian method.
>>
>> Shouldn't I be able to just swap out the two, choosing Newtonian for 
>> speed, or RK4 for accuracy, and see relatively equal* results?
> 
> Yes, if your time-step is small enough for Newton to work roughly 
> correctly. Do you still get the same difference if you reduce the time 
> step by a factor of 100?  If you do, then it's a coding error, if you 
> don't then it's simply the fact the Newton is not doing very well at 
> your previous time step amount.
> 
> Did you try a simple system first, eg constant force or very soft spring 
> from one point?  With a constant force you should be able to calculate 
> the exact velocity and position by hand and compare with what your 
> algorithm gives out.

OK, I "fixed" the timestep at 1/200th of a second, and removed all 
particle collisions - now the only force is that of gravity.

Using the two integrators, the particles follow roughly the same path, 
but using RK4 they appear to have an increased velocity - they follow 
the same path much more quickly.

Which means I've got an error somewhere in my code...

-- 
...Ben Chambers
www.pacificwebguy.com


Post a reply to this message

From: scott
Subject: Re: I think I did something wrong with RK4...
Date: 14 Apr 2008 03:24:17
Message: <480306a1@news.povray.org>
> OK, I "fixed" the timestep at 1/200th of a second, and removed all 
> particle collisions - now the only force is that of gravity.
>
> Using the two integrators, the particles follow roughly the same path, but 
> using RK4 they appear to have an increased velocity - they follow the same 
> path much more quickly.
>
> Which means I've got an error somewhere in my code...

It should be very easy to debug, simply print out (to a file?) the "state" 
and "derivative of state" information at each step of RK4 for one particle 
(that starts at zero velocity would make things simpler).  eg Just print out 
position and velocity in the gravity direciton.

Attached is a spreadsheet I made that matches up with the code I posted 
before, it shows you the values at each step for a constant acceleration, 
and compares the results with the Euler method and the exact solution. 
Hopefully you can spot the mistake in your code...


Post a reply to this message


Attachments:
Download 'rk4.xls.dat' (44 KB)

From: Chambers
Subject: Re: I think I did something wrong with RK4...
Date: 14 Apr 2008 11:01:56
Message: <480371e4$1@news.povray.org>
scott wrote:
> It should be very easy to debug, simply print out (to a file?) the 
> "state" and "derivative of state" information at each step of RK4 for 
> one particle (that starts at zero velocity would make things simpler).  
> eg Just print out position and velocity in the gravity direciton.

Thanks, I got it sorted out.  I still don't know what the error was, but 
switching from preallocating the arrays to allocating new ones every 
step & throwing them away when done solved the problem.

So there must have been some carryover somewhere that I missed.

Anyway, I'll post a binary soon.  I've been going through the forums for 
XNA studio, and apparently there's no easy way to distribute games for 
windows yet (the whole thing is a work in progress, and they're about to 
release yet another version of it).

-- 
...Ben Chambers
www.pacificwebguy.com


Post a reply to this message

From: Orchid XP v8
Subject: Re: I think I did something wrong with RK4...
Date: 14 Apr 2008 13:14:15
Message: <480390e7$1@news.povray.org>
Chambers wrote:

> Thanks, I got it sorted out.  I still don't know what the error was, but 
> switching from preallocating the arrays to allocating new ones every 
> step & throwing them away when done solved the problem.
> 
> So there must have been some carryover somewhere that I missed.

Ah, the evils of mutable state...


...er, wait, ignore me. [Oh, you already are...]

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Darren New
Subject: Re: I think I did something wrong with RK4...
Date: 14 Apr 2008 17:20:44
Message: <4803caac@news.povray.org>
Orchid XP v8 wrote:
> Chambers wrote:
> 
>> Thanks, I got it sorted out.  I still don't know what the error was, 
>> but switching from preallocating the arrays to allocating new ones 
>> every step & throwing them away when done solved the problem.
>>
>> So there must have been some carryover somewhere that I missed.
> 
> Ah, the evils of mutable state...
> ....er, wait, ignore me. [Oh, you already are...]

You know, it's not all that hard to accidentally carry over incorrect 
state in a language without mutable state, too. :-)

-- 
   Darren New / San Diego, CA, USA (PST)
     "That's pretty. Where's that?"
          "It's the Age of Channelwood."
     "We should go there on vacation some time."


Post a reply to this message

From: Chambers
Subject: Re: I think I did something wrong with RK4...
Date: 14 Apr 2008 22:18:06
Message: <4804105e$1@news.povray.org>
Orchid XP v8 wrote:
> Chambers wrote:
> 
>> Thanks, I got it sorted out.  I still don't know what the error was, 
>> but switching from preallocating the arrays to allocating new ones 
>> every step & throwing them away when done solved the problem.
>>
>> So there must have been some carryover somewhere that I missed.
> 
> Ah, the evils of mutable state...
> 
> 
> ...er, wait, ignore me. [Oh, you already are...]
> 

I'm not ignoring you.  It was from reading all your posts on Haskell 
that I got interested in immutable values to begin with.  While I'm 
still not using them very much, at least now they're in the back of my 
mind :)

-- 
...Ben Chambers
www.pacificwebguy.com


Post a reply to this message

From: scott
Subject: Re: I think I did something wrong with RK4...
Date: 15 Apr 2008 03:44:05
Message: <48045cc5$1@news.povray.org>
> Thanks, I got it sorted out.  

Cool :-)


Post a reply to this message

<<< Previous 10 Messages Goto Latest 10 Messages Next 2 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.