Attempted to access y(15); index must be a positive integer or logical.
Show older comments
Here is my code:
clear;
for t=0:0.01:4;
i=t*100+1;
if t<2
y(i)=3.5*exp(-t/2)*cos(2*pi*t);
T(i)=t;
else
y(i)=3.5*exp(-1)*cos(6*pi*t);
T(i)=t;
end
plot(T, y,'g','linewidth',2)
end
As you can see, I want to plot the piecewise function. To store y and t as vectors, I created an index i . But when i equals 15, it reports 'Attempted to access y(15); index must be a positive integer or logical.' But when t=0.14, i=15 and it makes sense, I think.So how can it be wrong? Please help me with the problem. Thanks!
Accepted Answer
More Answers (1)
Iain
on 4 Sep 2014
0 votes
Its a floating point error down at the 15th significant figure. If you simply round "i", it'll work.
Categories
Find more on Logical 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!