在 MATLAB 中使用拉普拉斯变换求解初值二阶微分方程问题
在多变量微积分领域,初值问题(IVP)是一个带有一些初始条件的一般微分方程,它通常指定未知函数在其域中某个给定点的值。这里,初始条件是解和/或其在其域中特定点的导数的值。
使用拉普拉斯变换求解初值二阶微分方程问题的步骤:
第 1 步:将拉普拉斯变换应用于给定方程的两边。
第 2 步:在应用拉普拉斯变换后分离“L(y)”项。
第 3 步:将给出的初始值条件与上一步中找到的“L(y)”中的二阶微分方程一起代入。
第 4 步:简化“L(y)”。
第 5 步:现在,在上述方程的两边应用拉普拉斯逆变换。因此。我们获得了所需的解决方案'y(t)'(因为,InvL(L(y))=>y)。
方法:
- disp(txt):此方法显示“txt”。
- input(txt):此方法显示“txt”并等待用户输入值并按回车键。
- diff(y) (or) diff(y,x) :此方法相对于 x 区分 y。
- laplace(y,ts):该方法返回拉普拉斯变换,其中自变量为't',变换变量为's'。
- subs(y, old, new):此方法返回 y 的副本,将所有出现的 old 替换为 new。
- ilaplace(Y,s,t):该方法返回Y的拉普拉斯逆变换,其中自变量为s,变换变量为t
- ezplot(y,xinterval):此方法在指定间隔“xinterval”上绘制曲线 y=f(x)。
示例 1:
Matlab
% MATLAB code to Solve Initial Value Second Order
% Differential Equation Problem using Laplace Transform:
% To clear all variables from the current workspace
clear all
clc
disp("Solving Initial Value 2nd Order Differential Equation
Problem using Laplace Transform in MATLAB | GeeksforGeeks")
% To Declare them as Variables
syms t s y(t) Y
dy(t)=diff(y(t));
d2y(t)=diff(y(t),2);
F=input('Input the coefficients [a,b,c]: ');
% Coefficients of the 2nd Order Differential Equation
a=F(1);b=F(2);c=F(3);
% R.H.S of the 2nd Order Differential Equation
nh=input('Enter the non-homogeneous part f(x) :');
% 2nd Order Differential Equation Formed
equation=(a*d2y(t)+b*dy(t)+c*y(t)-nh);
LTY=laplace(equation,t,s);
% Initial Value Conditions
y0=IC(1);dy0=IC(2);
IC=input('Enter the initial conditions in the form [y0,Dy(0)]:');
disp("After applying Laplace Transform on Both Sides: ")
% Y = L(y)
LTY=subs(LTY,laplace(y(t),t,s),Y)
% Substitute the Initial Value Condition-1 (y(0))
LTY=subs(LTY,y(0),y0);
% Substitute the Initial Value Condition-2 (y'(0))
LTY=subs(LTY,subs(diff(y(t),t),0),dy0);
% Collect the 'Y' Terms (L(y) terms)
eq=collect(LTY,Y);
disp("After Simplification, L(y) is: ")
% Simplify 'Y'
Y=simplify(solve(eq,Y))
% Find Inverse Laplace of 'Y' (ILap(Y)=ILap(L(y))=y))
yt=simplify(ilaplace(Y,s,t));
disp('The solution of the differential equation y(t)=')
disp(yt);
% Displaying the Plot of y(t) Found
ezplot(yt,[y0, y0+2]);
Matlab
% MATLAB code to Solve Initial Value Second Order
% Differential Equation Problem using Laplace Transform:
% To clear all variables from the current workspace
clear all
clc
disp("Solving Initial Value 2nd Order Differential Equation
Problem using Laplace Transform in MATLAB | GeeksforGeeks")
% To Declare them as Variables
syms t s y(t) Y
dy(t)=diff(y(t));
d2y(t)=diff(y(t),2);
F=input('Input the coefficients [a,b,c]: ');
% Coefficients of the 2nd Order Differential Equation
a=F(1);b=F(2);c=F(3);
% R.H.S of the 2nd Order Differential Equation
nh=input('Enter the non-homogeneous part f(x) :');
% 2nd Order Differential Equation Formed
equation=(a*d2y(t)+b*dy(t)+c*y(t)-nh);
LTY=laplace(equation,t,s);
% Initial Value Conditions
y0=IC(1);dy0=IC(2);
IC=input('Enter the initial conditions in the form [y0,Dy(0)]:');
disp("After applying Laplace Transform on Both Sides: ")
% Y = L(y)
LTY=subs(LTY,laplace(y(t),t,s),Y)
% Substitute the Initial Value Condition-1 (y(0))
LTY=subs(LTY,y(0),y0);
% Substitute the Initial Value Condition-2 (y'(0))
LTY=subs(LTY,subs(diff(y(t),t),0),dy0);
% Collect the 'Y' Terms (L(y) terms)
eq=collect(LTY,Y);
disp("After Simplification, L(y) is: ")
% Simplify 'Y'
Y=simplify(solve(eq,Y))
% Find Inverse Laplace of 'Y' (ILap(Y)=ILap(L(y))=y))
yt=simplify(ilaplace(Y,s,t));
disp('The solution of the differential equation y(t)=')
disp(yt);
% Displaying the Plot of y(t) Found
ezplot(yt,[y0, y0+2]);
输出:
d^2y/dt^2 + 2dy/dt + 5y = exp(-t)(sin(t)), y(0)=0 ,y'(0)=1
示例 2:
MATLAB
% MATLAB code to Solve Initial Value Second Order
% Differential Equation Problem using Laplace Transform:
% To clear all variables from the current workspace
clear all
clc
disp("Solving Initial Value 2nd Order Differential Equation
Problem using Laplace Transform in MATLAB | GeeksforGeeks")
% To Declare them as Variables
syms t s y(t) Y
dy(t)=diff(y(t));
d2y(t)=diff(y(t),2);
F=input('Input the coefficients [a,b,c]: ');
% Coefficients of the 2nd Order Differential Equation
a=F(1);b=F(2);c=F(3);
% R.H.S of the 2nd Order Differential Equation
nh=input('Enter the non-homogeneous part f(x) :');
% 2nd Order Differential Equation Formed
equation=(a*d2y(t)+b*dy(t)+c*y(t)-nh);
LTY=laplace(equation,t,s);
% Initial Value Conditions
y0=IC(1);dy0=IC(2);
IC=input('Enter the initial conditions in the form [y0,Dy(0)]:');
disp("After applying Laplace Transform on Both Sides: ")
% Y = L(y)
LTY=subs(LTY,laplace(y(t),t,s),Y)
% Substitute the Initial Value Condition-1 (y(0))
LTY=subs(LTY,y(0),y0);
% Substitute the Initial Value Condition-2 (y'(0))
LTY=subs(LTY,subs(diff(y(t),t),0),dy0);
% Collect the 'Y' Terms (L(y) terms)
eq=collect(LTY,Y);
disp("After Simplification, L(y) is: ")
% Simplify 'Y'
Y=simplify(solve(eq,Y))
% Find Inverse Laplace of 'Y' (ILap(Y)=ILap(L(y))=y))
yt=simplify(ilaplace(Y,s,t));
disp('The solution of the differential equation y(t)=')
disp(yt);
% Displaying the Plot of y(t) Found
ezplot(yt,[y0, y0+2]);
输出:
d^2y/dt^2 - 2dy/dt + y = e^t, y(0)=2 ,y'(0)=-1