how to set a loop to calculate time

4 次查看(过去 30 天)
I'm trying to calculate time starting at 0.01 hrs and increasing by 1.1 until it reaches 24 hrs,
for i=1:83
t=0.01
t(i)=t(i)*1.1;
end
and I also need it to have a max value of 24 that the final time calculated even if it's more than 24 it comes back as 24.

采纳的回答

Torsten
Torsten 2022-3-23
t0 = 0.01;
n = log(24/t0)/log(1.1);
t = t0*1.1.^(0:floor(n));
t(end+1) = 24.0;

更多回答(1 个)

John D'Errico
John D'Errico 2022-3-23
EVERY pass through your loop, you REDEFINE the variable t. Learn to use MATLAB. Stop thinking in terms of loops for everything.
t = 0.1:1.1:24;
t(end)
ans = 23.2000
Note that, using this increment and start point, the last value of t will be 23.2, not 24. So are you saying that then you want to append one more element to the end of t?
if t(end) < 24
t(end + 1) = 24;
end

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by