How can I write a system of equation more fastly

1 Ansicht (letzte 30 Tage)
Nello Troccia
Nello Troccia am 13 Mär. 2012
Hi, I have to write a function and this function is a big system of non linear equation but this system is repetitive. See later:
function F = funzioni(x)
global Ts0 Tgr vs Ros Cs lx h S Tg
F = [x(1)-Ts0;
x(2)-Ts0;
x(3)-Ts0;
x(4)-Ts0;
x(5)-Ts0;
x(6)-Tgr;
vs*Ros*Cs*((x(12)+x(2)-x(7))/(2*lx))-(h*S*(Tg-x(7)));
vs*Ros*Cs*((x(13)+x(3)-x(8))/(2*lx))-(h*S*(Tg-x(8)));
vs*Ros*Cs*((x(14)+x(4)-x(9))/(2*lx))-(h*S*(Tg-x(9)));
x(10)-x(9);
x(11)-Tgr;
vs*Ros*Cs*((x(17)+x(7)-x(12))/(2*lx))-(h*S*(Tg-x(12)));
vs*Ros*Cs*((x(18)+x(8)-x(13))/(2*lx))-(h*S*(Tg-x(13)));
vs*Ros*Cs*((x(19)+x(9)-x(14))/(2*lx))-(h*S*(Tg-x(14)));
x(15)-x(14);
x(16)-Tgr;
vs*Ros*Cs*((x(22)+x(17)-x(12))/(2*lx))-(h*S*(Tg-x(17)));
vs*Ros*Cs*((x(23)+x(18)-x(13))/(2*lx))-(h*S*(Tg-x(18)));
vs*Ros*Cs*((x(24)+x(19)-x(14))/(2*lx))-(h*S*(Tg-x(19)));
x(20)-x(19);
x(21)-Tgr;
vs*Ros*Cs*((x(27)+x(17)-x(22))/(2*lx))-(h*S*(Tg-x(22)));
vs*Ros*Cs*((x(28)+x(18)-x(23))/(2*lx))-(h*S*(Tg-x(23)));
vs*Ros*Cs*((x(29)+x(19)-x(24))/(2*lx))-(h*S*(Tg-x(24)));
x(25)-x(24);
x(26)-Tgr;
x(27)-x(22);
x(28)-x(23);
x(29)-x(24);
x(30)-x(29);];
how can I write this system more fastly??? thanks a lot

Antworten (1)

Walter Roberson
Walter Roberson am 13 Mär. 2012
Write simple helper expressions.
LK1 = @(K) vs*Ros*Cs*((x(K+10)+x(K)-x(K+5))/(2*lx))-(h*S*(Tg-x(K+5)));
LK2 = @(K) vs*Ros*Cs*((x(K+5)+x(K)-x(K-5))/(2*lx))-(h*S*(Tg-x(K)));
[....
X(6)-Tgr;
LK1(2);
LK1(3);
LK1(4);
x(10)-x(9);
x(11)-Tgr;
LK1(7);
LK1(8);
LK1(9);
x(15)-x(14);
x(16)-Tgr;
LK2(17);
LK2(18);
LK2(19);
....]
  1 Kommentar
Nello Troccia
Nello Troccia am 14 Mär. 2012
Ok Ok!!! I found the error. But when I ask how can write the system I mean if I can use a structure like this:
function F = funzioniIndicizzate(x)
global n m Ts0 Tgr vs Ros Cs lx h S Tg
for z=1:1:m %prima colonna
F(z)=x(z)-Ts0;
end
for z=m+1:m:(m*n) %prima riga
F(z)=x(z)-Tgr;
end
for p=2:1:(n-1) %corpo centrale
for z=(m*n)-(m-2):1:(m*p)-1
F(z)=vs*Ros*Cs*((x(z+m)+x(z-m)-2*x(z))/(2*lx))-(h*S*(Tg-x(z)));
end
end
for z=m:m:(m*n) %ultima riga
F(z)=x(z)-x(z-1);
end
for z=(m*n)-m:1:m*n %ultima colonna
F(z)=x(z)-x(z-m);
end
Unfortunately this doesn't work well

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by