Beginner - can't get plot/graph of "discrete logistic model"...

3 views (last 30 days)
I am a beginner MATLAB user and am working on the following example:
++++++++++++++++++++++++++++
A very interesting iterative relationship much studied recently is defined by
y(k+1) = r*y(k)*(1 − y(k))
(this is a discrete form of the well-known logistic model). Given y0 and r , successive yk ’s may be easily computed. For example, if y0 = 0.2 and r = 1, then y1 = 0.16, y2 = 0.1334, and so on. This formula is often used to model population growth in cases where growth is limited, restricted by shortages of food, living area, and the like. yk exhibits fascinating behavior, known as mathematical chaos, for values of r between 3 and 4 (independent of y0).Write a program that plots yk against k (as individual points). Values of r that give particularly interesting graphs are 3.3, 3.5, 3.5668, 3.575, 3.5766, 3.738, 3.8287, and many more that can be found by patient exploration.
++++++++++++++++++++++++++++
I have written a code but the resulting graph doesn't seem to be particularly interesting for any values. Is there something wrong in my code? If so, what? I am clumsy with indexing but it's not obvious to me what's gone awry. Here is the code:
++++++++++++++++++++++++++++
r=1 %at first, then values from 3-4
for k=1:20;
y(k+1)=r*y(k)*(1-y(k));
end
plot(k,y,'o')
++++++++++++++++++++++++++
EDIT: I think maybe I'm using the wrong plotting function? Here's another similar example where I'm having the same problem. Can't see what is wrong with my code :(
++++++++++++++++++++++++++
PROBLEM:
A rather beautiful fractal picture can be drawn by plotting the points (xk , yk ) generated by the following difference equations
x(k+1) = y(k) (1 + sin 0.7*x(k) ) − 1.2*sqrt(x(k)),
y(k+1) = 0.21 − x(k),
starting with x0 = y0 = 0. Write a program to draw the picture (plot individual points; do not join them).
++++++++++++++++++++++++++
CODE:
for k=0:20
x(k+1)=y(k)*(1+sin(0.7*x(k)))-1.2*sqrt(abs(x(k)));
y(k+1)=0.21-x(k);
end
plot(x,y,'o')

Accepted Answer

Kelly Kearney
Kelly Kearney on 13 Feb 2014
In your first example, you haven't shown us what value you used for y(1). Make sure to set
y(1) = 0.2;
before running the loop. The assignment says that the function will exhibit chaos "independent of y0", but really y0 has to be >0 and <1 to exhibit the patterns, so if you start at, for example, 1, you're going to get a very boring plot.
  3 Comments
Kelly Kearney
Kelly Kearney on 13 Feb 2014
Matlab doesn't assume anything... like all computer languages, it does exactly what you tell it to, no more, no less.
But other than the issue with initialization, the only real problem I see is that you're trying to plot vs. k, but k at that point is only a scalar (the last value of the for-loop, k=20). Changing that, and adding some preallocation (and assignment of k=1 values) for x and y, and everything looks fine.
% Example 1
r=3.8;
nk = 100;
y = zeros(1,nk);
y(1) = 0.2;
for k=1:nk-1;
y(k+1)=r*y(k)*(1-y(k));
end
subplot(2,1,1);
plot(1:nk,y,'-');
% Example 2
nk = 100;
x = zeros(nk,1);
y = zeros(nk,1);
for k=1:nk-1
x(k+1)=y(k)*(1+sin(0.7*x(k)))-1.2*sqrt(abs(x(k)));
y(k+1)=0.21-x(k);
end
subplot(2,1,2);
plot(x,y,'.');
axis equal;
B.M.
B.M. on 14 Feb 2014
Sorry, I meant I assumed you would assume I had put in y0/y1=0.2.
Thanks, Kelly. I'll have to pour over the code til I figure it out, but I certainly see plots you've created which are much more in line with what I'd expect from the text.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots 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!