For Loop range error
显示 更早的评论
I am running the following for-loop to evaluate over a range of time, over 0.5s steps. But when I run the function it gives me a result I cant comprehend. The result is evaluated over steps of 0.0005s, I have no clue why it is this way. Can someone please help?
Tab=[];
for t= -5.0:0.5:50.0 %s
if t>=0 & t<=10
v=(11*(t^2))-(5*t)
elseif t>=10 & t<=20
v=1100-(5*t)
elseif t>=20 & t<=30
v=(50*t)+2*((t-20)^2)
elseif t>30
v=1520* exp(-0.2*(t-30))
else
v=0
end
Tab=[Tab; t v]
end
Tab
Result:
Tab =
1.0e+03 *
-0.0050 0
-0.0045 0
-0.0040 0
-0.0035 0
-0.0030 0
-0.0025 0
-0.0020 0
-0.0015 0
-0.0010 0
-0.0005 0
0 0
0.0005 0.0003
0.0010 0.0060
0.0015 0.0173
0.0020 0.0340
0.0025 0.0563
0.0030 0.0840
0.0035 0.1172
0.0040 0.1560
0.0045 0.2003
0.0050 0.2500
0.0055 0.3053
0.0060 0.3660
0.0065 0.4323
0.0070 0.5040
0.0075 0.5813
0.0080 0.6640
0.0085 0.7522
0.0090 0.8460
0.0095 0.9453
0.0100 1.0500
0.0105 1.0475
0.0110 1.0450
0.0115 1.0425
0.0120 1.0400
0.0125 1.0375
0.0130 1.0350
0.0135 1.0325
0.0140 1.0300
0.0145 1.0275
0.0150 1.0250
0.0155 1.0225
0.0160 1.0200
0.0165 1.0175
0.0170 1.0150
0.0175 1.0125
0.0180 1.0100
0.0185 1.0075
0.0190 1.0050
0.0195 1.0025
0.0200 1.0000
0.0205 1.0255
0.0210 1.0520
0.0215 1.0795
0.0220 1.1080
0.0225 1.1375
0.0230 1.1680
0.0235 1.1995
0.0240 1.2320
0.0245 1.2655
0.0250 1.3000
0.0255 1.3355
0.0260 1.3720
0.0265 1.4095
0.0270 1.4480
0.0275 1.4875
0.0280 1.5280
0.0285 1.5695
0.0290 1.6120
0.0295 1.6555
0.0300 1.7000
0.0305 1.3754
0.0310 1.2445
0.0315 1.1260
0.0320 1.0189
0.0325 0.9219
0.0330 0.8342
0.0335 0.7548
0.0340 0.6830
0.0345 0.6180
0.0350 0.5592
0.0355 0.5060
0.0360 0.4578
0.0365 0.4142
0.0370 0.3748
0.0375 0.3392
0.0380 0.3069
0.0385 0.2777
0.0390 0.2513
0.0395 0.2273
0.0400 0.2057
0.0405 0.1861
0.0410 0.1684
0.0415 0.1524
0.0420 0.1379
0.0425 0.1248
0.0430 0.1129
0.0435 0.1022
0.0440 0.0924
0.0445 0.0836
0.0450 0.0757
0.0455 0.0685
0.0460 0.0620
0.0465 0.0561
0.0470 0.0507
0.0475 0.0459
0.0480 0.0415
0.0485 0.0376
0.0490 0.0340
0.0495 0.0308
0.0500 0.0278
4 个评论
Walter Roberson
2021-1-25
I do not see anything wrong at the moment?
I suggest that you command
format long g
and look at the output again.
Shantanu Chatterji
2021-1-25
Walter Roberson
2021-1-25
format long g does not change the calculation, only the way the output is displayed. You overlooked the early lines of the output
Tab =
1.0e+03 *
That means that you have to mentally multiply all of the displayed results by 1E3 (that is, 1000) to get the stored value, and that the portion of values less than 0.001*1E3 have been discarded for display purposes.
I personally wish that Mathworks would stop making format short the default. I have seen too many people caught by it.
Shantanu Chatterji
2021-1-25
回答(1 个)
KSSV
2021-1-25
t0 = -0.5 ; % initial time
t1 = 50 ; % final time
dt = 0.5 ; % time step
T = t0:dt:t1 ; % create time vector
N = length(T) ; % length of time steps
Tab = zeros(N,1) ; % initialize velocity
for i = 1:N
t = T(i) ;
if t>=0 && t<=10
v=(11*(t^2))-(5*t) ;
elseif t>=10 && t<=20
v=1100-(5*t) ;
elseif t>=20 && t<=30
v=(50*t)+2*((t-20)^2) ;
elseif t>30
v=1520* exp(-0.2*(t-30)) ;
else
v=0 ;
end
Tab(i,:) = v ;
end
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!