Sunday, November 12, 2023

Linear Algebra : Solution to a system of ODE using matrix method

clc;
clear;
close;
%Read the coefficient matrix
A=input("Enter the coefficent matrix:");
n=length(A);
%Find eigenvalue and eigenvector
% in terms of ModalMatrix=P and SpectralMatrix=D;
[P,D]= eig(A)
PInv=inv(P);
%initial condition for Y'=AY
yx=input("Enter the initial x values:");
yy=input("Enter the initial y values:");
%Y'=AX=P*D*PInv*Y -(1)
%Take X=PInv*Y => X'=PInv*Y'
%Multiply PInv to (1)
%PInv*Y'=D*PInv*Y
%X'=D*X -New System of ODE
%initial condition for X'=DX
xx=yx
xy=PInv*yy'; %Here ' means transpose, to make same dimention to multiply
syms x(t) %For differential Equations X'=DX, take x(t) as dependent variable
for i=1:n
for j=1:n
if i==j
ode=diff(x,t)==D(i,j)*x
cond = x(xx(j))==xy(j);
X(j)=dsolve(ode,cond);
end
end
end
%X- Solution to X'=DX
%We need solution to Y i.e., Y=P*X
Y=vpa(P*X');
Sol=Y';
for i=1:n
fprintf("x%d(t)=%s\n",i,Sol(i))
end


Example

Solve the following system of first-order linear differential equations using matrix method.

$$y'_1=y_1+y_2; y'_2=4y_1-2y_2; y_1 (0)=,y_2 (0)=6$$

Output

Enter the coefficient matrix:[1 1;4 -2]

P =

    0.7071   -0.2425

    0.7071    0.9701

D =

     2     0

     0    -3

Enter the initial x values:[0 0]

Enter the initial y values:[1 6]

ode(t) =

diff(y(t), t) == 2*y(t)

ode(t) =

diff(y(t), t) == -3*y(t)

x1(t)=2.0*exp(2.0*t) - 1.0*exp(-3.0*t)

x2(t)=2.0*exp(2.0*t) + 4.0*exp(-3.0*t)

 



No comments:

Post a Comment

Search This Blog