Open In App

Method of Variation of Parameters to Solve 2nd Order Differential Equations in MATLAB

Last Updated : 19 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

MATLAB can be used to solve numerically second and higher-order ordinary differential equations. In this article, we will see the method of variation of parameters to Solve 2nd Order Differential Equations in MATLAB.

Step 1: Let the given 2nd Order Differential Equation in terms of ‘x’ is:

\frac{d^{2}y}{dx^{2}} + p \frac{dy}{dx} + qy = f(x)

Step 2: Then, we reduce it to its Auxiliary Equation(AE) form: _{r}{^2}+ pr +Q = 0

Step 3: Then, we find the Determinant of the above AE by the Relation: Det  = ^ { p^2 - 4Q}

Step 4: If the Determinant found above is Positive (2 Distinct Real roots r1 & r2), then the Complementary Function(CF) will be:y = C1{e^{r1^x} + e^{r2^x}}

Step 5: If the Determinant found above is Zero (1 Unique Real Root ,r1=r2=r), then the Complementary Function(CF) will be: y = C1{e^{rx} + C2 X e^{rx}}

Step 6: If the Determinant found above is Negative (Complex Roots, r = α ± iβ), then the Complementary Function(CF) will be: y = {e^{ax} (C1 cos{\beta X })+ i (C2 Sin X {\beta x}})

Step 7: In all the Above 3-Cases, the Coefficient of C1 is termed ‘y1‘, and the Coefficient of C2 is termed ‘y2.

Step 8: Then we find the Wronskian(W) by the Relation: W(y_{1}, y_{2}) = y_{1}({y_{2}')- {y_{2}y_{1}'

Step 9: After finding ‘W’, we find the Particular Integral (PI) by the Relation: PI = -y_{1}\int_{}^{}\frac{y{2}f(x)}{w}dx + y_{2}\int_{}^{}  \frac{y{1}f(x)}{w} dx

Step 10: Finally the General Solution(GS) of the 2nd Order Differential Equation is found by the Relation:  GS = CF + PI

 

Example:

Matlab

% MATLAB code for Method of Variation of Parameters 
% to Solve 2nd Order Differential 
% Equations in MATLAB
clear all     
clc  
% To Declare them as Variables
syms r c1 c2 x 
disp("Method of Variation of Parameters to Solve 
2nd Order Differential Equations in MATLAB | GeeksforGeeks")
  
E=input("Enter the coefficients of the 2nd Order Differential equation"); 
X=input("Enter the R.H.S of the 2nd order Differential equation");
  
% Coefficients of the 2nd Order Differential Equations
AE=a*r^2+b*r+c;
a=E(1); b=E(2); c=E(3); 
S=solve(AE);
  
% Roots of Auxiliary Equation (AE)
r1=S(1); r2=S(2);
  
% Determinant of Auxiliary Equation (AE)
D=b^2-4*a*c;     
if D>0
    y1=exp(r1*x); 
    y2=exp(r2*x);
    % Complementary Function
    cf=c1*y1+c2*y2 
elseif D==0
    y1=exp(r1*x);
    y2=x*exp(r2*x); 
    % Complementary Function
    cf=c1*y1+c2*y2 
else
    alpha=real(r1); 
    beta=imag(r2);
    y1=exp(alpha*x)*cos(beta*x); 
    y2=exp(alpha*x)*sin(beta*x);
     % Complementary Function
    cf=c1*y1+c2*y2
end
W=simplify(y1*diff(y2,x)-y2*diff(y1,x)); 
  
% Particular Integral
PI=simplify((-y1)*int(y2*X/W) + (y2)*int(y1*X/W)) 
  
% General Solution
GS=simplify(cf+PI)

                    

Output:

Input: y'' -6y' + 25 = e2x + sinx + x

 

Input: y'' -2y' + 3 = x3 + cosx

 



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

Similar Reads