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

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.

 Respuesta aceptada

Wan Ji
Wan Ji el 30 de Ag. de 2021
Editada: Wan Ji el 30 de Ag. de 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 comentarios

Wan Ji
Wan Ji el 30 de Ag. de 2021
Editada: Wan Ji el 30 de Ag. de 2021
Hi,
As to pose a question. @Bathala Teja, you should show more details of your variables rather than just simplify it to something a constant matrix. The time of those who answer these questions is limited, do not make fun of them.
I need to say I really enjoy helping others, but I just like to anwer those questions that I see, the hidden is in your heart not in mine. So show me your true colors, my friend. As for the ODE, I have suggested you not to mix symbolic solutions of ode with numerical solutions. Here I just write down the symbolic solution to your question(in fact, answer 2 to this question).
As you use
Bi = sin(teta).*([1 2 3;
4 5 6;
7 8 9])
Do not kid me, this matrix is singular!!!!!! and has no inverse.
And I see you subsitute teta (theta exactly) with Y(3), why not directly use y3?
A = subs(Ai, teta, Y(3))
B = subs(Bi, teta, Y(3))
You drive crazy.
I just write what I believe
syms y1(t) y2(t) y3(t)
A = [2,5,1;7,9,0;4,1,3]*cos(y3);
B = sin(y3).*([1 0 0;0 5 0;0 0 9]); % since it's singular, I rewrite it here
V = [1;1;1];
Y = [y1;y2;y3];
eq = diff(Y)==inv(B)*(V-A*Y)
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')
Actually, this does not have a solution, so I dont know what you are thinking about now, maybe homework, maybe something formidable.
If it is homework, it doesnot mattter you upload it as a photo, your question is quite rediculous to some extend. I mean, not a normal person who has studied maths can pose such a question
Sorry for wasting your valuablke time. Actually the original code is too big. In that i will get A and B matrices as output. These A and B matrices are 27*27 in size and they have theta as a variable in it.
I need to solve 27 ODE's by using this exp B^(-1).*(V-(A*Y)), last variable in the ODE eq's is theta(Means Y27 = theta).
I just asked here for finding the way to solve in this case.
Tell me one thing is it possible to solve with substitution or i need change entire code for avoiding substitution(as you suggested above)??
By the way it is formidable thats why i havn't shared total code. Once again sorry for your inconvenience.
It doesnot matter how many odes there are, one thing that you should notice is that I always make the solution in two ways. One is symbolic, the other is numeric. Do not mix them together. Once again I remind you of that.
Though you have 27 odes, it is also very easy to get a numeric solution. I just give something a framework to help you understand how to solve odes
function dYdt = odefun(t, Y)
% input Y is a 27x1 matrix
% input t refers to time
% dYdt refers to the derivative of Y function with respect to time
% dYdt is a 27x1 matrix
B = []; % B is a 27x27 matrix, maybe include some Y items
% for example B(1,1) = Y(3); B(1,2) = Y(27)*Y(16);
V = []; % V is a 27x1 vector, maybe includes some Y items
A = []; % the same size as B, maybe include some Y items
dYdt = B\(V-A*Y); % that's enough
end
Now following is the main function command
tspan = [0 10];
Y0=zeros(27,1); % something a 27x1 matrix
[t, Y] = ode45(@(t,Y)odefun(t,Y), tspan, Y0);
% plot the results
Y1 = Y(:, 1);
hold on
plot(t, Y1);
Y2 = Y(:, 1);
plot(t, Y2);
Y3 = Y(:, 1);
plot(t, Y3);
legend('Y1','Y2','Y3')
Happy to see your suggestion, but i already tried this way.
In the ODE function code i have to call (Since i cant include this function code in my original code where i get A and B as result) A and B matrices from my original code which are variable form (EX: A = [cos(theta) sin(2*theta+pi/3) ........]>>like this 27*27).
Problem is, i already used sym for theta in my main code. It is giving error whenever i call these variable matrices into function of ODE's.
I want to remind you onething, in my main code i already used sym for theta and formed A and B matrices interms of theta. Now i want to solve ODE's by including A and B in the eq. But the main problem comes here is putting a variable matrix(sym matrix) into ODE eq. But i can eliminate this theta variable by substituting last ODE solution(means Y27). I think its not possible to solve a ODE like this. Now iam trying to substitute t in place of theta, lets see how this works.
I think i have to proceed with syms, i cant do in numerical way. Iam totally tired of trying various ways.
Once again thanks for your reply.
replace theta with y(...), I hope you get the result soon
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.

Iniciar sesión para comentar.

Más respuestas (1)

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?

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Preguntada:

el 30 de Ag. de 2021

Comentada:

el 31 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by