[英]Why does this C++ function produce chaotic output?
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 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? 我的问题是:对于每次迭代操作“ x”的方式,最恰当的算法/数学描述是什么?
For each iteration of the loop, the variable x
is assigned a new value based on some calculation involving the old value of x
. 对于循环的每次迭代,都会根据涉及
x
的旧值的一些计算为变量x
分配一个新值。 The variable i
is used simply to limit the number of times this calculation repeats. 变量
i
仅用于限制此计算重复的次数。
Similarly, you can change the value of x
(without using i
) in a much simpler way: 类似地,您可以以一种更简单的方式更改
x
的值(不使用i
):
x = x + 1;
Use that instead of the x = lambda * x * (1-x);
用它代替
x = lambda * x * (1-x);
line and observe how the value increases. 线,观察值如何增加。
You are using x
in the following statement x = lambda * x * (1-x);
您在以下语句中使用
x
x = lambda * x * (1-x);
. 。 This way
x
value changes on every iteration. 这样,
x
值在每次迭代时都会更改。
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. for循环中的“ i”被称为迭代变量,这意味着它将用作跟踪no的计数器。 of iterations of the loop.
循环的迭代次数。
Whereas, the "x" is the variable which you want to play with in the loop. 而“ x”是您要在循环中使用的变量。
You can try printing both the values of i and x in the loop to understand the concepts of iteration. 您可以尝试在循环中同时打印i和x的值,以了解迭代的概念。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.