make the for loop run faster

2 ビュー (過去 30 日間)
Asliddin Komilov
Asliddin Komilov 2019 年 7 月 14 日
コメント済み: Asliddin Komilov 2019 年 7 月 14 日
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

採用された回答

infinity
infinity 2019 年 7 月 14 日
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 件のコメント
Asliddin Komilov
Asliddin Komilov 2019 年 7 月 14 日
Elapsed time is 2 times shorter.
thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProblem-Based Optimization Setup についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by