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

조회 수: 5 (최근 30일)
Sarah AlMehairi
Sarah AlMehairi 2017년 6월 10일
편집: MathReallyWorks 2017년 6월 10일
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;

답변 (2개)

MathReallyWorks
MathReallyWorks 2017년 6월 10일
편집: MathReallyWorks 2017년 6월 10일
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 2017년 6월 10일
편집: Image Analyst 2017년 6월 10일
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

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by