make the for loop run faster

3 Ansichten (letzte 30 Tage)
Asliddin Komilov
Asliddin Komilov am 14 Jul. 2019
Kommentiert: Asliddin Komilov am 14 Jul. 2019
I would appreciate any help to optimize this code since it runs 86400x365x19x37 times:
for ii = 1 : length(time)
if time(ii)<=sunrise
Ta(ii)=Tmin;
elseif time(ii)>=sunrise && time(ii)<=Tmax_time
Ta(ii)=Tmin+(Tmax-Tmin)*sin((time(ii)-sunrise)/(Tmax_time-sunrise)*pi/2);
elseif time(ii)>Tmax_time && time(ii)<=sunset
Ta(ii)=T0+(Tmax-T0)*sin(pi/2+(time(ii)-Tmax_time)*pi/(2*4));
elseif time(ii)>sunset && time(ii)<=Day_end
Ta(ii)=T0+(Tmin-T0)/(Day_end-sunset).^0.75*(time(ii)-sunset).^0.75;
end
end
thanks

Akzeptierte Antwort

infinity
infinity am 14 Jul. 2019
Hello,
How about if you try to use this
time = 1:100;
T0 = 0;
sunrise = 10;
Tmin = 1;
Tmax = 100;
Tmax_time = 50;
sunset = 70;
Day_end = 100;
Ta = (time <= sunrise).* Tmin...
+ (time > sunrise & time <=Tmax_time).*...
(Tmin+(Tmax-Tmin)*sin((time-sunrise)/(Tmax_time-sunrise)*pi/2)) ...
+ (time > Tmax_time & time <=sunset).*...
(T0+(Tmax-T0)*sin(pi/2+(time-Tmax_time)*pi/(2*4)))...
+ (time>sunset & time<=Day_end).*...
(T0+(Tmin-T0)/(Day_end-sunset).^0.75*(time-sunset).^0.75);
You could apply for your data of "time, T0, sunrise, etc.".
  1 Kommentar
Asliddin Komilov
Asliddin Komilov am 14 Jul. 2019
Elapsed time is 2 times shorter.
thanks

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Problem-Based Optimization Setup finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by