【问题标题】:Iterating through formula with varying parameters?迭代具有不同参数的公式?
【发布时间】:2015-09-21 21:01:35
【问题描述】:

我正在编写一些代码来计算流体中两个粒子之间的相互作用。获得新职位的主要公式依赖于其他 3 个函数,它们为公式获取必要的组件。

公式基本上是:

new position = old position + Important_matrix * Important_Force + (other force)^2

这是我的代码的基本布局:

initial position of particle 1 = [1,1,1]
initial position of particle 2 = [2,2,2]

def GetImportantMatrix(position of first particle, position of second particle):
'code here'
return the_important_matrix

def GetImportantForce(the_important_matrix):
'code here'
return important_force

def GetOtherForce():
'code here'
return other_force

def getnewposition(initial position of part. 1, initial position of part. 2, the_important_matrix, important_force, other_force, iterations):

???

我的实际问题是:如何将公式实现到函数中,以便它自动调用其他函数来获取所需的变量,同时在每次迭代后更新?例如,如何我开始调用其他函数以确保当前函数获得所需的信息,并且每次都正确更新。

我已经在我见过的其他代码 sn-ps 中看到了一些这样的例子,但是我无法弄清楚如何将它应用到我的代码中,因为位置变量每次都在变化,而且这也是使其他变量每次都发生变化。

我希望这是有道理的?我对 Python 很陌生,还在学习,如果你们能指出任何有助于解决此问题的资源,我将不胜感激。

【问题讨论】:

  • 我不太明白你的意思,你能进一步解释一下这行吗the position variable changes each time, and that also makes the other variables change each time
  • 调用每个函数,将返回值赋给一个变量,使用最终方程上的变量。

标签: python simulation physics


【解决方案1】:

您的 getnewposition 代码可能如下所示

def getnewposition(pos1, pos2):
    importantMatrix = getImportantMatrix(pos1,pos2)
    return pos + importantMatrix + getImportantForce(importantMatrix) + pow(getOtherForce(),2)

然后,对于您的主代码,您只需调用 getnewposition 并将您的位置变量作为您的参数。

或类似的东西。我不太清楚你的意思,所以如果你澄清一下,我可能会提供更多帮助。

【讨论】:

  • 首先,感谢您的帮助,我很感激。我尝试了您的方法,但不幸的是出现了尺寸错误,因为我正在使用的数量是数组。现在我收到 iPython 错误:operands could not be broadcast together with shapes (2,3) (1,6).
  • 我试图编辑我之前的评论,但我花了太长时间。我直接从我尝试进行的编辑中复制了它:编辑:忘记添加其余的消息。我怎么知道 Numpy 说的哪些变量因为那个大小而不能一起广播?比如,我怎么知道哪些变量(甚至是哪些运算、乘法或加法)由于维度错误而无法执行?
  • 手动排除故障,每次消除一个,直到它运行,然后将它们重新组合在一起
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-04
  • 1970-01-01
  • 1970-01-01
  • 2022-10-07
  • 2018-12-31
  • 1970-01-01
相关资源
最近更新 更多