变常微分方程的作图

2024-04-26 10:53:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在正在写一套代码,用来解一个常微分方程。。。我的代码正在工作,但是,我想能够修改它来解决一组不同的常数值微分方程。这是我目前拥有的,如果ran有效的话。你知道吗

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

def f(w, x):
    # d1 = omega lambda
    d1 = w
    b2 = 0.0

    # 0.2<c<1.4, 0.20 increments
    c = 0.2  

    q = (1 - d1 - (2*((d1**1.5)/c))) / (2 + (3*(b2)))    

    f = (d1**2) * (1 - d1) * ((1 / d1) + (2 / (c * (d1**0.5))) - ((3 * b2 *    q) / (d1 * (1-d1))))
    return f

#determine domain, x
x = np.linspace(-80, 80, 1000001)

d1 = 10 ** -8

sol = odeint(f, d1, x)

plt.xlabel("x")
plt.ylabel("Omega Lambda")
plt.plot(x, sol, 'r')
plt.show() 

但是,我想构造一个由一组不同的c值产生的每一行组成的图。。。我想绘制的c的图形是:

c = 0.2,0.4,0.6,0.8,1.0,1.2,1.4

Tags: 代码fromimportnumpyasnppltscipy
1条回答
网友
1楼 · 发布于 2024-04-26 10:53:10
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

def f(w, x , b2 , c):
    # d1 = omega lambda
    d1 = w


    # 0.2<c<1.4, 0.20 increments
    #c = 0.2  

    q = (1 - d1 - (2*((d1**1.5)/c))) / (2 + (3*(b2)))    
    f = (d1**2) * (1 - d1) * ((1 / d1) + (2 / (c * (d1**0.5))) - ((3*b2*q)/(d1*1-d1))))
    return f

#determine domain, x
x = np.linspace(-80, 80, 1000001)

d1 = 10 ** -8
b2 = 0.0
c = [  0.2,0.4,0.6,0.8,1.0,1.2,1.4 ]
for i in c:

    sol = odeint(f, d1, x, args = (b2 , i))
    plt.xlabel("x")
    plt.ylabel("Omega Lambda")
    plt.plot(x, sol, 'r')
    plt.show()

相关问题 更多 >