#範例7-1:微分方程式ODE求解,y'= ∂y/∂x = x #微分方程式:y'(x) = ∂y/∂x = x #邊界條件是: y(1) = 1 #原始方程式: y(x) = (x**2)/2 + 1/2 #降階,微分方程式ODE, y'(x) = x #語法:dsolve(eq, f(x), ics={y(1):1}) from sympy import * #1.第一種寫法 x = symbols('x') y = Function('y') ode = diff(y(x), x) - x print('1.第一種寫法:') print('沒有給定邊界條件是,C1無限多組可能') #from ode to find the 原始函數 y(x) ans = dsolve(ode, y(x)) print('ODE解是個函數=',ans) print('ODE求解 : ', ans.args[0],' = ', ans.args[1]) #邊界條件作為字典傳遞給 dsolve,通過 ics 命名參數 ans = dsolve(ode, y(x),ics={y(0):1}) print('2.給定邊界條件是: y(1) = 1') print('ODE解是個函數=',ans) print('ODE求解 : ', ans.args[0],' = ', ans.args[1]) #2.第二種寫法: x = symbols('x',real = True) #實數 y = symbols('y',cls=Function) #函數 y=x**2..... ode = diff(y(x), x) - x #from ode to find the 原始函數 y(x) ans = dsolve(ode, y(x),ics={y(0):1}) print('\n3.第二種寫法:給定邊界條件是: y(1) = 1') print('ODE求解,', ans.args[0],'=', ans.args[1]) #畫出y(x) f = ans.args[1] plot(f)