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)
채택된 답변
추가 답변 (3개)
Sulaymon Eshkabilov
2019년 3월 26일
%% 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
댓글 수: 1
Yusif Razzaq
2020년 5월 5일
really helped me out. thanks
Sulaymon Eshkabilov
2019년 3월 26일
편집: Sulaymon Eshkabilov
2019년 3월 26일
% 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
Thilini Madhushika
2021년 12월 30일
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)')
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 = 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)')
k = k2; m = m2;
disp('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)
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')
Thilini Madhushika
2021년 12월 30일
@ Walter Roberson Thank you 😊
Mohd Aaquib Khan
2022년 4월 24일
@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)
Walter Roberson
2022년 4월 26일
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)
Mohd Aaquib Khan
2022년 5월 16일
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.
Walter Roberson
2022년 5월 16일
solve() cannot solve for functions such as Y(s)
Walter Roberson
2018년 11월 4일
You have
''D(y)(0)'
Which is invalid syntax. It is the empty string '' immediately followed by D(y)(0) followed by transpose operation.
댓글 수: 2
Danielle Lawhorn
2018년 11월 4일
Walter Roberson
2018년 11월 5일
Remove the first of the two '' so that you have 'D(y)(0)'
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!








