![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173415/image.png)
How to indentify the variables when plotting after solving a coupled system of ODEs?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Enterprixe
am 18 Mär. 2017
Beantwortet: Star Strider
am 18 Mär. 2017
Hello, i had a system of coupled ODEs and i think i have solved it, the problem is that the variable Y which gives the output, i dont know how to tell what column is the response to the x(t), y(t), z(t) or their first derivatives. Help would be much appreciated. Here is the code
syms x(t) y(t) z(t) p(t) Y
p=sin((0.5+3.5*t)*t);
D1y = diff(y,t);
D2y = diff(y,t,2);
D1x = diff(x,t);
D2x = diff(x,t,2);
D1z = diff(z,t);
D2z = diff(z,t,2);
eqns=[D2x+2*x-1*y==p,D2y+(0.5*10*exp(-10*t))*D1y-x+2*y-z==0,D2z-y+2*z==0];
yode = odeToVectorField(eqns);
Yodefcn = matlabFunction(yode, 'Vars',[t Y]);
tspan = (0:0.001:10);
Y0 = [0 0,0 0,0 0];
[T,Y] = ode45(Yodefcn, tspan, Y0);
plot(T, Y)
0 Kommentare
Akzeptierte Antwort
Star Strider
am 18 Mär. 2017
The easiest way is to ask for a second output from your odeToVectorField call. It tells you the substitutions the function made to define each element in the returned column:
[yode,Sbs] = odeToVectorField(eqns);
here, the substitutions are:
Sbs =
y
Dy
x
Dx
z
Dz
The columns of ‘Yodefcn’ exactly reproduces those returned by odeToVectorField, and ode45 returns them in that order.
That can be used with some other assignments to automatically produce a legend that labels every line in the plot.
The Code —
syms x(t) y(t) z(t) p(t) Y
p=sin((0.5+3.5*t)*t);
D1y = diff(y,t);
D2y = diff(y,t,2);
D1x = diff(x,t);
D2x = diff(x,t,2);
D1z = diff(z,t);
D2z = diff(z,t,2);
eqns=[D2x+2*x-1*y==p,D2y+(0.5*10*exp(-10*t))*D1y-x+2*y-z==0,D2z-y+2*z==0];
[yode,Sbs] = odeToVectorField(eqns);
Yodefcn = matlabFunction(yode, 'Vars',[t Y]);
tspan = (0:0.001:10);
Y0 = [0 0,0 0,0 0];
[T,Y] = ode45(Yodefcn, tspan, Y0);
plot(T, Y)
Lgndc = sym2cell(Sbs);
Lgnds = regexp(sprintf('%s\n', Lgndc{:}), '\n', 'split');
legend(Lgnds(1:end-1), 'Location','N')
The Plot —
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173415/image.png)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!