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

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)

 采纳的回答

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.

更多回答(3 个)

%% 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
% 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 个评论

I also have same problem. I don't know how to substitute initial conditions for dy1 and dy2.
k=2,m=1 for both equations. condition 1 =1,condition2=1,condition3=sqrt(6),condition4= -1*sqrt(6)
This is my code:
clc
clear all
syms y1(t) y2(t) s
assume(t>0)
dy1 = diff(y1,t);
dy2 = diff(y2,t);
disp('assume(t>0)')
disp('diff(y1,t)=dy1')
disp('diff(y2,t)=dy2')
disp('diff(y1,t,2) == (-2*(k/m)*y1+(k/m)*y2)')
disp('Enter the coefficients correctly for the first equation')
k=input('k=');
m=input('m=');
eqn1= diff(y1,t,2) == (-2*(k/m)*y1+(k/m)*y2);
disp('diff(y2,t,2) == ((k/m)*y1-2*(k/m)*y2)')
k=input('k=');
m=input('m=');
disp('Enter the coefficients correctly for the second equation')
eqn2= diff(y2,t,2) == ((k/m)*y1-2*(k/m)*y2);
c1=input('enter condition 1:');
c2=input('enter condition 2:');
c3=input('enter condition 3:');
c4=input('enter condition 4:');
eqn1LT = laplace(eqn1,t,s)
eqn2LT = laplace(eqn2,t,s)
syms y1_LT y2_LT
eqn1LT = subs(eqn1LT,[laplace(y1,t,s) laplace(y2,t,s)],[y1_LT y2_LT])
eqn2LT = subs(eqn2LT,[laplace(y1,t,s) laplace(y2,t,s)],[y1_LT y2_LT])
eqns = [eqn1LT eqn2LT]
vars = [y1_LT y2_LT]
[y1_LT,y2_LT] = solve(eqns,vars)
y1sol = ilaplace(y1_LT,s,t)
y2sol = ilaplace(y2_LT,s,t)
y1sol = simplify(y1sol)
y2sol = simplify(y2sol)
vars = [y1(0) y2(0) dy1(0) dy2(0)]
values = [c1 c2 c3 c4]
y1sol = subs(y1sol,vars,values)
y2sol = subs(y2sol,vars,values)
%disp('Enter the range for the plot the graph')
%tmin = input('tmin value:');
%tmax = input('tmax value:');
subplot(2,1,1)
ezplot(y1sol,t)
title('The Graph Of y1(t) Vs t')
ylabel('y1(t)')
xlabel('t')
subplot(2,1,2)
ezplot(y2sol,t)
title('The Graph Of y2(t) Vs t')
ylabel('y2(t)')
xlabel('t')
You look like you are substituting for dy1(0) and dy2(0) already ? Not sure what you want to do differently ?
if ~isunix()
k1 = input('first k=');
m1 = input('first m=');
k2 = input('second k=');
m2 = input('second m=');
c1 = input('enter condition 1:');
c2 = input('enter condition 2:');
c3 = input('enter condition 3:');
c4 = input('enter condition 4:');
else
k1 = 2;
m1 = 1;
k2 = 2;
m2 = 1;
c1 = 1;
c2 = 1;
c3 = sqrt(6);
c4 = -1*sqrt(6);
end
syms y1(t) y2(t) s
assume(t>0)
dy1 = diff(y1,t);
dy2 = diff(y2,t);
disp('assume(t>0)')
assume(t>0)
disp('diff(y1,t)=dy1')
diff(y1,t)=dy1
disp('diff(y2,t)=dy2')
diff(y2,t)=dy2
disp('diff(y1,t,2) == (-2*(k/m)*y1+(k/m)*y2)')
diff(y1,t,2) == (-2*(k/m)*y1+(k/m)*y2)
disp('Enter the coefficients correctly for the first equation')
Enter the coefficients correctly for the first equation
k = k1; m = m1;
eqn1= diff(y1,t,2) == (-2*(k/m)*y1+(k/m)*y2);
disp('diff(y2,t,2) == ((k/m)*y1-2*(k/m)*y2)')
diff(y2,t,2) == ((k/m)*y1-2*(k/m)*y2)
k = k2; m = m2;
disp('Enter the coefficients correctly for the second equation')
Enter the coefficients correctly for the second equation
eqn2= diff(y2,t,2) == ((k/m)*y1-2*(k/m)*y2);
eqn1LT = laplace(eqn1,t,s)
eqn1LT = 
eqn2LT = laplace(eqn2,t,s)
eqn2LT = 
syms y1_LT y2_LT
eqn1LT = subs(eqn1LT,[laplace(y1,t,s) laplace(y2,t,s)],[y1_LT y2_LT])
eqn1LT = 
eqn2LT = subs(eqn2LT,[laplace(y1,t,s) laplace(y2,t,s)],[y1_LT y2_LT])
eqn2LT = 
eqns = [eqn1LT eqn2LT]
eqns = 
vars = [y1_LT y2_LT]
vars = 
[y1_LT,y2_LT] = solve(eqns,vars)
y1_LT = 
y2_LT = 
y1sol = ilaplace(y1_LT,s,t)
y1sol = 
y2sol = ilaplace(y2_LT,s,t)
y2sol = 
y1sol = simplify(y1sol)
y1sol = 
y2sol = simplify(y2sol)
y2sol = 
vars = [y1(0) y2(0) dy1(0) dy2(0)]
vars = 
values = [c1 c2 c3 c4]
values = 1×4
1.0000 1.0000 2.4495 -2.4495
y1sol = subs(y1sol,vars,values)
y1sol = 
y2sol = subs(y2sol,vars,values)
y2sol = 
%disp('Enter the range for the plot the graph')
%tmin = input('tmin value:');
%tmax = input('tmax value:');
subplot(2,1,1)
ezplot(y1sol,t)
title('The Graph Of y1(t) Vs t')
ylabel('y1(t)')
xlabel('t')
subplot(2,1,2)
ezplot(y2sol,t)
title('The Graph Of y2(t) Vs t')
ylabel('y2(t)')
xlabel('t')
@Walter Roberson I modified solution 1 by you for newer version. I am more interested in the ytrans, (Y(s))
I am getting the laplace equation correctly, but if I try to solve for Y, it shows empty solution. Can you please spot the issue for me.
Thanks
syms s t y(t) Y(s)
Dy = diff(y);
D2y = diff(y, 2);
eqn= D2y+Dy+y== (t+1)^3
lteqn=laplace(eqn,t,s)
neweqn=subs(lteqn,[laplace(y(t),t,s),y(0),Dy(0)],[Y,1,0])
ytrans=solve(neweqn,Y)
syms s t y(t) Y(s) YY
Dy = diff(y);
D2y = diff(y, 2);
eqn= D2y+Dy+y== (t+1)^3
lteqn=laplace(eqn,t,s)
neweqn=subs(lteqn,[laplace(y(t),t,s),y(0),Dy(0)],[YY,1,0])
ytrans=solve(neweqn,YY)
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.

请先登录,再进行评论。

You have
''D(y)(0)'
Which is invalid syntax. It is the empty string '' immediately followed by D(y)(0) followed by transpose operation.

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by