我有一個關于繪制 x(t) 的問題,即知道 dx/dt 等于下面的運算式的以下微分方程的解。在 t = 0 時 x 的值為 0。
syms x
dxdt = -(1.0*(6.84e 45*x^2 5.24e 32*x - 2.49e 42))/(2.47e 39*x 7.12e 37)
我想繪制這個一階非線性微分方程的解。決議解涉及復數,因此無關緊要,因為該方程模擬了現實生活中的程序,但 Matlab 可以使用數值方法求解方程并繪制它。有人可以建議如何做到這一點嗎?
uj5u.com熱心網友回復:
在matlab中試試這個
tspan = [0 10];
x0 = 0;
[t,x] = ode45(@(t,x) -(1.0*(6.84e 45*x^2 5.24e 32*x - 2.49e 42))/(2.47e 39*x 7.12e 37), tspan, x0);
plot(t,x,'b')
我試了一下,我得到了這個

希望對你有幫助。
uj5u.com熱心網友回復:
我寫了一個關于如何將 Python 與 SymPy 和 matplotlib 結合使用的示例。SymPy 可用于計算定積分和不定積分。通過計算不定積分并添加一個常數以將其設定為在 t = 0 時計算為 0。現在您有了積分,因此只需繪制即可。我會定義一個從起點到終點的陣列,中間有 1000 個點(可能更少)。然后,您可以使用每個時間點的常數計算積分值,然后可以使用 matplotlib 繪制該值。關于如何使用 matplotlib 自定義繪圖還有很多其他問題。
這顯示了函式 dxdt 的不定積分的基本圖,假設 x(t) = 0。運行時元組的變化Plotting()將設定要繪制的 x 值的范圍。這被設定為在呼叫函式時設定的最小值和最大值之間繪制 1000 個資料點。
有關自定義繪圖的更多資訊,我推薦matplotlib 檔案。有關積分的檔案可以在SymPy 檔案中找到。
import pandas as pd
from sympy import *
from sympy.abc import x
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
def Plotting(xValues, dxdt):
# Calculate integral
xt = integrate(dxdt,x)
# Convert to function
f = lambdify(x, xt)
C = -f(0)
# Define x values, last number in linspace corresponding to number of points to plot
xValues = np.linspace(xValues[0],xValues[1],500)
yValues = [f(x) C for x in xValues]
# Initialize figure
fig = plt.figure(figsize = (4,3))
ax = fig.add_axes([0, 0, 1, 1])
# Plot Data
ax.plot(xValues, yValues)
plt.show()
plt.close("all")
# Define Function
dxdt = -(1.0*(6.84e45*x**2 5.24e32*x - 2.49e42))/(2.47e39*x 7.12e37)
# Run Plotting function, with left and right most points defined as tuple, and function as second argument
Plotting((-0.025, 0.05),dxdt)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/352776.html
上一篇:從子矩陣創建矩陣?
