How do I plot all my points in the same figure?

5 views (last 30 days)
I'm trying to plot the iterations in one figure, however, I'm getting over 80+ figures. Can anyone help me fix this? Sorry in advance if this code is disturbing.
The code is supposed to find all the iterations of V1 until the error converges to 0.001.
for i=0.0001:0.001:1
V1_iter= V1 - i;
error= (V1 - V1_iter)/V1;
plot(error,V1_iter,'r-*')
title('Graph of Iteration vs Error');
xlabel('Error');
ylabel('Iteration V1');
hold on;
if error == 0.001
end
figure
drawnow;

Answers (2)

MathReallyWorks
MathReallyWorks on 10 Jun 2017
Edited: MathReallyWorks on 10 Jun 2017
Hello sarah,
You are getting 80+ figures because you have used
figure
in your for loop.
This instruction always opens a new figure in each iteration.
Remove this line of your code, then it will work well.
If the above code is the part of main program then use "figure" just above your for loop.
Now, it works well:
for i=0.0001:0.001:1
V1_iter= V1 - i;
error= (V1 - V1_iter)/V1;
plot(error,V1_iter,'r-*')
title('Graph of Iteration vs Error');
xlabel('Error');
ylabel('Iteration V1');
hold on;
if error == 0.001
end
drawnow;
end

Image Analyst
Image Analyst on 10 Jun 2017
Edited: Image Analyst on 10 Jun 2017
Simply get rid of the call to "figure" in your loop. If you don't have it, it will plot into the current axes, or automatically create a figure with an axes on it for you if you don't already have a figure. So you don't need figure at all unless you already have an existing figure that you don't want to draw in and want to create a second separate axes.
Also, it's recommended that you don't use i or j (imaginary variables) for a loop index. Use k or something:
for k = 0.0001 : 0.001 : 1
V1_iter = V1 - k;
error = (V1 - V1_iter) / V1;
plot(error, V1_iter, 'r-*')
title('Graph of Iteration vs Error');
xlabel('Error');
ylabel('Iteration V1');
hold on;
if error == 0.001
end
drawnow;
end

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!