Yes Oscar, I am fully aware of dsolve. Here is the successful Matlab code (Note: IC y(0)=y’(0)=0)
syms s y(t) Y g(t) Dy=diff(y(t),t); D2y=diff(Dy,t); g=8*t*(heaviside(t)-heaviside(t-5))+40*heaviside(t-5); ode=D2y+4*y-g ==0; ltode=laplace(ode); ltode=subs(ltode,laplace(y),Y); ltode=subs(ltode,y(0),0); ltode=subs(ltode,subs(diff(y(t), t), t, 0),0); Y=solve(ltode,Y); y=ilaplace(Y) Otput from Matlab on input ltode=laplace(ode); tode = (8*exp(-5*s))/s^2 - s*y(0) + s^2*laplace(y(t), t, s) - subs(diff(y(t), t), t, 0) - 8/s^2 + 4*laplace(y(t), t, s) == 0 ltode=subs(ltode,laplace(y),Y) gives output ltode = 4*Y - s*y(0) + (8*exp(-5*s))/s^2 - subs(diff(y(t), t), t, 0) + Y*s^2 - 8/s^2 == 0 Matlab output on commands ltode=subs(ltode,y(0),0) ltode=subs(ltode,subs(diff(y(t), t), t, 0),0) gives ltode = 4*Y + (8*exp(-5*s))/s^2 - subs(diff(y(t), t), t, 0) + Y*s^2 - 8/s^2 == 0 ltode = 4*Y + (8*exp(-5*s))/s^2 + Y*s^2 - 8/s^2 == 0 Y=solve(ltode,Y) produces output Y = -((8*exp(-5*s))/s^2 - 8/s^2)/(s^2 + 4) and finally y=ilaplace(Y) gives y = 2*t - sin(2*t) + 8*heaviside(t - 5)*(sin(2*t - 10)/8 - t/4 + 5/4) So, chis procedure is not possible to process in current version of SymPy. When will it be possible? Any estimation? Cheers, Staffan > 13 jan. 2021 kl. 19:59 skrev Oscar Benjamin <[email protected]>: > > I haven't used sympy's Laplace transform code but I can tell you that > dsolve is able to solve the differential equation: > > In [14]: y = Function('y') > > In [15]: t = Symbol('t') > > In [16]: g = 8*t*(Heaviside(t) - Heaviside(t-5)) + 40*Heaviside(t-5) > > In [17]: eq = Eq(y(t).diff(t, 2) + 4*y(t), g) > > In [18]: str(eq) > Out[18]: 'Eq(4*y(t) + Derivative(y(t), (t, 2)), 8*t*(Heaviside(t) - > Heaviside(t - 5)) + 40*Heaviside(t - 5))' > > In [19]: str(dsolve(eq)) > Out[19]: 'Eq(y(t), 2*(t*Heaviside(t) + 5*Heaviside(t - 5))*cos(2*t)**2 > + (C1 - 10*cos(10)*Heaviside(t - 5) + > 4*Integral(t*sin(2*t)*Heaviside(t - 5), t))*cos(2*t) + > 2*(t*Heaviside(t) - t*Heaviside(t - 5) + 5*Heaviside(t - > 5))*sin(2*t)**2 + (C2 - cos(2*t)*Heaviside(t - 5) - Heaviside(t) + > cos(10)*Heaviside(t - 5))*sin(2*t))' > > There is an unevaluated integral in there and it's quite slow because > of computing the integrals involved so there can be improvements to > made to the integration routines here. > > Not having used sympy's laplace transforms before I'm not sure how > they would be used for a case like this: > > In [24]: s = Symbol('s') > > In [25]: str(laplace_transform(eq.lhs - eq.rhs, t, s)) > Out[25]: '(4*LaplaceTransform(y(t), t, s) + > LaplaceTransform(Derivative(y(t), (t, 2)), t, s) - 40*exp(-5*s)/s + > 8*(5*s - exp(5*s) + 1)*exp(-5*s)/s**2, 0, True)' > > I guess that `LaplaceTransform(Derivative(y(t), (t, 2)), t, s)` is an > unevaluated Laplace transform of the derivative. How exactly does > Matlab represent that? > > > Oscar > > On Wed, 13 Jan 2021 at 18:46, Staffan Lundberg > <[email protected]> wrote: >> >> >> I am working with a project to replace Matlab with Python, in a calculus >> course. Explicitly, following problem is solved in Matlab. It is an >> inhomogenous IVP with constant coefficients together with relevant IC's. In >> Matlab Code: Dy=diff(y(t),t); D2y=diff(Dy,t); >> g=8*t*(heaviside(t)-heaviside(t-5))+40*heaviside(t-5); ode=D2y+4*y-g ==0; My >> goal is to Laplace transform the equation, obtain a solution in frequency >> domain, and finally transform it back to time domain, obtaining the final >> solution. This procedure is successfully done by Matlab. >> >> I address this issue to the developer team. Will it, in future releases of >> SymPy, be possible to solve this problem in Python/SymPy? >> >> -- >> You received this message because you are subscribed to the Google Groups >> "sympy" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/sympy/10568891-8a24-43aa-8f93-e1a6b7de72e6n%40googlegroups.com. > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/CAHVvXxRJUFhiF3t9qtsMBTqhMBqg8JPAOaV2gyTjdHCYdSs0MA%40mail.gmail.com. -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/D61C3110-AD53-4151-B38E-365B78379B06%40gmail.com.
