This is the code I've written and been provided with, and below is the error I am repeatedly getting. I've tried using both y.diff & Derivative functions in this code, but both of them give me the same error.
#y'-2ty = -t
from sympy import *
t = symbols('t')
y = Function('y')(t)
eqn = (Derivative(y(t)) - (2*t*y(t)), -t)
display(eqn)
ysol = dsolve(eqn,y(t))
print("The solution is ", ysol.lhs, " = ", ysol.rhs)
print("The solution is ")
display(ysol)
Error:
----> 1 ysol = dsolve(eqn,y(t))
TypeError: 'y' object is not callable
The line
already defines
yas a function oft. When you writey(t)later, you're asking sympy to callyas if it's a function (like when you call theprintfunction asprint(...)). You have two options: (1) Don't defineyas a function oft, i.e.With that change, you will no longer get the error. (2) Keep
ydefined as it is but later only writeyinstead ofy(t), i.e.In your question you said you tried
y.diff(), but with how you've definedy, that should actually work, so I'm guess you actually didy(t).diff().Now, you actually have another issue later, which is that you should define
eqnas a sympy equation, i.e.Without that,
dsolvewill throw an error (it's expecting an equation).As another minor point, avoid wildcard imports (
from X import *) in your code. Some people will doimport sympy as smporimport sympy as spor justimport sympy. Then, you would doy = smp.Function("y").