%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

 Respuesta aceptada

Jan
Jan el 11 de Jul. de 2018

0 votos

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 comentarios

madhan ravi
madhan ravi el 11 de Jul. de 2018
But I want to see degree of polynomial in each subplot?
Jan
Jan el 11 de Jul. de 2018
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 el 11 de Jul. de 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.
Jan
Jan el 12 de Jul. de 2018
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 el 12 de Jul. de 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.
Jan
Jan el 12 de Jul. de 2018
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 el 12 de Jul. de 2018
Editada: madhan ravi el 12 de Jul. de 2018
Awesome!! Thank you so much @jan.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.

Preguntada:

el 11 de Jul. de 2018

Editada:

el 12 de Jul. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by