鱼C论坛

 找回密码
 立即注册
查看: 1238|回复: 3

关于用python建立的某一数学模型的求助

[复制链接]
发表于 2024-3-3 18:29:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
咳咳,让我算算,3年没泡论坛了,可能出现帖子格式不对,给大家带来麻烦,见谅
回归正题
代码:
  1. import math
  2. import decimal as dec
  3. import numpy as np
  4. import scipy as sp

  5. class Model:
  6.     def __dec(self, x):#创建十进制对象
  7.         return dec.Decimal(x)
  8.         
  9.     def __init__(self, a=1.496e11, e=1/60, T=365.25964*sp.constants.day, phi=(66+34/60)/180*math.pi):#初始化
  10.         self.a = self.__dec(a)
  11.         self.e = self.__dec(e)
  12.         self.T = self.__dec(T)
  13.         self.phi = self.__dec(phi)
  14.         self.pi = self.__dec(math.pi)
  15.         
  16.     def Fradium(self, theta):#焦半径计算
  17.         Fradium = self.a * (1 - self.e ** 2) / (1 + self.e * self.__dec(math.cos(theta)))
  18.         return float(Fradium)
  19.         
  20.     def theta_of_t(self, t):#theta(t)的计算,需要用到数值积分,这也是问题的主要出处
  21.         t = self.__dec(t)
  22.         func_of_theta = lambda theta:(1 / (1 + self.e * self.__dec(math.cos(theta)))) ** 2#待积分函数
  23.         res_of_integration = lambda theta:sp.integrate.quad(func_of_theta(theta), 0, theta)#积分结果
  24.         equation = lambda theta:float(res_of_integration(theta) - 2 * self.pi * math.sqrt(1 - self.e**2) * t / (self.T * (1 - self.e ** 2) ** 2))#方程
  25.         root = sp.optimize.root(equation, self.pi)#解方程
  26.         try:
  27.             return float(root)
  28.         except:
  29.             print('error')
  30.             return

  31.     def v_of_planet(self, theta):#行星运行速度(如果没算错)
  32.         theta = self.__dec(theta)
  33.         v = 2 * self.pi * self.a * self.__dec(math.sqrt(1 + self.e ** 2 + 2 * self.e * self.__dec(math.cos(theta)))) / (math.sqrt(1 - self.e ** 2) * self.T)#计算式
  34.         return float(v)
  35.         
  36.     def Phi(self, theta):#阳光直射纬度(如果没算错)
  37.         theta = self.__dec(theta)
  38.         Phi = -math.asin(math.cos(theta) * math.cos(self.phi))
  39.         return float(Phi)
  40.         
  41.     def pos_in_xOy(self, theta):#对于的平面直角坐标系中的坐标
  42.         theta = self.__dec(theta)
  43.         Fradium = self.__dec(self.Fradium(theta))
  44.         x = Fradium * math.cos(theta) + self.a * self.e
  45.         y = Fradium * math.sin(theta)
  46.         return (float(x), float(y))
复制代码

对于上面的数学模型有以下几个问题:
1.如果调用下面语句:
  1. if __name__=='__main__':
  2.     m = Model()
  3.     print(m.theta_of_t(1))
复制代码

那么运行结果如下
  1. Warning (from warnings module):
  2.   File "G:\tst\JustACommonModel.py", line 23
  3.     func_of_theta = lambda theta:(1 / (1 + self.e * self.__dec(math.cos(theta)))) ** 2
  4. DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
  5. Traceback (most recent call last):
  6.   File "G:\tst\JustACommonModel.py", line 54, in <module>
  7.     print(m.theta_of_t(1))
  8.   File "G:\tst\JustACommonModel.py", line 26, in theta_of_t
  9.     root = sp.optimize.root(equation, self.pi)
  10.   ...
  11. ValueError: invalid callable given
复制代码

应该是积分函数的调用之类的问题吧,代码较硬,难以解释,有志解决问题的壮士敬请谅解

2:.代码实在过硬,难以解释,就是上面说的

3.精度不足,而利用decimal中的Decimal()创建实例化对象后,会有类型不统一而精度下降或抛出错误
等等诸多问题

还有待有志之士前来挑战这几个难得让我晕头转向的bug
如果代码还存在除上面提到以外的问题还请指正
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-3-3 18:29:38 | 显示全部楼层
嘿,代码看起来有点复杂,但我会尽力解释并帮助你解决问题。

1. 首先,关于DeprecationWarning的警告,它是由于在调用函数quad时,传入了一个lambda函数而不是一个可调用对象。你可以将这个lambda函数改为func_of_theta本身,这样警告就不会出现了。修改后的代码如下:

  1. res_of_integration = sp.integrate.quad(func_of_theta, 0, theta)
复制代码


2. 第二个问题与第一个问题类似,也是关于函数的调用。在使用sp.optimize.root函数时,你应该传入一个可调用对象,而不是一个函数的返回值。因此,将equation函数作为参数传入root函数即可:

  1. root = sp.optimize.root(equation, self.pi)
复制代码


3. 关于精度问题,你可以使用decimal库提供的Decimal类型来保持计算精度。在初始化中,你已经将a、e、T和phi转换成了Decimal类型。但是,在theta_of_t函数中,你需要确保所有的中间计算都使用Decimal类型。所以,你可以将func_of_theta和equation函数中的计算都改为使用Decimal类型,以保持精度。修改后的代码如下:

  1. def theta_of_t(self, t):
  2.     t = self.__dec(t)
  3.     func_of_theta = lambda theta: (self.__dec(1) / (self.__dec(1) + self.e * self.__dec(math.cos(theta)))) ** 2
  4.     res_of_integration = sp.integrate.quad(func_of_theta, 0, theta)
  5.     equation = lambda theta: float(res_of_integration[0] - 2 * self.pi * math.sqrt(1 - self.e**2) * t / (self.T * (1 - self.e ** 2) ** 2))
  6.     root = sp.optimize.root(equation, self.pi)
  7.     try:
  8.         return float(root.x[0])
  9.     except:
  10.         print('error')
  11.         return None
复制代码


希望这些修改能够解决你的问题!如果还有其他疑问,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-3 18:50:21 | 显示全部楼层
nb
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-3-4 06:29:53 From FishC Mobile | 显示全部楼层
给我整不会了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-23 10:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表