Language: C++
Description/Purpose:
A spring-mass system with mass \( m \), damping coefficient \( \gamma \), and spring constant \( k \) is described by the differential equation
\[ m y''(t) + \gamma y'(t) + k y(t) = f(t) \]
where \( f(t) \) is defined to be the driving function for the system. This equation is derived from a sum of forces, so \( f(t) \) is equal to the force left over when all other forces cancel out.
Input:
Mass m
Damping Coefficient gamma
Spring Constant k
Initial Conditions \( y_o, y’_o \)
Output:
A function that takes time as input and returns the position of the mass at that time.
Usage/Example:
To use this function, you have to include it in the file you need to call it from.
Output:
Implementation/Code:
The spring-mass differential equation is a second degree linear equation, which will have two roots to its corresponding characteristic equation. These roots should be allowed to have complex domain as a spring mass system is one that often tends to be oscillatory. The complimentary solution will have the form:
There is, however, a caveat. If \( \lambda_1 = \lambda_2 \) then there needs to be an additional \( u(t) \) that one of the solutions must be multiplied by to guarantee linear independence of the eigenfunctions of the differential equation.
This covers all possible cases of the eigenvalues for this second order differential equation. Sometimes, people will split the former case into two parts, one where you have real non-distinct roots, one where you have complex non-distinct roots. The latter involves writing the analytic solution in terms of \( sin(x) \) and \( cos(x) \), which is equivalent to the real non-distinct case when using Euler’s Identity \( e^{ix} = cos(x) + i sin(x) \). Since the function I am writing only returns a value, and does not care how the solution is written, I am only using the two cases of equivalent roots and non-equivalent roots.
The particular solution would involve quite a bit of integration, and most likely some clever use of the Wronskian determinant via the method of Variation of Parameters. As we are not yet equipped to compute such integration in code in this class, I have left the particular solution as a stub to be filled out later.
Solving for the analytic particular solution to the homogenous part of the spring-mass differential equation, we can eliminate \( C_1 \) and \( C_2 \) by algebraicly determining their equivalents with respect to \( y_o \) and \( y’_o \). The coefficients will be different for distinct vs repeated roots, so a ternary statement is used in the following code to handle either case.
All this leaves for programming is determining the eigenvalues and leading coefficients, then to construct a function to determine the position of the mass at time t: