Filter löschen
Filter löschen

Use same algorithm for different data

3 Ansichten (letzte 30 Tage)
Ricardo López
Ricardo López am 7 Jan. 2021
Kommentiert: Mathieu NOE am 7 Jan. 2021
Good morning people,
I have a big algorithm and I would like to use different data without having to repeat the algorithm (for and if loops) many times. Any idea how?
I am pasting some part of my code as an example:
if j==1
Tin(j)=20;
Con(j) = H1(j)+Qel(j)*u(j);
NL_(j) = Con(j) - PV1(j);
Terr(j)=Ts(j)-Tin(j);
u(j)=0;
SOC(j)=0.5;
if Terr(j)>=1
u(j)=1;
end
end
I would like to change H1 for H2, H3,... Hn, as well as PV1, PV2,... PVn
  1 Kommentar
Mathieu NOE
Mathieu NOE am 7 Jan. 2021
hi
your code should be declared as a function and then you pass the arguments, and do loops for differents inputs
see matalb doc about how to create functions

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Rik
Rik am 7 Jan. 2021
The source of your problem is that you chose to put data (i.e. the counter) in the variable name.
You should consider storing H1-Hn in a cell array (or normal array) so you can trivially loop over them.
%don't use this, but make sure to do something like this when the data is created
H={H1,H2,H3};PV={PV1,PV2,PV3};
for n=1:numel(H)
if j==1
Tin(j)=20;
Con(j) = H{n}(j)+Qel(j)*u(j);
NL_(j) = Con(j) - PV{n}(j);
Terr(j)=Ts(j)-Tin(j);
u(j)=0;
SOC(j)=0.5;
if Terr(j)>=1
u(j)=1;
end
end
end

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by