make the for loop run faster

2 views (last 30 days)
Asliddin Komilov
Asliddin Komilov on 14 Jul 2019
Commented: Asliddin Komilov on 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

Accepted Answer

infinity
infinity on 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.".

More Answers (0)

Categories

Find more on Problem-Based Optimization Setup in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!