error:Attempted to access F2(6); index must be a positive integer or logical.

  • Follow


I am trying to create an array inside a for loop and then I will plot it in MATLAB. but I am tring to do it by incrementing the variable by 0.01 at one time. like this:

for w = 0.01:0.01:20
      F2(100*w) = G/abs(w^4 - C*G*w^2 + G);
end

it gives me the error : Attempted to access F2(6); index must be a positive integer or logical.

this seems silly because 6 is a positive integer index and also it can make the code work until 6 but at 6 it gives an error

what am I doing wrong?? is there any other way for me to do what I want??

thanks in advance
0
Reply Kerem 11/9/2010 9:11:04 PM

"Kerem  tezcan" <kctezcan@gmail.com> wrote in message <ibcdd8$t2s$1@fred.mathworks.com>...
> I am trying to create an array inside a for loop and then I will plot it in MATLAB. but I am tring to do it by incrementing the variable by 0.01 at one time. like this:
> 
> for w = 0.01:0.01:20
>       F2(100*w) = G/abs(w^4 - C*G*w^2 + G);
> end
> 
> it gives me the error : Attempted to access F2(6); index must be a positive integer or logical.
> 
> this seems silly because 6 is a positive integer index and also it can make the code work until 6 but at 6 it gives an error
> 
> what am I doing wrong?? is there any other way for me to do what I want??
> 
> thanks in advance

dont do this in a for loop, the following is faster:

w = 0.01:0.01:20;
F2 = G/abs(w.^4 - C*G*w.^2 + G);
0
Reply John 11/9/2010 9:26:04 PM


On 10-11-09 03:11 PM, Kerem tezcan wrote:

> for w = 0.01:0.01:20
> F2(100*w) = G/abs(w^4 - C*G*w^2 + G);
> end

It is not possible for binary floating point to exactly represent 0.01, and 
not potentially possible to exactly represent any binary fractions in your 
specified range other than 0.25, 0.5, 0.75, 1.0, and integers added to those 
values. When those inexact binary representations are multiplied by 100, the 
resulting number will often not be an exact integer, but the difference 
between an exact integer and the actual number might be so small that the 
error message rounds off to the integer during the printout.

The difference between 6 and the 6-like entry that appears in 
(0.01:.01:20)*100 is only 8.88178419700125e-16 but that is enough for the 
indexing routine to complain about it being non-integral.

You are advised to use full integers:

for w = 1:2000
F2(w) = G/abs((w/100)^4 - C*G*(w/100)^2 + G);
end

Which of course can be recoded as

w = 1:2000;
F2 = G/abs((w/100).^4 - C*G*(w/100).^2 + G);

without any loop.
0
Reply Walter 11/9/2010 9:28:53 PM


"Kerem  tezcan" <kctezcan@gmail.com> wrote in message 
news:ibcdd8$t2s$1@fred.mathworks.com...
> I am trying to create an array inside a for loop and then I will plot it 
> in MATLAB. but I am tring to do it by incrementing the variable by 0.01 at 
> one time. like this:
>
> for w = 0.01:0.01:20
>      F2(100*w) = G/abs(w^4 - C*G*w^2 + G);
> end
>
> it gives me the error : Attempted to access F2(6); index must be a 
> positive integer or logical.
>
> this seems silly because 6 is a positive integer index and also it can 
> make the code work until 6 but at 6 it gives an error

In this scenario, 100*w is NOT exactly 6 but is very close to 6 (so close 
that it is _displayed_ as 6.)  Read this document for more information:

http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf

> what am I doing wrong?? is there any other way for me to do what I want??

w = 0.01:0.01:20;
F2 = zeros(size(w)); % Make sure to preallocate
for k = 1:numel(w)
    F2(k) = G/abs(w(k)^4-C*G*w(k)^2+G);
end

or:
w = 0.01:0.01:20;
F2 = G./abs(w.^4-C*G*w.^2+G);

-- 
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on 
http://www.mathworks.com 

0
Reply Steven_Lord 11/9/2010 9:31:11 PM

3 Replies
687 Views

(page loaded in 0.038 seconds)

Similiar Articles:











7/22/2012 3:16:35 AM


Reply: