Solving differential equations using numeric methods

In summary, the conversation discusses a movement system in Game Maker: studio where the code adds to the speed while a key is pressed and subtracts from the speed afterwards. However, the value of speed is unexpected and different from an analytical analysis. It is suggested to include the subtraction of speed*0.25 in an else clause to achieve the desired result.
  • #1
DarkBabylon
72
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
  • #2
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
 
  • #3
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?
 

Related to Solving differential equations using numeric methods

1. What are numeric methods for solving differential equations?

Numeric methods are mathematical techniques used to approximate the solution to a differential equation. These methods involve using discrete values and iterative processes to approximate the continuous solution.

2. Why do we use numeric methods for solving differential equations?

Numeric methods are used when it is not possible or practical to find an analytical solution to a differential equation. They also allow for the solution to be approximated at specific points rather than over the entire domain.

3. What are some common numeric methods for solving differential equations?

Some common numeric methods for solving differential equations include Euler's method, Runge-Kutta methods, and the finite difference method. These methods vary in complexity and accuracy, and the choice of method depends on the specific problem.

4. How accurate are numeric methods for solving differential equations?

The accuracy of numeric methods depends on the specific method used and the parameters of the differential equation. Some methods, such as higher-order Runge-Kutta methods, can provide very accurate solutions, while others may have larger errors. It is important to carefully choose the appropriate method for the problem at hand.

5. Can numeric methods be used for all types of differential equations?

Numeric methods can be used for many types of differential equations, including ordinary differential equations and partial differential equations. However, some equations may require more specialized methods, and the choice of method should consider the specific type of equation being solved.

Similar threads

  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
457
  • Programming and Computer Science
Replies
3
Views
950
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Calculus and Beyond Homework Help
Replies
10
Views
518
Back
Top