I am writing some code to model the motion of a spring. Given the initial position and velocity of the system, I am trying to calculate the position velocity at a certain time later.
To do this, I am approximating making the approximation x(t+e) = x(t) + ev(t). The position a short time 'e' seconds later is equal to the current position plus e times the velocity.
We can do the same for the velocity. v(t+e) = v(t) + ea(t). The new velocity is the same as the old velocity plus the product of acceleration and time. For a spring, a(t) = -x(t), so we instead have v(t+e) = v(t) - ex(t).
The idea of my programme is to print the position and velocity at certain 'steps' (the step is equal to e).
I defined functions for position and velocity, in accordance with the aforementioned equations.
#include <iostream>
using namespace std;
//Defining the initial state of the system
float initialPosition = 1;
float initialVelocity = 0;
float step = 0.1; 
float duration = 5;
int numberSteps = duration / step;
//Declaring the functions
float position(float t);
float velocity(float t);
//Outputting the position and velocity at time steps
int main() 
{
    cout << "Time \t" << "Position \t" << "Velocity \t" << endl;
    for(int i = 0; i < numberSteps; ++i)
    {
        cout << step * i << "\t" << position(step * i) << "\t \t" << velocity(step * i) << endl;
    }
    return 0;
}
float position(float t)
{
    if(t == 0)
    {
        return initialPosition;
    }
    else 
    {
        return position(t - step) + step * velocity(t - step);
    }
}
float velocity(float t)
{
    if(t == 0)
    {
        return initialVelocity;
    }
    else
    {
        return velocity(t - step) - step * position(t - step);
    }
}
The output I am getting (on textmate) is as follows
Time    Position    Velocity    
0          1            0
0.1        1         -0.1
0.2        0.99      -0.2
bootstrap.sh: line 10:   595 Segmentation fault: 11  "$A_OUT"
I have tried to figure out what the segmentation fault is, but many sources seem to say it has something to do with pointers (which I am not aware of, and haven't used in this programme). I spent an afternoon trying to configure gdb on my mac, to figure out the exact error, but to no avail.
EDIT I understand the problem with floats.
Replacing
if(t == 0){...}
with
if(t < 0.1 * step){...} 
seems to have done the trick.
 
     
     
     
    