I am not getting correct graph from this code.What could be the problem

I have to plot bifurcation diagram which is basically a graph.The graph which should be obtained should much resemble the following graph
Much part of the code represents the conditions for getting the values of v,that part is correct.The problem is with graph plotting of k versus v.The code is giving much incorrect graph.The code is
% code
v=zeros(31,1);
v(1)=33;
for k=0.10:0.01:0.26;
for n=1:30
d(n)=0.4717-(k*(v(n)-25));
if d(n)<0
v(n+1)=(0.8872*v(n))
else if d(n)>1
v(n+1)=(0.8872*v(n))+(((1.2*33)*(33-v(n)))/(v(n)))
else
v(n+1)=(0.8872*v(n))+((((1.2)*(33)*(33-v(n)))*(d(n)^2))/(v(n)))
end
end
end
end
k=0.10:0.01:0.26
plot(k,v(1:17))
xlabel('k')
ylabel('v')
The incorrect graph obtained from the above code is

 Accepted Answer

You're only plotting the data from the last iteration of your outer for loop and so are missing all the others from
k=0.10:0.01:0.25
Try the using hold to retain the current plot when adding new ones. Your code could then look something like
close all;
figure;
hold on;
xlim([0.10 0.26]);
ylim([22 34]);
v=zeros(31,1);
v(1)=33;
for k=0.10:0.001:0.26;
for n=1:30
d(n)=0.4717-(k*(v(n)-25));
if d(n)<0
v(n+1)=(0.8872*v(n))
else
if d(n)>1
v(n+1)=(0.8872*v(n))+(((1.2*33)*(33-v(n)))/(v(n)))
else
v(n+1)=(0.8872*v(n))+((((1.2)*(33)*(33-v(n)))*(d(n)^2))/(v(n)))
end
end
end
plot(k,v)
pause(0.05)
end
xlabel('k')
ylabel('v')

9 Comments

@Geoff Hayes But this code is giving an empty graph.How to save values from previous iterations for plotting graph.
student - when I run the code that I put into my answer, then I see the following
If you wish to save the data from each iteration (for future analysis) then you need to save the v arrays as
close all;
figure;
hold on;
xlim([0.10 0.26]);
ylim([22 34]);
v=zeros(31,1);
v(1)=33;
myData = [];
atIteration = 1;
for k=0.10:0.001:0.26;
for n=1:30
d(n)=0.4717-(k*(v(n)-25));
if d(n)<0
v(n+1)=(0.8872*v(n));
else
if d(n)>1
v(n+1)=(0.8872*v(n))+(((1.2*33)*(33-v(n)))/(v(n)));
else
v(n+1)=(0.8872*v(n))+((((1.2)*(33)*(33-v(n)))*(d(n)^2))/(v(n)));
end
end
end
plot(k,v)
pause(0.05)
myData(atIteration,:) = v;
atIteration = atIteration + 1;
end
xlabel('k')
ylabel('v')
Note how myData is updated on each iteration of the outer for loop. Rather than initializing it as
myData = [];
I suggest pre-sizing the array since you know the number of iterations of the outer loop and the dimensions of v.
Thank you.There is some error on my side.I am using matlab 2017 and the same code is giving me empty graph.I could not know reason?
I'm using R2014a but that shouldn't be a problem. Try putting a break point at line 21 and look at the k and v data that you are about to plot. Does that data look correct?
Or try the second piece of code that I attached and then just plot the myData matrix once all iterations have completed.
The data for v is correct.I wanted to get the same graph which you got through my matlab too.Still I am getting blank graph.
I entered the code with command
% code
plot(k,v,'*')
It gave me correct graph.But still the graph which you got is plotted in a better way,as details are more visible through it.How to get the same graph as yours?The graph which I got is
Try replacing the * with a .
plot(k,v,'.')
But I think that should be the default...
yes,now this command has given correct graph.Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 18 Dec 2017

Commented:

on 21 Dec 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!