0

I have the following code:

int main ( int argc, char **argv ){
double lambda = 4;
double x = .2;

for ( int i=1; i<=30; i++ )
  {
    printf( "%.5f \n", x );
    x = lambda * x * (1-x);
  }           
}

That outputs the following:

0.20000 0.64000 0.92160 0.28901 0.82194 0.58542 0.97081 0.11334 0.40197 0.96156 0.14784 0.50392 0.99994 0.00025 0.00098 0.00394 0.01568 0.06174 0.23173 0.71212 0.82001 0.59036 0.96734 0.12638 0.44165 0.98638 0.05374 0.20342 0.64815 0.91221

My question is: What is the most apt algorithmic/mathematical description for the manner in which 'x' is being manipulated every iteration?

nicktendo
  • 587
  • 1
  • 9
  • 24
  • 1
    `x` is changing based on its own previous value. `i` is just a counter how many times the calculation should be repeated. – Joachim Isaksson Mar 14 '16 at 04:46
  • Its giving perfect results. Dont see any problem there. – Digital_Reality Mar 14 '16 at 04:47
  • [Further reading](http://www.cs.utsa.edu/~wagner/pubs/logistic/remarks.pdf). Mathematically, this generates an infinite sequence of distinct random numbers if we start off with `x` irrational. Clearly this can't be replicated by floating point (since they are all rational) and that paper discussed some ways of dealing with the errors that creep in from floating point. – M.M Mar 14 '16 at 05:13
  • Shouldn't `printf` take a pointer to `x`? – Greg M Mar 14 '16 at 05:45
  • 4
    Just wondering why nobody mentioned that the function iterates the logistic equation which for most values of `3.54 – Frank Puffer Mar 14 '16 at 09:25
  • 1
    Thank you for the comment, @FrankPuffer, that is what I had intended be a part of my initial question, but realized after the fact that I failed to aptly express that inquiry, so I accepted the first explicitly correct response to the body of my question. – nicktendo Mar 14 '16 at 12:46

3 Answers3

2

For each iteration of the loop, the variable x is assigned a new value based on some calculation involving the old value of x. The variable i is used simply to limit the number of times this calculation repeats.

Similarly, you can change the value of x (without using i) in a much simpler way:

x = x + 1;

Use that instead of the x = lambda * x * (1-x); line and observe how the value increases.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
2

You are using x in the following statement x = lambda * x * (1-x); . This way x value changes on every iteration.

The Apache
  • 1,076
  • 11
  • 28
  • 1
    This does not explain why the sequence is chaotic. Many iterations show convergence to a fixed value or periodic beaviour. This specific equation, called logistic equation, is one of the most famous models for chaotic behaviour. – Frank Puffer Mar 14 '16 at 09:32
  • 1
    @FrankPuffer: The question OP was asking was how the value of `x` could change if the calculation does not involve the value of `i`. – dreamlax Mar 16 '16 at 03:28
  • @dreamlax: Right, but actually he asked two completely different questions, the one in the title, which I was refering to, and the other one at the end of the text. – Frank Puffer Mar 16 '16 at 07:53
1

The "i" in the for loop is known as the iteration variable, which means it will be used as a counter for keeping track of the no. of iterations of the loop.

Whereas, the "x" is the variable which you want to play with in the loop.

You can try printing both the values of i and x in the loop to understand the concepts of iteration.

AdityaD
  • 11
  • 2