将平面上的点(xn,yn)映射到一个新点的方法是什么?

huangapple go评论98阅读模式
英文:

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);
  }
}
英文:

将平面上的点(xn,yn)映射到一个新点的方法是什么?

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 &lt; 10, y &lt; 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 &lt; 10; i += 1
  ) {
    x = 1 - a * x ** 2 + y
    y = b * x
    console.log(x, y);
  }
}

getPoints()

<!-- end snippet -->

答案2

得分: 0

Konrad在评论中指出xy变为负数后就不再变为正数,因此循环永远不会结束。在这种情况下,更好的解决方案是使用while循环,并检查xy的绝对值,这样无论值的方向如何,循环都会终止:

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) &lt; 10 &amp;&amp; Math.abs(y) &lt; 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 &lt; 10, y &lt; 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 &lt; 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 &lt; 10, b &lt; 10;
  a += 2, b++
  ) {
  console.log(a, b)
  }

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月9日 00:49:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定