Problem using polyfit in a loop

조회 수: 4 (최근 30일)
shaina dhamija
shaina dhamija 2021년 6월 3일
댓글: dpb 2021년 6월 7일
Hello
I am having problem using polyfit in a for loop.
A is a null matrix. Polynomial fitting of order 10 has to be subtracted from the raw data z((I(:,j)+100/dt:end),j) and the residual has to be zero padded. t(I(:,j)+100/dt:end) is the time axis. But the loop shows matrix dimensions do not agree. Please find the code below.
for j = 1 : 1 : L
z1(:,j) = A(:,j) + (padarray(z((I(:,j)+100/dt:end),j)-polyval(polyfit(t(I(:,j)+100/dt:end),z((I(:,j)+100/dt:end),j),10),t(I(:,j+100/dt:end)),j)),N-length(z((I(:,j)+100/dt:end),j),0,'post');
end
  댓글 수: 2
dpb
dpb 2021년 6월 4일
You don't have to try to write everything in one line...factor your code first so you can read it and debug...once have done that, use the debugger to step through and see where your logic error is.
shaina dhamija
shaina dhamija 2021년 6월 7일
Thank you for your response. I could debug it by not writing everything in a line. Found the error.

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

답변 (1개)

Steven Lord
Steven Lord 2021년 6월 4일
If you're trying to remove a polynomial trend from your data, see the detrend function. As of release R2019a you can specify a degree >1 of the polynomial trend to remove.
  댓글 수: 2
shaina dhamija
shaina dhamija 2021년 6월 7일
Thank you for responding. I wil check the detrend function if it works for me.
dpb
dpb 2021년 6월 7일
High-order polynomials are notroiously ill-behaved and prone to numeric issues...depending upon what range of values are for x, x.^10 can become extremely large. Consider using some other form than such a high-order polynomial or at least use the option in polyfit|polyval to standarize the inputs that will help on the numerics...

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

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by