solving second order differential equations with LaPlace Transforms: getting an error when trying to input an initial condition (R2017b) ("error: untitled line")

12 views (last 30 days)
This is my code. I am getting an error in
syms s
eqn=sym('D(D(y))(t)+D(y)(t)+y(t)=((t+1)^3)*exp(-t)*cos(t)*sin(3*t)');
lteqn=laplace(eqn,t,s)
syms y ;
neweqn=subs(lteqn,{'laplace(y(t),t,s)','y(0)',''D(y)(0)'},{Y,1,0})
% the line above has the error, and the issue is with the 'D(y)(0)' term
ytrans=simplify(solve(neweqn,Y))
y=ilaplace(ytrans,s,t)

Accepted Answer

Star Strider
Star Strider on 4 Nov 2018
The laplace function does not accept initial conditions (as dsolve does).
I requested this as an enhancement several months ago. It has thus far not appeared.
You will have to do coding gymnastics with the subs function to do the necessary substitutions.

More Answers (3)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 26 Mar 2019
%% Here is the 3rd solution that works in MATLAB2018a/b and 2019a
clc; clearvars; syms t s Y y(t) Dy(t)
assume([t Y] > 0);
Dy = diff(y, t);
D2y = diff(Dy, t);
LS = ((t+1)^3)*exp(-t)*cos(t)*sin(3*t);
% Diff. Equation formulation:
EQN=D2y+Dy+y-LS;
% Laplace transform of the Diff. Equation
LEQN=laplace(EQN,t,s);
% Substitute ICs and initiate the arbitrary unknown "Y"
LT_Y=subs(LEQN,laplace(y,t,s),Y);
LT_Y=subs(LT_Y, y(0), 1); % y(0) = 1
LT_Y=subs(LT_Y, subs(diff(y(t), t), t, 0), 0); % dy(0)= 0
% Solve for the arbitrary unknown: Y
ys=solve(LT_Y,Y);
% Inverse of the Laplace Transform:
y=ilaplace(ys,s,t)
ezplot(y); shg

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 26 Mar 2019
Edited: Sulaymon Eshkabilov on 26 Mar 2019
% Here are two solutions:
%% Solution 1. Note: laplace() takes ICs.
clearvars; syms s t y Y
eqn=sym('D(D(y))(t)+D(y)(t)+y(t)=((t+1)^3)*exp(-t)*cos(t)*sin(3*t)'); % Works Matlab 2017a/b or earlier vers not for 2018a or later vers.
lteqn=laplace(eqn,t,s);
neweqn=subs(lteqn,{'laplace(y(t),t,s)','y(0)','D(y)(0)'},{Y,1,0})
% No error now
ytrans=simplify(solve(neweqn,Y))
y=ilaplace(ytrans,s,t), ezplot(y), shg
%% Solution 2
clearvars; syms t y(t)
%assume([t Y] > 0)
Dy = diff(y);
D2y = diff(y, 2);
EQN = D2y+Dy+y-((t+1)^3)*exp(-t)*cos(t)*sin(3*t);
y_s = dsolve(EQN, y(0)==1, Dy(0)==0);
ezplot(y_s), legend('toggle'), hold off; shg
  7 Comments
Mohd Aaquib Khan
Mohd Aaquib Khan on 16 May 2022
Thanks, So if we use Y(s) it doesnt work and if we use Y only(YY in your code) it works. Quite weird. Please do explain if there is an explaination for this.

Sign in to comment.


Walter Roberson
Walter Roberson on 4 Nov 2018
You have
''D(y)(0)'
Which is invalid syntax. It is the empty string '' immediately followed by D(y)(0) followed by transpose operation.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!