Solving Spring System with RK4: Step-by-Step Guide

In summary: So in the example above, are you using the K values only on calculating acceleration? Yes, both accelerations and velocities are calculated as K values: ak1->ak4 are accelerations, vk1->vk4 are velocities, calculated during a time step Δt. Then the accelerations ak1->ak4 are used to update velocity (v[ i+1 ]) and velocities vk1->vk4 are used to update position (p[ i+1 ]). I'm assuming that acceleration is a function of position a(p[ i ]). In some cases, like re-entry of a space craft, acceleration would be a function of velocity (aerodynamic drag) and position
  • #1
Bluestribute
194
0
I'm not sure if this is the appropriate place, but I've just been searching, with no luck, on how to solve a spring system with RK4 (hence the math section, since it's a Runge Kutta question). How would you set it up to change position based on spring properties? I haven't really found any consistent things either, except that x' = v and v' = a (which is standard).

What would one iteration look like? As far as having position and velocity as your inputs, and outputting an updated position after the iteration. I know how to do RK4, just not so great at implementing it.
 
Technology news on Phys.org
  • #2
Bluestribute said:
I'm not sure if this is the appropriate place, but I've just been searching, with no luck, on how to solve a spring system with RK4 (hence the math section, since it's a Runge Kutta question).
That's a numerical methods, so I have moved the thread to Programming.

Bluestribute said:
How would you set it up to change position based on spring properties? I haven't really found any consistent things either, except that x' = v and v' = a (which is standard).
Start by setting out the differential equations you need to solve.
Bluestribute said:
What would one iteration look like? As far as having position and velocity as your inputs, and outputting an updated position after the iteration. I know how to do RK4, just not so great at implementing it.
Once you have done the above, applying the RK4 algorithm is trivial (meaning that there is nothing special, it will look the same as it does for any set of coupled first-order ODEs).
 
  • #3
DrClaude said:
set of coupled first-order ODEs.
That's probably the issue, knowing how to use RK4 with coupled ODEs. Example sequence, ak1 -> ak4 are calculated accelerations, vk1 -> vk4 are calculated velocities, p[ i ] is current position. Note that vk2 is based off ak1, vk3 is based off ak2, ... ; trying to use vk2 based off ak2, vk3 based off ak3, ... , will throw off the RK4 coupled ODE calculations. Similarly, ak2 is based off vk1, ak3 is based off vk2 (in case the code was changed to calculate vk's before ak's).

Code:
    ak1 = a(p[i])
    vk1 = v[i]
    ak2 = a(p[i] + vk1 (Δt/2))
    vk2 = v[i] + ak1 (Δt/2)
    ak3 = a(p[i] + vk2 (Δt/2))
    vk3 = v[i] + ak2 (Δt/2)
    ak4 = a(p[i] + vk3 (Δt))
    vk4 = v[i] + ak3 (Δt))

    v[i+1] = v[i] + (Δt/6) (ak1 + 2 ak2 + 2 ak3 + ak4)
    p[i+1] = p[i] + (Δt/6) (vk1 + 2 vk2 + 2 vk3 + vk4)
 
Last edited:
  • #4
So in the example above, are you using the K values only on calculating acceleration? Then do you solve it like normal, or would you repeat that process for X and V, where V would be solved using K values? Cause the way I know it is that the K values are in the form h*f (where f is the multivar function and you have different additives for each K1-4), and you're only doing that with accelerations . . .

Or would I just setup a spring system as:

v' = v - x (or whatever it is, though I didn't see much of what it should be . . . ) and do it similarly? Where I would solve first vk1 by plugging in v,x, and then solve xk1 by . . . uh . . . is it the same as above, the "k1"*Δt/2?
 
  • #5
Bluestribute said:
So in the example above, are you using the K values only on calculating acceleration?
As mentioned in my example, both accelerations and velocities are calculated as K values: ak1->ak4 are accelerations, vk1->vk4 are velocities, calculated during a time step Δt. Then the accelerations ak1->ak4 are used to update velocity (v[ i+1 ]) and velocities vk1->vk4 are used to update position (p[ i+1 ]). I'm assuming that acceleration is a function of position a(p[ i ]). In some cases, like re-entry of a space craft, acceleration would be a function of velocity (aerodynamic drag) and position (density of atmosphere, force of gravity). For an example spring like case, if a(p[ i ]) = -p[ i ], you get some variation of a sine wave, depending on initial condition, like v[0] = 1, p[0] = 0.
 
Last edited:
  • #6
So when you say acceleration is a function of position, are you saying F = ma = -kΔx (english language to math language is not my strong suit! Haha)? So would ak1 just be -kΔx/m? I think if I can get that relationship, I could figure out the implementation . . . I think
 
  • #7
Bluestribute said:
So when you say acceleration is a function of position, are you saying F = ma = -kΔx (english language to math language is not my strong suit! Haha)? So would ak1 just be -kΔx/m? I think if I can get that relationship, I could figure out the implementation . . . I think
Almost, for your example a(x) = -kx / m, based on position x, not change in position Δx. So ak1 = - k x[ i ] / m. x[0] would be the initial position, and v[0] would be the initial velocity. Note I corrected the formulas for ak4 and vk4 (they use Δt not Δt/2) Rewriting the example loop fragment using x instead of p:

Code:
    ak1 = a(x[i])
    vk1 = v[i]
    ak2 = a(x[i] + vk1 (Δt/2))
    vk2 = v[i] + ak1 (Δt/2)
    ak3 = a(x[i] + vk2 (Δt/2))
    vk3 = v[i] + ak2 (Δt/2)
    ak4 = a(x[i] + vk3 (Δt))
    vk4 = v[i] + ak3 (Δt))

    v[i+1] = v[i] + (Δt/6) (ak1 + 2 ak2 + 2 ak3 + ak4)
    x[i+1] = x[i] + (Δt/6) (vk1 + 2 vk2 + 2 vk3 + vk4)

ak1 and vk1 are the initial acceleration and velocity at the start of the current time step. x[ i ] + vk1 (Δt/2) is the first estimated position at the mid-point of the current time step and so a(x[ i ] + vk1 (Δt/2)) would be the first estimated acceleration at the mid-point of the current time step. ak2 and vk2 are then estimated acceleration and velocity at the mid-point of the current time step. ak3 and vk3 are improved estimates of acceleration and velocity at the mid-point of the current time step, using vk2 and ak2 as feedback. ak4 and vk4 are estimates of acceleration and velocity at the end of the current time step, using vk3 and ak3 as feedback.
 
Last edited:

1. What is a spring system?

A spring system is a physical system that consists of one or more springs connected together. It is commonly used in engineering and physics to model the behavior of objects that are subject to forces such as compression, tension, and torsion.

2. What is RK4 and how does it work?

RK4 (Runge-Kutta 4th order) is a numerical method used to solve differential equations. It works by breaking down the problem into smaller steps and using a weighted average of the slopes at different points to estimate the solution at each step. This makes it more accurate than simpler methods like Euler's method.

3. How do I use RK4 to solve a spring system?

To use RK4 to solve a spring system, you will need to first write down the equations that describe the behavior of the system. These equations will involve variables such as mass, spring constant, and displacement. Then, you can use a computer program or a calculator to apply RK4 to these equations and solve for the values of these variables at different points in time.

4. What are the advantages of using RK4 to solve a spring system?

RK4 is a more accurate method compared to simpler methods like Euler's method. It also allows for a larger time step, meaning that the solution can be calculated faster. Additionally, RK4 can be applied to solve a wide range of differential equations, making it a versatile tool for solving complex problems.

5. Are there any limitations to using RK4 to solve a spring system?

One limitation of RK4 is that it can only be used to solve systems that can be described by differential equations. It also requires knowledge of the initial conditions and may not give accurate results for very large or very small time steps. Additionally, if the equations for the spring system are highly nonlinear, RK4 may not give accurate results.

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
Replies
38
Views
452
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
19
Views
17K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
788
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
325
  • Programming and Computer Science
Replies
3
Views
7K
  • Astronomy and Astrophysics
Replies
4
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
Back
Top