필터 지우기
필터 지우기

How do I do a linear fit on half of the data points in a set?

조회 수: 8 (최근 30일)
JJH
JJH 2018년 11월 22일
댓글: Star Strider 2018년 11월 22일
I have a code that should plot a set of data points and then a linear fit to the data points. Only the second half of the data follows a linear trend so I have set the fit over the second half of the data only. I have the code
fig3 = figure;
for ij = 1:N
subplot(1,2,1);
plot(Pdissarray(ij,:)',peakposarray(ij,2:end)','.','color', CM(ij,:))
xlabel('Dissipated Power (mW)');
ylabel('Peak Wavelength (nm)');
ylim([842 855]);
hold on
fitA = polyfit(Pdissarray(ij,15:end),peakposarray(ij,15:end),1);
lambdafit = polyval(fitA,Pdiss(ij,:));
subplot(1,2,1);
plot(Pdissarray(ij,:),lambdafit,'--','color',CM(ij,:));
hold on
end
where pdissarray and peakposarray are data sets such that each row is the thing I want to plot for each iteration of the loop and there are N rows (i.e. the arrays are each Nx50 and each N represents a single data set). The plot of these data sets is great, I get a set of N lines where each is a plot of the first row of each array. The problem is the fit. I'm trying to do a polynomial fit of degree one on each one of these lines. I do polyfit followed by polyval and then plot this output but the plot of this fit is empty. What am I doing wrong? Is it an indexing problem?
  댓글 수: 2
madhan ravi
madhan ravi 2018년 11월 22일
first upload your data(values)
JJH
JJH 2018년 11월 22일
I'm attaching the files. Each row represents one data set such that the first row of Pdiss should be plotted with the first row of peakpos. I realised there is a typo in the code above:
fitA = polyfit(Pdissarray(ij,15:end),peakposarray(ij,15:end),1);
should be
fitA = polyfit(Pdissarray(ij,15:end),peakposarray(ij,16:end),1);
but this is not the problem with the data fitting.

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

채택된 답변

Star Strider
Star Strider 2018년 11월 22일
There are several problems with the code you posted.
This works for me:
fig3 = figure;
for ij = 1:N
subplot(1,N,ij);
plot(Pdissarray(ij,:)',peakposarray(ij,2:end)','.','color', CM(ij,:))
xlabel('Dissipated Power (mW)');
ylabel('Peak Wavelength (nm)');
ylim([842 855]);
hold on
fitA = polyfit(Pdissarray(ij,15:end),peakposarray(ij,16:end),1);
lambdafit = polyval(fitA,Pdissarray(ij,:));
plot(Pdissarray(ij,:),lambdafit,'--','color',CM(ij,:));
hold off
end
  댓글 수: 2
JJH
JJH 2018년 11월 22일
Hi, thanks for your help, this is close to what I want, but I'd like all the plots to be on the same figure. Is that in how you defined subplot?
Star Strider
Star Strider 2018년 11월 22일
My pleasure.
You included a subplot call, so I assumed you wanted them all as subplots. To plot them all in one axes object, this works:
fig3 = figure;
hold all
for ij = 1:N
plot(Pdissarray(ij,:)',peakposarray(ij,2:end)','.','color', CM(ij,:))
fitA = polyfit(Pdissarray(ij,15:end),peakposarray(ij,16:end),1);
lambdafit = polyval(fitA,Pdissarray(ij,:));
plot(Pdissarray(ij,:),lambdafit,'--','color',CM(ij,:));
end
hold off
xlabel('Dissipated Power (mW)');
ylabel('Peak Wavelength (nm)');
ylim([845 853]);
Keeping only the operations affected by the for looop inside it makes it more efficient. I also tweaked the ylim argument to make the plot a bit more readable.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by