Method of Variation of Parameters to Solve 2nd Order Differential Equations in MATLAB (original) (raw)

Last Updated : 19 Apr, 2022

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=ar^2+br+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-4ac;
if D>0 y1=exp(r1x); y2=exp(r2x); % Complementary Function cf=c1y1+c2y2 elseif D==0 y1=exp(r1x); y2=xexp(r2x); % Complementary Function cf=c1y1+c2y2 else alpha=real(r1); beta=imag(r2); y1=exp(alphax)cos(betax); y2=exp(alphax)sin(betax); % Complementary Function cf=c1y1+c2y2 end W=simplify(y1diff(y2,x)-y2*diff(y1,x));

% Particular Integral PI=simplify((-y1)int(y2X/W) + (y2)int(y1X/W))

% General Solution GS=simplify(cf+PI)

`

Output:

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

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