ex8.7

等我刷把宗师發表於2024-11-12
from scipy.integrate import odeint
import numpy as np
import pylab as plt
import sympy as sp

dy = lambda y, x: -2*y+2*x**2+2*x  #自變數在後面
xx = np.linspace(0,3,31)
s = odeint(dy, 1, xx)
print('x={}\n對應的數值解y={}'.format(xx, s.flatten()))
plt.plot(xx, s,'*')
sp.var('x'); y=sp.Function('y')
eq=y(x).diff(x)+2*y(x)-2*x**2-2*x
s=sp.dsolve(eq,ics={y(0):1})
sx = sp.lambdify(x,s.args[1],'numpy')  #符號函式轉匿名函式
plt.plot(xx, sx(xx))
plt.show()