微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Python 中使用 fsolve 时缺少必需的位置参数

如何解决在 Python 中使用 fsolve 时缺少必需的位置参数

我正在编写代码来帮助使用 fsolve 模拟行星的轨道,但我不断收到此错误

TypeError                                 Traceback (most recent call last)
<ipython-input-22-6ee902b5be74> in <module>
     11 
     12     # Backward Euler Method
---> 13     pn = p + dt*fsolve(nPointSystem,q,args = [m,G,n],xtol=1e-12)
     14     qn = q + dt*pn/mx3
     15 

TypeError: nPointSystem() missing 2 required positional arguments: 'G' and 'n'

代码如下:

import numpy as np
from scipy.optimize import fsolve

# mass of bodies
m = [1,1/6023600,1/408523.71,1/328900.5614,1/3098708,1/1047.3486,1/3497.898,1/22902.98,1/19412.24,1/135200000]

# Initial positions
q = [[0.00450250884530125842,0.00076707348146464055,0.00026605632781203556],[0.35726020644727541518,-0.09154904243051842990,-0.08598103998694037053],[0.60824943318560406033,-0.34913244319590053792,-0.19554434578540693592],[0.11601490913916648627,-0.92660555364038517604,-0.40180627760698804496],[-0.11468858243909270380,-1.32836653083348816476,-0.60615518941938081574],[-5.38420940699214597622,-0.83124765616108382433,-0.22509475703354987777],[7.88988993382281817537,4.59571072692601301962,1.55843151672508969735],[-18.26990081497826660524,-1.16271158021904696130,-0.25036954074255487461],[-16.05954509192446441763,-23.94294829087950141524,-9.40042278035400838599],[-30.48782211215555045830,-0.87324543019672926542,8.91129698418475509659]]

# Initial veLocities
qdot=[[-0.00000035174423541454,0.00000517762777222281,0.00000222910220557907],[0.00336784566219378527,0.02488934284224928990,0.01294407158679596663],[0.01095242010990883868,0.01561250673986247038,0.00632887645174666542],[0.01681162005220228976,0.00174313168798203152,0.00075597376713614610],[0.01448200480794474793,0.00023728549236071136,-0.00028374983610239698],[0.00109236329121849772,-0.00652329419119226767,-0.00282301226721943903],[-0.00321720349109366378,0.00433063223355569175,0.00192641746379945286],[0.00022154016562741063,-0.00376765355824616179,-0.00165324380492239354],[0.00264312279157656145,-0.00150349208075879462,-0.00068127100487234772],[0.00032256219593323324,-0.00314874792755160542,-0.00108017793159369583]]

# Gravitation Constant
G = 2.95912208286e-4

# Convert Lists to np arrays
m = np.array(m)
q = np.array(q)
qdot = np.array(qdot)

# Converting m from an nx1 to an nx3 array
mx3 = np.repeat(np.expand_dims(m,1),3,axis=1)

# Initial momentum
p = mx3 * qdot

# Time step in days
dt = 5

# number of planets
n = len(m)

def nPointSystem(q,m,n):
    pdot = np.zeros((n,3))
    
    for i in range(n):
        for j in range(n):
            if i != j:
                q_len = np.linalg.norm(q[i,:]-q[j,:])
                pdot[i,:] -= G*m[i]*m[j]*(q[i,:])/q_len**3   
    return pdot

# Time Stepper
    for i in range(1,100):
    # Backward Euler Method
    pn = p + dt*fsolve(nPointSystem,xtol=1e-12)
    qn = q + dt*pn/mx3
    
# Replace values of p and q
    p = pn
    q = qn

据我所知,我包含了我需要的位置参数。我最终要解决的是

pn = p + dt*qn
qn = q + dt*pn/mx3

我正在尝试使用后向欧拉公式对太阳系中行星的轨道进行数值求解(最终证明它不起作用)并且由于后向欧拉公式需要隐式求解,因此使用解决。但无论我如何尝试实现这些参数,我都会遇到类似的错误

在 fsolve 中,我使用 q 作为第一个值的猜测点,因为它会有点接近下一个计算值。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。