how to set a loop to calculate time

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.

 Accepted Answer

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

More Answers (1)

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

Categories

Find more on Loops and Conditional Statements 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!