英文:
How to take a point (xn, yn) in the plane and maps it to a new point?
问题
我尝试使用循环方法,但程序卡住了。
function getPoints() {
var x, y;
for (
x = 1, y = 2, a = 1.4, b = 0.3;
x < 10, y < 10;
x = 1 - a * x ** 2 + y, y = b * x
) {
console.log(x, y);
}
}
英文:
I tried to use the loop method but the program stuck.
function getPoints() {
var x, y;
for (
x = 1, y = 2, a = 1.4, b = 0.3;
x < 10, y < 10;
x = 1 - a * x ** 2 + y, y = b * x
) {
console.log(x, y);
}
}
答案1
得分: 1
以下是翻译好的部分:
我想看看这个系统如何工作,并在几次迭代后获得一个点(x,y)的列表。
function getPoints() {
const a = 1.4,
b = 0.3
for (
let i = 0, x = 1, y = 2; i < 10; i += 1
) {
x = 1 - a * x ** 2 + y
y = b * x
console.log(x, y);
}
}
getPoints()
希望这对您有帮助。
英文:
> I would like to see how this system works and get a list with points (x,y) after a few iterations.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function getPoints() {
const a = 1.4,
b = 0.3
for (
let i = 0, x = 1, y = 2; i < 10; i += 1
) {
x = 1 - a * x ** 2 + y
y = b * x
console.log(x, y);
}
}
getPoints()
<!-- end snippet -->
答案2
得分: 0
如Konrad在评论中指出,x
和y
变为负数后就不再变为正数,因此循环永远不会结束。在这种情况下,更好的解决方案是使用while循环,并检查x
和y
的绝对值,这样无论值的方向如何,循环都会终止:
let x = 1, y = 2
const a = 1.4, b = 0.3;
while (Math.abs(x) < 10 && Math.abs(y) < 10) {
console.log(x, y)
x = 1 - a * x ** 2 + y;
y = b * x;
}
此外,请注意,您的原始for循环并不执行您所认为的操作。在表达式x < 10, y < 10
中,逗号不表示“和”,它是一个逗号运算符,表示“忘记除了最后一个逗号后的所有内容”,也就是y < 10
。展开下面的片段以查看示例:
for (
let a = 0, b = 0;
a < 10, b < 10;
a += 2, b++
) {
console.log(a, b)
}
英文:
As Konrad pointed out in a comment, x
and y
become negative and never again become positive, so the loop never ends. In this case, a better solution would be to use a while-loop and check for the absolute value of x
and y
, so the loop will terminate regardless of what direction the values go:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let x = 1, y = 2
const a = 1.4, b = 0.3;
while (Math.abs(x) < 10 && Math.abs(y) < 10) {
console.log(x, y)
x = 1 - a * x ** 2 + y;
y = b * x;
}
<!-- end snippet -->
Also, note that your original for-loop does not do what you think it does. In the expression x < 10, y < 10
, the comma does not mean and, it is a comma operator and means forget everything except the thing after the last comma, which is y < 10
. Expand the snippet below to see an example.
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-js -->
for (
let a = 0, b = 0;
a < 10, b < 10;
a += 2, b++
) {
console.log(a, b)
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论