求大佬把这段mathematica代码转为python代码,在线等,挺急的!

查看 61|回复 4
作者:快乐星光小天使   
[i]
[i]
以下为文字版代码:
f[x_, n_, k_, p_] = x^n/(k^n + x^n) (p (p + 1))/2 + k^n/(k^n + x^n) ((p - 1) p)/2 + (p + 1) (1 - p);
CFFL[a_, b_, c_] := ParametricNDSolveValue[{x'[t] == f[x[t], nxx, kxx, a] - x[t],y'[t] == f[y[t], nyy, kyy, b] f[x[t], nxy, kxy, 1] - y[t],z'[t] == f[z[t], nzz, kzz, c] f[x[t], nxz, kxz, 1] f[y[t], nyz, kyz, 1] - z[t], x[0] == x0, y[0] == y0, z[0] == z0}, {x[t], y[t], z[t]}, {t, 0, 100}, {kxx, kyy, kzz, kxy, kxz, kyz, nxx, nyy, nzz, nxy, nxz, nyz, x0, y0, z0}];
Table[DynamicModule[{kxx = 0.3`, kxy = 0.01`, kxz = 0.01`, kyy = 0.3`,kyz = 0.01`, kzz = 0.3`, nxx = 3, nxy = 1, nxz = 1, nyy = 3, nyz = 1, nzz = 3, tmax = 10, x0 = a, y0 = 0.185, z0 = 0.19}, Show[Plot[Evaluate[CFFL[#1[[1, 1]], #1[[1, 2]], #1[[1, 3]]][kxx, kyy, kzz, kxy, kxz, kyz, nxx, nyy, nzz, nxy, nxz, nyz, x0, y0, z0]][[3]] /. t -> t1, {t1, 0, tmax}, PlotRange -> All, PlotStyle -> {Thick, #1[[2]]}, AxesOrigin -> {0, 0}] & /@ Transpose[{Select[SortBy[Flatten[(Outer[List, ##1] &) @@ Array[{-1, 0, 1} &, 3],2], -Count[#1, 0] &], Count[#1, -1] == 0 &][[;; 4]], {Black, Blue, Red, Gray}}]]], {a, {0, .1, .5}}]
希望有大佬相助,谢谢大佬!!!

大佬, 代码

快乐星光小天使
OP
  

之前有利用chatgpt转换,虽然能运行,但生成的图不正确,也不知道哪里有问题。
日比兔   

这是bing转的,这段代码看起来很复杂,我无法完全转换成Python代码。但是我可以尝试给您一些基本的步骤和提示,希望能对您有所帮助。
首先,您需要导入SymPy库,并使用var函数定义所有的符号变量,例如:
[Python] 纯文本查看 复制代码
import sympy as sp
x, y, z, t = sp.var('x y z t')
kxx, kxy, kxz, kyy, kyz, kzz = sp.var('kxx kxy kxz kyy kyz kzz')
nxx, nxy, nxz, nyy, nyz, nzz = sp.var('nxx nxy nxz nyy nyz nzz')
x0, y0 ,z0 = sp.var('x0 y0 z0')
p = sp.var('p')
a = sp.var('a')
然后,您需要使用SymPy的函数和运算符来重写Mathematica中的函数和表达式,例如:
[Python] 纯文本查看 复制代码
f = lambda x,n,k,p: x**n/(k**n + x**n) * (p * (p + 1))/2 + k**n/(k**n + x**n) * ((p - 1) * p)/2 + (p + 1) * (1 - p)
接下来,您需要使用SymPy的数值微分方程求解器(NDSolve)来代替Mathematica中的ParametricNDSolveValue函数,例如:
[Python] 纯文本查看 复制代码CFFL = lambda a,b,c: sp.NDSolve([sp.Eq(x(t).diff(t), f(x(t), nxx,kxx,a) - x(t)),sp.Eq(y(t).diff(t), f(y(t),nyy,kyy,b) * f(x(t),nxy,kxy,p=1) - y(t)),sp.Eq(z(t).diff(t), f(z(t),nzz,kzz,c) * f(x(t),nxz,kxz,p=1) * f(y(t),nyz.kyz,p=1) - z(t)),sp.Eq(x(0),x0),sp.Eq(y(0),y0),sp.Eq(z(0),z0)], [x,y,z], (t , 0 ,100))
最后,您需要使用SymPy的绘图功能(plot)来代替Mathematica中的Table和Show函数,例如:
[Python] 纯文本查看 复制代码for a in [0,.1,.5]:
    sol = CFFL(a,a,a)
    sp.plot(sol[2],(t , 0 ,10))
请注意,这些代码只是一个大致的示例,并没有经过测试或验证。您可能还需要根据您的具体需求进行一些调整和优化。
无敌小儿   

import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
def f(x, n, k, p):
return (xn/(kn+xn))(p(p+1))/2 + kn/(kn+xn)((p-1)p)/2 + (p+1)*(1-p)
def CFFL(a, b, c):
def fun(t, xyz, params):
kxx, kyy, kzz, kxy, kxz, kyz, nxx, nyy, nzz, nxy, nxz, nyz, x0, y0, z0 = params
x, y, z = xyz
dxdt = f(x, nxx, kxx, a) - x
dydt = f(y, nyy, kyy, b)f(x, nxy, kxy, 1) - y
dzdt = f(z, nzz, kzz, c)f(x, nxz, kxz, 1)*f(y, nyz, kyz, 1) - z
return [dxdt, dydt, dzdt]
return lambda kxx, kyy, kzz, kxy, kxz, kyz, nxx, nyy, nzz, nxy, nxz, nyz, x0, y0, z0: solve_ivp(fun, [0, 100], [x0, y0, z0], args=([kxx, kyy, kzz, kxy, kxz, kyz, nxx, nyy, nzz, nxy, nxz, nyz, x0, y0, z0],),method='BDF').y
points = [np.array([i,j,k]) for i in [-1,0,1] for j in [-1,0,1] for k in [-1,0,1] if (i !=0 or j !=0 or k != 0)]
sorted_points = sorted(points, key=lambda pt: -np.count_nonzero(pt == 0))
combinations = [comb for comb in sorted_points if np.count_nonzero(comb == -1) == 0][:4]
colors = ['black', 'blue', 'red', 'gray']
tmax = 10
y0 = 0.185
z0 = 0.19
for a in [0, 0.1, 0.5]:
fig, ax = plt.subplots()
for i, comb in enumerate(combinations):
kxx = kyy = kzz = 0.3
kxy = kxz = kyz = 0.01
nxx = nyy = nzz = 3
nxy = nxz = nyz = 1
    x0 = a
    xyz = CFFL(comb[0], comb[1], comb[2])(kxx, kyy, kzz, kxy, kxz, kyz, nxx, nyy, nzz, nxy, nxz, nyz, x0, y0, z0)
    ax.plot(np.linspace(0, tmax, xyz.shape[1]), xyz[2,:], color=colors[i], linewidth=2.0)
ax.set(xlabel='Time', ylabel='z', title=f'a={a}')
ax.grid()
快乐星光小天使
OP
  


无敌小儿 发表于 2023-3-8 10:16
import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt

感谢!我这就测试下!
您需要登录后才可以回帖 登录 | 立即注册

返回顶部