I am not getting correct graph from this code.What could be the problem
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
Geoff Hayes
on 18 Dec 2017
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.
Geoff Hayes
on 21 Dec 2017
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.
student
on 21 Dec 2017
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?

Geoff Hayes
on 21 Dec 2017
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?
Geoff Hayes
on 21 Dec 2017
Or try the second piece of code that I attached and then just plot the myData matrix once all iterations have completed.
student
on 21 Dec 2017
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.
student
on 21 Dec 2017
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

Geoff Hayes
on 21 Dec 2017
Try replacing the * with a .
plot(k,v,'.')
But I think that should be the default...
student
on 21 Dec 2017
yes,now this command has given correct graph.Thank you.
More Answers (0)
Categories
Find more on Mathematics in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)