Fourth order Runge–Kutta in C# - two differential equations

Click For Summary

Discussion Overview

The discussion revolves around implementing the fourth-order Runge-Kutta method in C# to solve two non-linear differential equations. Participants are sharing code snippets, troubleshooting issues, and comparing results with a Simulink model.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • The original implementation by the user has a potential mix-up in the function arguments for the differential equations.
  • Some participants suggest modifications to the Runge-Kutta implementation to better align with standard formulations, particularly regarding the handling of the time variable.
  • There are discussions about the necessity of using the correct order of variables in the functions and the implications of these changes on the results.
  • One participant expresses confusion about the proposed changes, noting that their original implementation produces results consistent with Simulink.
  • Another participant emphasizes the importance of treating the intermediary constants as vectors, indicating that both x and y components should be considered in the calculations.
  • There is a request for methods to calculate the error of the solution, indicating ongoing exploration of the numerical methods involved.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the correctness of the original implementation versus the proposed modifications. There are competing views on the proper handling of variables and the resulting accuracy of the solutions.

Contextual Notes

Some participants note that the original code may not fully adhere to the standard Runge-Kutta method as described in literature, particularly regarding the treatment of time and the structure of the equations.

Who May Find This Useful

Readers interested in numerical methods for solving differential equations, particularly in the context of programming in C#, may find this discussion relevant.

  • #31
Thank you :)
Everything is working perfectly! But if you had a moment could you explain to me the idea? We use RK to integrate x, and isn't RK designated for differential equations?
 
Physics news on Phys.org
  • #32
Itosu said:
Thank you :)
Everything is working perfectly! But if you had a moment could you explain to me the idea? We use RK to integrate x, and isn't RK designated for differential equations?

Solving a differential equation is a more advanced form of integration.

Consider for instance the DE: dy - f(x)dx = 0
This is the same as dy/dx = f(x).
And that is the same as calculation the integral of f(x)dx.

In other words, any integral can also be written as a differential equation, where the result of the integration is the solution of the differential equation.

RK can calculate the solution for this numerically.
 
  • #33
Thank you!
And regarding errors - I should run the programm with step = H/2 and compare results, however on what basis? - I don't know the exact result that should be. Should I do it one more time and check if the results vary much?
 
  • #34
Ok, I understand that if one set of results differs much from another set (with different step) then the step should be decreased until differences won' tbe large.

One more question :)
Here we are using fixed-step. Is it difficult to implement variable step size?

Thank you in advance!
 
  • #35
Itosu said:
Thank you!
And regarding errors - I should run the programm with step = H/2 and compare results, however on what basis? - I don't know the exact result that should be. Should I do it one more time and check if the results vary much?

When you use the step H/2, the result should be about 32 times as accurate (this is what O(h5) means).
You can interpret this as that you have at least one extra digit that is accurate in your result.
So all the digits that are the same in both results will be presumably accurate.


Itosu said:
Ok, I understand that if one set of results differs much from another set (with different step) then the step should be decreased until differences won' tbe large.

One more question :)
Here we are using fixed-step. Is it difficult to implement variable step size?

Thank you in advance!

No, it's not particularly difficult to implement a variable step size.

You will have to adjust your rungeDx() method to not simply update this.x, this.y, and this.auc, but these values need to be returned from the method.
That way, you can calculate each step more than once without actually executing it.

In each step you need to repeatedly calculate the result, halving the step size, until your result does not change much anymore, that is, changes less than some threshold that you defined beforehand.
With each step you can also try to repeatedly double the step size until it changes too much.

Basically, that's it.
 
  • #36
So if I get:

h AUC(after 2 hours)
0.01 335.40257585727842
0.005 335.87803342989893

Can I presume that 335 is accurate result? (without knowing the part after comma).
If h=0.01 does O(h5) mean that up to the 0.015 I get accurate digits?

And should I count O(h5) or O(h4)? Local/global error?
 
  • #37
Itosu said:
If h=0.01 does O(h5) mean that up to the 0.015 I get accurate digits?

It means that up to *a constant times* 0.015 you will get accurate digits.
This constant is unknown (and might be bigger than you think or suspect).
Itosu said:
So if I get:

h AUC(after 2 hours)
0.01 335.40257585727842
0.005 335.87803342989893

Can I presume that 335 is accurate result? (without knowing the part after comma).

Since the constant is unknown and you have a discontinuity in your function, you'll have to experiment a bit.
Try a step like 0.0025.
I expect that you'll see an AUC that starts with 335.8, and probably even 335.87 or 335.88.
That should give you "confidence" that 335 are indeed accurate digits, but there is no "guarantee" mathematically speaking.
The result to present, based on your calculation, would be 335.9 ± 0.5 or 335.88 ± 0.48.
Note that whenever you measure something and present a result, you are supposed to always include the margin of error in your result.
 
Last edited:
  • #38
Thank you, I understand.
Could you explain more deeply the idea of the error O(h5) or provide a link?

Thank you in advance!
 
  • #39
Itosu said:
Thank you, I understand.
Could you explain more deeply the idea of the error O(h5) or provide a link?

Thank you in advance!

The errors of numerical algorithms are expressed with this big O notation:
http://en.wikipedia.org/wiki/Big_O_notation"
 
Last edited by a moderator:

Similar threads

  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
7K