odeFunction: List variables through loop

조회 수: 1 (최근 30일)
Tejas Adsul
Tejas Adsul 2018년 7월 2일
편집: Stephen23 2018년 7월 3일
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
Stephen23
Stephen23 2018년 7월 3일
편집: Stephen23 2018년 7월 3일
@Tejas Adsul: numbered variables are a sign that you are doing something wrong. Read this to know why:
Tejas Adsul
Tejas Adsul 2018년 7월 3일
I tried using cell array. I created T1 = {sym('x',[n,1])}, and then wrote T1{1} instead of x1,x2,etc. It still gave me an invalid variable '0' error.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 7월 3일
편집: Stephen23 2018년 7월 3일
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);

추가 답변 (2개)

Jonathon Gibson
Jonathon Gibson 2018년 7월 2일
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.
  댓글 수: 3
Stephen23
Stephen23 2018년 7월 3일
편집: Stephen23 2018년 7월 3일
It is recommended to preallocate the arrays before the loop:
Tejas Adsul
Tejas Adsul 2018년 7월 3일
Okay no, this does not work. I'm not creating new variables. I already have x1,x2 etc in my equations. I just need to list them out in vars. I initialized my variables as x = sym('x',[n,1]) and same for y. But just writing 'x' in reduceDifferentialOrder didn't help. It gave 'invalid variable 0' error. Only explicitly writing out x1,x2,x3 etc does the job.

댓글을 달려면 로그인하십시오.


Tejas Adsul
Tejas Adsul 2018년 7월 3일
I found out a way. The reason I kept getting invalid variable '0' error was my equations had variables dependent on t (x1(t), x2(t),etc) while the variables I was providing in vars matrix were independent of t (x1, x2, etc). Anyway, what I did was create a text file vars.txt, and used fprintf in a loop to create the vars matrix in string form. Then, I read from that file using fileread, and used str2sym to get my vars matrix. It's not the most elegant way, but it works for now. Here's the code, if it helps anyone:
fileID = fopen('vars.txt','wt');
fprintf(fileID,'[');
for i = 1:n
fprintf(fileID,'x%d(t),',i);
end
for i = 1:n-1
fprintf(fileID,'y%d(t),',i);
end
fprintf(fileID,'y%d(t)]',i+1);
filename = 'vars.txt';
filetext = fileread(filename);
vars = str2sym(filetext);

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by