#範例8-4:Euler-Cauchy 方程式之解--複數根 # ode :x**2*y''(x) - x*y'(x) + 4y = 0 #邊界條件或起始條件:y(1) =2, y'(1) =-1 #語法:dsolve(eq, f(x), ics={y(1):2,y(x).diff(x).subs(x,1):-1}) #所解出的原方程式:y(x) = -sin(2*log(x))/2 + 2*cos(2*log(x)) from sympy import * x = symbols('x') y = Function('y')(x) #注意:ode是個方程式,不是函數 ode = Eq(x**2*y.diff(x,2) + x*y.diff(x) + 4*y) ans = dsolve(ode, y, ics={y.subs(x,1):2, y.diff(x).subs(x,1):-1}) print('ode的解:', ans.args[0], '=', ans.args[1]) plot(ans.args[1], (x,0,10), line_color='red')