Why plot is over plotted?

%i degree for curve fitting
%j is the iterating number for subplot title
x=[1:10]
y=sin(x)
for i=10:10:100
title([' degree = ' ,num2str(i)])
for j=1:10
subplot(10,1,j)
p= polyfit(x,y,i)
x2=[1:0.01:10]
y2=polyval(p,x2)
plot(x,y,'o',x2,y2,'-b')
hold on
end
end

 Risposta accettata

Jan
Jan il 11 Lug 2018
x = 1:10; % No need for square brackets
y = sin(x);
for k = 1:10
% NextPlot='add' is the same as "hold on"
AxesH(k) = subplot(10, 1, k, 'NextPlot', 'add');
end
x2 = 1:0.01:10;
for i = 10:10:100
for j = 1:10
p = polyfit(x,y,i);
y2 = polyval(p,x2);
plot(AxesH(j), x,y,'o', x2,y2,'-b');
end
end
subplot clears formerly existing axes objects. So create the axes at first and use them as parent object as first input to plot.

7 Commenti

madhan ravi
madhan ravi il 11 Lug 2018
But I want to see degree of polynomial in each subplot?
Please explain it. What do you want to see where? In your original code you had a
title([' degree = ' ,num2str(i)])
But this was overwritten in each iteration over i. I do not understand the purpose of the inner loop over j at all. Is it intended that all diagrams have the same contents?
madhan ravi
madhan ravi il 11 Lug 2018
I want to see the degree of polynomial in each subplot and eac subplot is curve fitted according to i , I don’t know how use loop in this scenario , if you could help me debug the code I would be so thankful.
I want to see the degree of polynomial in each subplot
Please, madhan ravi, remember that I cannot read your mind. How do you want to "see" this? By a legend and lines in different colors? By a title above each subplot? By different markers for the lines? By annotations or text beside the lines?
I cannot help you to debug the code, when it is not clear, what the bug is. What do you want to achieve?
madhan ravi
madhan ravi il 12 Lug 2018
Sorry jan for being unclear . All I want to do is like the example below: T The first subplot title should be 10.
The second subplot title should be ‘20’.
The third subplot title should be ‘30’
The fourth subplot title should be ‘40’.
The fifth subplot title should be ‘50’
The sixth subplot title should be ‘60’
The seventh ‘70’
The eighth ‘80’
The nineth ‘90’
The last ‘100’.
This is what I need .
I highly appreciate your desperation to help me.
You are welcome, madhan ravi. This is a forum for Matlab questions :-)
Insert a line to draw the title in the first loop to create the axes:
for k = 1:10
% NextPlot='add' is the same as "hold on"
AxesH(k) = subplot(10, 1, k, 'NextPlot', 'add');
title(AxesH(k), sprintf('%d', k * 10));
end
Does this help?
madhan ravi
madhan ravi il 12 Lug 2018
Modificato: madhan ravi il 12 Lug 2018
Awesome!! Thank you so much @jan.

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by