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

  • #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
5K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
14
Views
4K
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
7K