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:
Problem with plot

Subject: Problem with plot

From: Sanaa

Date: 31 Mar, 2012 18:21:12

Message: 1 of 6

Greetings,
I am trying to plot the bifurcation diagram for the logistic map
xn= ru*x_(n-1/2)(1-x_(n-1/2))
with the two files
>>>>>>>>>>
function [flo]=bifurcation(n,ru)
flo=0.5;

for i=1:2*n
        flo= ru*flo*(1-flo);
        
end
end
 
and

clear;
  n= 150;
 ru = (2.0/n):(2.0/n):4;
for m = 1:2*n
      x(m)=bifurcation(m/2,ru(m));
end

plot(ru,x)

 and I want to get a similar picture as in the standard map xn= ru*x*(1-x) whose code is given by
>>>>>>>>>>>>
clear
itermax=300;
finalits=30;finits=itermax-(finalits-1);
for ru=0:0.005:4
    x=.5;
    xo=x;
    for n=2:itermax
        xn=ru*xo*(1-xo);
        x=[x xn];
        xo=xn;
    end
    plot(ru*ones(finalits),x(finits:itermax),'.','MarkerSize',1)
    hold on
end
fsize=15;
set(gca,'XTick',0:1:4,'FontSize',fsize)
set(gca,'YTick',0:0.2:1)
xlabel('{ru}','FontSize',fsize)
ylabel('x','FontSize',fsize)
hold off
>>>>>>>>>>>>>>>>>>>

I always get error when I put the later lines in my first code! Any help please

Subject: Problem with plot

From: Roger Stafford

Date: 31 Mar, 2012 18:50:21

Message: 2 of 6

"Sanaa" wrote in message <jl7huo$593$1@newscl01ah.mathworks.com>...
> plot(ru*ones(finalits),x(finits:itermax),'.','MarkerSize',1)
- - - - - - - -
  Check up on the documentation for 'ones'. That is the source of your trouble.

Roger Stafford

Subject: Problem with plot

From: Sanaa

Date: 31 Mar, 2012 19:14:33

Message: 3 of 6

Thanks a lot for your reply. I modified my file to be like this

clear;
Npre = 200; n = 150;
x = zeros(n,1);
for ru = (2.0/n):(2.0/n):4;
    x(1)= 0.5;
    for n = 1:Npre
    x(1)= bifurcation(1/2,ru);
    end
for m = 1:2*n
      x(m+1)=bifurcation((m+1)/2,ru);
end
plot(ru, x, '.', 'markersize', 2);
  hold on;
%plot(ru,x)
end
title('Bifurcation diagram of the logistic map');
xlabel('ru'); ylabel('x_n');
set(gca, 'xlim', [2.5 4.0]);
hold off;

This is in addition to the function file

function [flo]=bifurcation(n,ru)
flo=0.5;

for i=1:2*n
        flo= ru*flo*(1-flo);
        
end
end
 but still do not get a standard picture as required! Any help please

Subject: Problem with plot

From: Sanaa

Date: 31 Mar, 2012 19:55:13

Message: 4 of 6

I just want to know why do I get so many colors and lines in my plot?!!!

Subject: Problem with plot

From: Roger Stafford

Date: 31 Mar, 2012 20:12:12

Message: 5 of 6

"Sanaa" wrote in message <jl7l2p$f6r$1@newscl01ah.mathworks.com>...
> Thanks a lot for your reply. I modified my file to be like this
>
> clear;
> Npre = 200; n = 150;
> x = zeros(n,1);
> for ru = (2.0/n):(2.0/n):4;
> x(1)= 0.5;
> for n = 1:Npre
> x(1)= bifurcation(1/2,ru);
> end
> for m = 1:2*n
> x(m+1)=bifurcation((m+1)/2,ru);
> end
> plot(ru, x, '.', 'markersize', 2);
> hold on;
> %plot(ru,x)
> end
> title('Bifurcation diagram of the logistic map');
> xlabel('ru'); ylabel('x_n');
> set(gca, 'xlim', [2.5 4.0]);
> hold off;
>
> This is in addition to the function file
>
> function [flo]=bifurcation(n,ru)
> flo=0.5;
>
> for i=1:2*n
> flo= ru*flo*(1-flo);
>
> end
> end
> but still do not get a standard picture as required! Any help please
- - - - - - - - - -
  I don't know what the "standard picture" is that you expect, but I can make a couple of comments on the code.

1. What is the point of the following loop?

    for n = 1:Npre
     x(1)= bifurcation(1/2,ru);
    end

Two hundred times it overwrites the 0.5 that was placed in x(1) with the same quantity, ru/4. It seems an exercise in futility.

2. You defined n = 150 but then used it in the aforementioned loop where it will presumably be changed to 200. Is that what you planned?

  I would like to make a general recommendation about your procedure, Sanaa. In any coding of this nature it is almost mandatory that you engage in a continual checking process as you develop the code. If possible, check results at each stage and see that the values and dimensions of your arrays are what you expect. Try things out on very small arrays at first. For example, if you had checked the size of 'ones(finalits)' before using it in 'plot' you would have immediately discovered the error there.

  I have the impression that you are writing an entire code without checking any of it out and then expecting it to run correctly on the first trial. That is not a good practice until you get to be a very expert programmer. I have been programming in various languages for a large fraction of my life and still have to check things as I go to avoid ending up with code that is overwhelmingly difficult to debug.

Roger Stafford

Subject: Problem with plot

From: Sanaa

Date: 31 Mar, 2012 21:02:12

Message: 6 of 6

Dear sir, you are completely right. I should check every single step in my code. I deleted unnecessary lines(as you advised) out of my code. Now I am having this code:
clear;
 n = 100;
for ru =linspace(4/(3*n),4,3*n);
for m = 1:3*n
      x(m)=bifurcationthird((m)/3,ru);
end
plot(ru*ones(3*n,1), x, '.', 'markersize', 2);
  hold on;
end
fsize=15;
title('Bifurcation diagram of the logistic map');
xlabel('ru'); ylabel('x_n');
set(gca,'XTick',0:1:4,'FontSize',fsize)
set(gca,'YTick',0:0.2:1)
hold off;

The question is : why there are so many lines in the beginning of my plot?
I really appreciate your help.

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