stochastic differential eqn in c++

Dear all,

I have a simple stochastic differential eqn that I am working with:

dx/dt = (1/gamma)*eta(t)= sqrt(GAMMA)*zeta(t)

gamma == constant

GAMMA== 2*kBT*gamma, where kBT (Boltzmann constant*temperature) is 1, and gamma is the friction coefficient, is the measure of intensity of stochastic force eta(t)

zeta(ti) is for noise generated using a random number generator (which can be either gaussian or uniform)

What I need to do is iteratively compute coordinates like this:
x(i+1) = x(i) + (sqrt(GAMMA)/gamma)*zeta(ti)
and produce an output file for coordinates at every timestep.
i
my problem is that I do not know how to incorporate the random number generator for zeta(ti)into my equation.

#include <iostream>
#include <stdlib>
#include <time.h>
using namespace std;

int main ()
{
// variable declaration:
int x, t, kBT, gamma, GAMMA;

kBT = 1
gamma = 1
GAMMA = 2*kBT*gamma

I realise that I need a uniform random number generator
which can be seeded as follows:
srand ( time(NULL) );

but I do not understand where this should go, together with everything else in int main () or some somehow separately? And how can it be synchronised with my equation?

Thank you for any input in advance
Usually srand((double) seed) has a second function with it, which generates the number from the seed:
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

Your code probably needs to shape that result for something different than an Integer.

For program design, it would depend on how well you understood functions, not the math type but the type we use in c++ programming. You can cram all the information into main() or you could break it up into separate functions for easier debugging.
Thanks Azagaros!

I had a look at the example you quoted, thanks for help! I am still stuck with the code as I do not know how to perform time integration in c++ such that I could record x with specific time-steps... are there any comprehensive examples / documentation on how this could be done? I have read c++ related literature, but the book I use does not emphasise the mathematical formulations unfortunately.

thanks in advance!
Going back to my original message I have now attempted to write some parts of the code, and wanted to ask whether someone could show me where my mistakes are, as upon compilation, I get messages that certain variables are "not declared in this scope"... Also I wanted to know whether I am on the right track with my equation?

Thanks for any input again!

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
using namespace std;

int main ()
{
// variable declaration:
double t, kBT, gamma, GAMMA;

kBT = 1.0;
gamma = 1.0;
GAMMA = 2.0*kBT*gamma;

// process:

srand(time(NULL)); // initial seed value

for (int i = 1; i <= 100; i=i+1)
{
u = 1.0*rand()/(RAND_MAX+1); // random number between 0.0 and 1.0
x(x+1) = x(i) + (sqrt(GAMMA/gamma)*u);

return a;
}}

Topic archived. No new replies allowed.