For loop inside a vector

1 次查看(过去 30 天)
bloodtalon
bloodtalon 2016-10-14
评论: bloodtalon 2016-10-14
So I have this variable e which stores 14 equations and now I want to use equationsToMatrix with these 14 equations. I can do them individually like this
[A, b] = equationsToMatrix([e(1) == 0, e(2) == 1,e(3) == 0],[v12 v23 v24 v34 v35 v45 v36 v57 v58 v49])
but I want to do it using a for loop inside the vector or something and tried this which gave me a "Illegal use of reserved keyword "for"" error.
[A, b] = equationsToMatrix([
for i=1:14
e(i)==0,
end
], [v12 v23 v24 v34 v35 v45 v36 v57 v58 v49])
I had also tried something like storing the equations in an array like
for i=1:14
equations(i)=e(i)==0
end
But that just gives me a zero vector of 1x14 thinking I want the logical output.
If you want the code for the e's and stuff it's here from the start just FYI
% Define symbols
syms p2 p3 p4 p5 v12 v23 v24 v34 v35 v45 v36 v57 v58 v49
% Define given pressures
p1=300;p6=100;p7=100;p8=100;p9=100;
% Define indices
ind=[12 23 24 34 35 45 36 57 58 49];
% Define d's
d=[.1 .08 .08 .1 .08 .08 .09 .09 .09 .09];
d2=d.^2;
% Define L's
l=[1000 800 800 900 800 800 1000 800 800 1000];
% Find alphas for momentum balances
a=32*0.1.*l./d2;
% Mass Balances
% Node 2
e(1)=d(ind==12)*v12-d(ind==23)*v23-d(ind==24)*v24;
% Node 3
e(2)=d(ind==23)*v23-d(ind==34)*v34-d(ind==35)*v35-d(ind==36)*v36;
% Node 4
e(3)=d(ind==24)*v24+d(ind==34)*v34-d(ind==45)*v45-d(ind==49)*v49;
% Node 5
e(4)=d(ind==35)*v35+d(ind==45)*v45-d(ind==57)*v57-d(ind==58)*v58;
% Momentum Balances
% 12
e(5)=p1-p2-a(ind==12)*v12;
% 23
e(6)=p2-p3-a(ind==23)*v23;
% 24
e(7)=p2-p4-a(ind==24)*v24;
% 34
e(8)=p3-p4-a(ind==34)*v34;
% 35
e(9)=p3-p5-a(ind==35)*v35;
% 36
e(10)=p3-p6-a(ind==36)*v36;
% 45
e(11)=p4-p5-a(ind==45)*v45;
% 49
e(12)=p4-p9-a(ind==49)*v49;
% 57
e(13)=p5-p7-a(ind==57)*v57;
% 58
e(14)=p5-p8-a(ind==58)*v58;
  1 个评论
bloodtalon
bloodtalon 2016-10-14
Nevermind seems I could just do e==0 and it does the job haha.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by