- #1
Livethefire
- 51
- 0
For weeks I've been coding a program to simulate waves on a string. Currently I am working on some numerical methods. Namely Euler and Runge-Kutta. I have Euler working fine as far as I can see for a variety of input waveforms (of course after it runs for a while it goes a bit beserk due to inaccuracies).
I can't for the life of me figure out to implement The RK4 method though. It gets messy. I've searched for the past few weeks, on the internet and every forum I can find to see if I can understand the method better and I can't get my head around it.
Im using the "standard" equations on Wiki. And the first thing I am really confused about is the f(x, t) function for example.. which is intuitive for the euler method I feel, but whenever there are 4 extra terms, I am lost as to what to do with them.
This is my euler method. A bit chunky I know, but it works.
I Have a string that is broken up into a number of elements, the arrays yold, ynew, vold, vnew and a Hold the position, velocity and acceleration of each element.
This is just the euler routine, I have a much larger piece of code which shows the wave and animates it with a graphics package, and it does what its supposed to.
Where the red shows the differential equation I am going to solve (2nd order ODE). This line is also my acceleration calculation.
And the bold shows my two euler approximations, twice because I am solving a 2nd order ODE.
My problem now is how do I replace these bold statements with the RK4 approximation?
I have another program set up with more arrays but any attempt at RK4 just doesn't work.
Specifically, I am confused abotu how to do it twice, and how do I approximate the function f for k2, k3, and k4?
For the euler method I just multiplied the time step by either velocity or acceleration (see code above).
Thank you for any help or guidance you can give to me on the matter.
Cheers.
I can't for the life of me figure out to implement The RK4 method though. It gets messy. I've searched for the past few weeks, on the internet and every forum I can find to see if I can understand the method better and I can't get my head around it.
Im using the "standard" equations on Wiki. And the first thing I am really confused about is the f(x, t) function for example.. which is intuitive for the euler method I feel, but whenever there are 4 extra terms, I am lost as to what to do with them.
This is my euler method. A bit chunky I know, but it works.
I Have a string that is broken up into a number of elements, the arrays yold, ynew, vold, vnew and a Hold the position, velocity and acceleration of each element.
This is just the euler routine, I have a much larger piece of code which shows the wave and animates it with a graphics package, and it does what its supposed to.
Code:
int i;
//Fixed ends, loop runs through 2nd element and 2nd to last element in array
for(i=1; i<(n-1); i++){
//calculates the acceleration of each element
[COLOR="Red"] a[i]=(k/mass_per_n)*((yold[i-1]-yold[i])+(yold[i+1]-yold[i]))[/COLOR]
//and its velocity with euler
[B]vnew[i]=vold[i]+a[i]*dt;[/B]
}
for(i=1; i<(n-1); i++){
Then calculates position with euler
[B] ynew[i]=yold[i]+vold[i]*dt; [/B]
}
Where the red shows the differential equation I am going to solve (2nd order ODE). This line is also my acceleration calculation.
And the bold shows my two euler approximations, twice because I am solving a 2nd order ODE.
My problem now is how do I replace these bold statements with the RK4 approximation?
I have another program set up with more arrays but any attempt at RK4 just doesn't work.
Specifically, I am confused abotu how to do it twice, and how do I approximate the function f for k2, k3, and k4?
For the euler method I just multiplied the time step by either velocity or acceleration (see code above).
Thank you for any help or guidance you can give to me on the matter.
Cheers.