code problems -- output generation

Hello all, I am confused about this piece of code that I wrote to try and numerically solve an equation like this for 100 timesteps:

x(i+1) = x(i) + (sqrt(GAMMA)/gamma)*zeta(ti)

I wanted to know if I am on the right track?

Thanks for any input in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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 u, i = 1; i <= 100; i=i+1)
{
u = 1.0*rand()/(RAND_MAX+1); // random number between 0.0 and 1.0
i = i + (sqrt(GAMMA/gamma)*u);
cout << i << " ";
i++;
return i;}
}
You're not making much sense in your use of 'i'.

On one hand, it's the iteration/counter variable, increased at every step by the for loop (r22).
On the other hand, it's also a significant value, as you assign it a value generated by your calculation (r23).
Then, you increase it again (r25) for no apparent reason (if you actually want to 'i+2' at each iteration, define it as such in the for loop control).
Then, you return the value of 'i'. Inside the loop, inside the main. That means your main() (i.e. your program) stops after one iteration (as return is called).

Just calculate the value of 'i' for a few iterations (which aren't executed due to return, but just hypothetically) and you'll see that it's not making much sense.
thank you for advice, Gaminic!

I understand that r25 iteration was not needed, and now have changed the code, but I am sure that this is still not what I need as even though there are fluctuations, values are mainly negative (which confuses me as I do not know why that happens), namely:

x(i+1) = x(i) + (sqrt(GAMMA)/gamma)*zeta(ti)

Also I want to print the first 100 iterations in a column and am not sure how this can be achieved.

thanks in advance for any help and suggestions!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
using namespace std;

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

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

// process:

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

for (double u, x, y;;)
{
u = 1.0*rand()/(RAND_MAX+1); // random number between 0.0 and 1.0
y = x + (sqrt(GAMMA/gamma)*u);
cout << y << " " ;
}}
Topic archived. No new replies allowed.