%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

 채택된 답변

Jan
Jan 2018년 7월 11일

0 개 추천

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

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

질문:

2018년 7월 11일

편집:

2018년 7월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by