How to solve system of first order ODE's by forming matrix

12 views (last 30 days)
Q:
dy1/dt = 2*y1+5*y2+y3
dy2/dt = 7*y1+9*y2
dy3/dt = 4*y1+y2+3*y3
y1(0) = 0, y2(0)= 0, y3(0) = 0;
i want to plot y1(t), y2(t) and y3(t) waveforms over time 0 to 10sec.
i tried the below code
syms y1(t) y2(t) y3(t)
Y = [y1(t);
y2(t);
y3(t)];
A = [2 5 1;
7 9 0;
4 1 3];
diff(Y, t) = A.*Y;
tspan = [0 10];
Y(0)=zeros(1.,3);
[t, Y] = dsolve(diff(Y, t), tspan, Y(0));
Y1 = Y(:, 1);
plot(t, Y1);
Y2 = Y(:, 1);
plot(t, Y2);
Y3 = Y(:, 1);
plot(t, Y3);
But iam getting an error like this
>> Untitled5
Error using sym/subsindex (line 864)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be
symbolic variables, and function body must be sym expression.
Error in sym/privsubsasgn (line 1151)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 972)
C = privsubsasgn(L,R,inds{:});
Error in Untitled5 (line 9)
diff(Y, t) = A.*Y;
Anyone please tell where i went wrong. I want to solve with matrices(since my main code have 20 ODE's to solve) only, please suggest me in this way only.
Thanks in advance.

Accepted Answer

Wan Ji
Wan Ji on 30 Aug 2021
Edited: Wan Ji on 30 Aug 2021
Do not use syms, because without syms, the code runs faster
A = [2 5 1;
7 9 0;
4 1 3];
odefun = @(t,Y)A*Y;
tspan = [0 10];
Y0=zeros(3,1);
[t, Y] = ode45(odefun, tspan, Y0);
Y1 = Y(:, 1);
hold on
plot(t, Y1);
Y2 = Y(:, 1);
plot(t, Y2);
Y3 = Y(:, 1);
plot(t, Y3);
legend('Y1','Y2','Y3')
As the initial conditions are all zero, the final results become zero for Y1 Y2 and Y3
IF you want to use syms
syms y1(t) y2(t) y3(t)
A = [2,5,1;7,9,0;4,1,3];
eq = diff([y1;y2;y3])==A*[y1;y2;y3];
conds = [y1(0)==0; y2(0)==0; y3(0)==0];
[y1,y2,y3] = dsolve(eq,conds)
tspan = 0:0.1:10;
y_1 = eval(subs(y1,t,tspan));
y_2 = eval(subs(y2,t,tspan));
y_3 = eval(subs(y3,t,tspan));
figure(1)
clf
hold on
plot(tspan, y_1);
plot(tspan, y_2);
plot(tspan, y_3);
legend('Y1','Y2','Y3')
Then the result becomes
y1 =
0
y2 =
0
y3 =
0
  7 Comments
Bathala Teja
Bathala Teja on 31 Aug 2021
if you dont mind can you tell me how to form a variable array without using symbolic math toolbox.
Nr = 20
nr = rand(1, Nr)
p =2;
Nr = 20
for j = 1:(2*p)
for n = (((j-1)*Nr/(2*p))+2):(j*(Nr/(2*p)))
Aord = 1/(2*pi);
nr(1, n) = nr(1, n)+cos((phi-theta-((n-j-1)*2)-((j-1)*2)))+Aord;
end
end
Here nr is a array of Nr elements consists of theta and phi in it. How to form without using sym.
Thanks in advance.

Sign in to comment.

More Answers (1)

Bathala Teja
Bathala Teja on 30 Aug 2021
Firstly iam very happy for your suggestion, thank you very much.
First solution is fine. But i have some complexity in my main code.
My main code contains contains A and B matrices(both 3*3) and is interms of some variable teta.
I want to substitute y3 in place of teta and proceed solving ODE's.
My ODE is inv(B).*(V-(A*Y)), where V = column matrix of order 3.
i tried like this
syms teta
Ai = cos(teta).*([2 5 1;
7 9 0;
4 1 3])
Bi = sin(teta).*([1 2 3;
4 5 6;
7 8 9])
V = ones(3, 1)
odefun = @(t,Y) inv(B).*(V-(A*Y));
A = subs(Ai, teta, Y(3))
B = subs(Bi, teta, Y(3))
tspan = [0 10];
Y0=zeros(3,1);
[t, Y] = ode45(odefun, tspan, Y0);
Y1 = Y(:, 1);
hold on
plot(t, Y1);
Y2 = Y(:, 1);
plot(t, Y2);
Y3 = Y(:, 1);
plot(t, Y3);
legend('Y1','Y2','Y3')
But iam getting this error
Unrecognized function or variable 'Y'.
Error in Untitled5 (line 43)
A = subs(Ai, teta, Y(3))
Problem is with substitute Y3, how to resolve that?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!