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

5 vues (au cours des 30 derniers jours)
Sarah AlMehairi
Sarah AlMehairi le 10 Juin 2017
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;

Réponses (2)

MathReallyWorks
MathReallyWorks le 10 Juin 2017
Modifié(e) : MathReallyWorks le 10 Juin 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 le 10 Juin 2017
Modifié(e) : Image Analyst le 10 Juin 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

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by