Open In App

Solving Initial Value 2nd Order Differential Equation Problem using Laplace Transform in MATLAB

Last Updated : 29 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In the domain of Multivariable calculus, the Initial value problem (IVP) is a general differential equation given together with some initial condition(s), which usually specifies the value of the unknown function at some given point(s) in its Domain. Here, Initial conditions are values of the solution and/or its derivative(s) at a specific point(s) in its domain.

Steps to Solve Initial Value Second Order Differential Equation Problem using Laplace Transform:

Step 1: Apply the Laplace Transform to the Given Equation on its Both Sides.

Step 2: Separate the ‘L(y)’ Terms after applying Laplace Transform. 

Step 3: Substitute the Initial Value Conditions given along with the 2nd Order Differential Equation in the ‘L(y)’ found in the above step.

Step 4: Simplify the ‘L(y)’.

Step 5: Now, Apply the Inverse Laplace Transform on Both Sides of the above Equation. Hence. we obtained the required solution ‘y(t)’ ( Since, InvL(L(y))=>y ).

Approach:

  • disp(txt): This method displays the ‘txt’.
  • input(txt): This method displays the ‘txt’ and waits for the user to input a value and press the Return key.
  • diff(y) (or) diff(y,x) : This method differentiates y with respect to x.
  • laplace (y,t s): This method returns the Laplace Transform of, where the independent variable is ‘t’, and the transformation variable is ‘s’.
  • subs(y, old, new): This method returns a copy of y, replacing all occurrences of old with new.
  • ilaplace(Y,s,t): This method returns the Inverse Laplace Transform of Y, where the independent variable is s and the transformation variable is t
  • ezplot(y,xinterval): This Method plots the curve y=f(x) over the specified interval ‘xinterval’.

Example 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]);


Output:

d^2y/dt^2 + 2dy/dt + 5y = exp(-t)(sin(t)), y(0)=0 ,y'(0)=1

 

 

Example 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]);


Output:

d^2y/dt^2 - 2dy/dt + y = e^t, y(0)=2 ,y'(0)=-1

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads