Solving differential equations using numeric methods

AI Thread Summary
In the discussion, a user is developing a movement system in Game Maker: Studio and encounters an unexpected behavior in their speed calculation. The code is designed to increase speed by 2 pixels per step when a key is pressed, while also reducing speed by a quarter of the current speed in each step. The user finds that this results in a maximum speed of 6 pixels per step, which contrasts with the expected top speed of 8 pixels per step when the entire differential equation is applied within the key press condition. The key issue identified is that the speed reduction occurs immediately after the speed increase, leading to a steady state of 6 pixels per step when the key is continuously pressed. Suggestions are made to restructure the code, potentially placing the speed reduction in an else clause, which would allow speed to increase when the key is pressed and decrease only when it is not pressed.
DarkBabylon
Messages
72
Reaction score
10
Hello, I have been working on a little movement system in a program called Game Maker: studio.
The code works fine on the programming perspective, but something I did not expect happened:
When I ran the code by adding to the speed while pressing a key, and every step passively subtracting from the speed I got a certain number I did not expect to find.
Essentially the code run like this:
if key=pressed {speed+=2};
speed-=0.25*speed;
Which according to the game maker syntax should add to the speed while the key is pressed by 2 pixels per step, and afterwards it will subtract from the speed what would be equal a quarter of the speed previously calculated.
With this method I got top speed of 6 pixels per step.
However when I just put out the entire differential equation under the key press 'if' statement, it returned a top speed of 8 pixels per step, which was exactly as an analytical analysis would predict.

Question is, why is it so? Can't seem to get why would that matter at least not at hindsight.
 
Technology news on Phys.org
perhaps you want the speed -= speed*0.25 to be in an else clause

that way pressing the key increments the speed and not pressing it reduces the speed.

Right now when pressing the key it adds 2 then then effectively subtracts a quarter of the speed.
Code:
loop 1: 10     -> 12    -> 9
loop 2: 9      -> 11    -> 8.25
loop 3: 8.25   -> 10.25 -> 7.68
 
The value of speed = 6 is a steady state when the key is pressed. If speed=6 before the code, then speed + 2 - .25 *( speed+2) = 6 + 2 - .25*(6+2) = 6 + 2 - 2 = 6 after the code. If you put a print statement after the 'if' line, then speed=8 at that point. Is that what you expected?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top