Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Index exceeds matrix Dimensions?

Subject: Index exceeds matrix Dimensions?

From: Sanaa

Date: 27 Mar, 2012 14:07:09

Message: 1 of 7

Greetings,
I am trying to plot a bifurcation diagram for the function given by
x_n = ru*x_(n-(1/3))*(1-x_(n-(1/3)))
for n = 1/3,2/3,1,...
For this I have two files

%This Mtlab function is needed to plot bifurcation diagram for the logistic map given
%by xn = ru*x_(n-1/3)(1-x_(n-1/3))
function [f]=bifurcationthird(n,ru)
f=0.5;
for i=1:3*n
        f= ru*f*(1-f);
  end
end

Then I call the function in the next file


%This Mtlab Code plot bifurcation diagram for the logistic map given
%by xn = ru*x_(n-1/3)(1-x_(n-1/3))

clear;
  n= 200;
ru = (3.0/n):(3.0/n):4;
for m = 1:3*n
      x(m)=bifurcationthird(m/3,ru(m));
end
plot(ru,x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I am keeping get the error index exceeds matrix dimensions?
WHERE is the problem if you please?

Subject: Index exceeds matrix Dimensions?

From: Matt J

Date: 27 Mar, 2012 15:29:21

Message: 2 of 7

"Sanaa" wrote in message <jkshid$42c$1@newscl01ah.mathworks.com>...
>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> I am keeping get the error index exceeds matrix dimensions?
> WHERE is the problem if you please?
================

If the question is "where", the error message should tell you. It should give you both the file name and the line number where the error is occurring.

Subject: Index exceeds matrix Dimensions?

From: Roger Stafford

Date: 27 Mar, 2012 17:45:21

Message: 3 of 7

"Sanaa" wrote in message <jkshid$42c$1@newscl01ah.mathworks.com>...
> ......
> n= 200;
> ru = (3.0/n):(3.0/n):4;
> for m = 1:3*n
> x(m)=bifurcationthird(m/3,ru(m));
> end
>......
- - - - - - - - -
  In the line

 ru = (3.0/n):(3.0/n):4;

you generate the array 'ru' containing only 266 elements, but the index m in the for-loop gets as high as m = 600. There is no 600th element of 'ru'. I think you may have meant to have ru = 4/(3*n):4/(3*n):4.

  You should realize that binary floating point numbers, which are what matlab uses for 'double', cannot represent 1/3 exactly, and that means that you are "skating on thin ice" when you write your code as you have. For example if you were to add up 1/3 six times and then multiply the sum by three, you would not get an exact six for an answer due to round off errors.

Roger Stafford

Subject: Index exceeds matrix Dimensions?

From: Sanaa

Date: 27 Mar, 2012 18:36:13

Message: 4 of 7


Thanks a lot for your help. It really helped me. May I ask how did you fit the dimensions together? I am new in Matlab. Another question if you please, did you mean that my code is not correct or what?
Thanks a lot in advance.

Subject: Index exceeds matrix Dimensions?

From: someone

Date: 27 Mar, 2012 18:59:14

Message: 5 of 7

"Sanaa" wrote in message <jkt1at$21a$1@newscl01ah.mathworks.com>...
>
> Thanks a lot for your help. It really helped me. May I ask how did you fit the dimensions together?

Probably by inspection & years of experience. You need to decide what is the actual dimension of ru. 266 or 600?

 I am new in Matlab. Another question if you please, did you mean that my code is not correct or what?

This link may help explain:
<<http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F>>

% You may want to change:
ru = (3.0/n):(3.0/n):4;
% to something like:
ru = round((3.0/n):(3.0/n):4);

doc round
doc ceil
doc fix
doc floor

> Thanks a lot in advance.

Subject: Index exceeds matrix Dimensions?

From: Roger Stafford

Date: 27 Mar, 2012 19:54:18

Message: 6 of 7

"Sanaa" wrote in message <jkt1at$21a$1@newscl01ah.mathworks.com>...
>
> Thanks a lot for your help. It really helped me. May I ask how did you fit the dimensions together? I am new in Matlab. Another question if you please, did you mean that my code is not correct or what?
> Thanks a lot in advance.
- - - - - - - - -
  If you are directing your question to me and if you are asking about the suggestion of:

 ru = 4/(3*n):4/(3*n):4;

the reasoning is that you clearly wanted m to range from 1 to 3*n, and at the same time ru from (just above) 0 to 4. Hence, 4 divided by 3*n gives a subinterval length of 4/(3*n) and that leads to the above suggestion.

  As far as I can make out, your code will probably work correctly as it stands except for the erroneous 'ru' array. However I would much rather see you write:

 ru = linspace(4/(3*n),4,3*n);

instead of 4/(3*n):4/(3*n):4 . Then there would be no question of getting the proper number of elements in 'ru'.

Roger Stafford

Subject: Index exceeds matrix Dimensions?

From: Sanaa

Date: 27 Mar, 2012 20:12:19

Message: 7 of 7

Thank you very much for your simple explanations. This really works very good.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us