odeFunction: List variables through loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Tejas Adsul
am 2 Jul. 2018
Bearbeitet: Stephen23
am 3 Jul. 2018
I have some differential equations that I'm solving using the odeFunction. I am following the steps as described in the documentation. In this step:
[eqs,vars] = reduceDifferentialOrder(eqn,y(t));
I have variables like:
[eqs,vars] = reduceDifferentialOrder(eqn,[x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6]);
Now, this depends on a variable n. If n = 6, I have variables upto (x6,y6). For any other n, I have variables upto (xn,yn). I want to run a simulation where n is changing, but not drastically. Hence, I want to write those variables in a form dependent on n. Meaning, if n = 9, the vars array should automatically contain [x1,x2,x3,x4,x5,x6,x7,x8,x9,y1,y2,y3,y4,y5,y6,y7,y8,y9]. Is there a way to do this?
2 Kommentare
Akzeptierte Antwort
Stephen23
am 3 Jul. 2018
Bearbeitet: Stephen23
am 3 Jul. 2018
Writing to file is slow and unnecessary, it is simpler and faster to just make the string directly:
>> Xs = sprintf(',x%d',1:9);
>> Ys = sprintf(',y%d',1:9);
>> str = sprintf('[%s,%s]',Xs(2:end),Ys(2:end));
>> vars = str2sym(str);
0 Kommentare
Weitere Antworten (2)
Jonathon Gibson
am 2 Jul. 2018
You could combine [x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6] beforehand into a matrix XY. You would iteratively concatenate your x variables X = [x1,x2,x3,...] and your y variables Y = [y1,y2,y3,...] and then combine them at the end XY = [X, Y].
X = [];
Y = [];
for i = 1:n
x_i = rand(10,1);
X = [X, x_i];
y_i = rand(10,1);
Y = [Y, y_i];
end
XY = [X, Y];
[eqs,vars] = reduceDifferentialOrder(eqn,XY);
Just replace rand(10,1) with however you are initializing your variables.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!