polyfit(): Is it possible to catch the error/warning and only then make corrective action? i.e. iferror(reduce the degree of the fit?)

조회 수: 6 (최근 30일)
This warning doesn't occur very often within my loop -- it depends upon the data (SNR). Seems an obvious need for a catch yet user manual doesn't mention it.
Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or
try centering and scaling as described in HELP POLYFIT.
> In polyfit (line 79)
In plot87_13c1_dig (line 444)
  댓글 수: 3
dpb
dpb 2019년 5월 29일
Eric Answer moved to Comment -- dpb
Thanks for your response.
  1. [p,S,mu] = polyfit( etc ) gives me a response very far removed from my visually satisfactory one p = polyfit()
  2. degree 4 gives me a better eyeball fit than 3, particularly for high SNR cases, but error occurs for low SNR cases so I've been ignoring it.
  3. I'll try. But wouldn't it be more consistent to issue a 1 or 0 as an output argument?
Try tomorrow. Maybe I can try a different approach. Thanks again. - ELeP
dpb
dpb 2019년 5월 29일
  1. The fitted polynomial response will be identical for cases that are ok without centering; probably you're not using the centered variable for the predictor variable. See the example in the doc, you pass polyval() z=(x-u)/s instead of x.
  2. 4th order polynomial is probably risky unless is never going to be used outside the defined range (never extrapolate, that is) and even there unless you're inspecting the output visually, there possibly could be an inflection point you wouldn't really want. IOW, caution against automating this too much.
  3. I don't follow the question raised, sorry...
You might want to consider an interpolating spline instead of just a polynomial as less error-prone solution. Again, seeing some typical data and knowing more about the end objective could lead to improved suggestions.

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

답변 (1개)

Eric LePage
Eric LePage 2019년 5월 29일
Yes tried the substitute variable... but put the numerator parenthesis in the wrong spot. Now fixed. No warnings.
Works superbly up to degree 5.
Thanks 1e+06. ELeP
  댓글 수: 1
dpb
dpb 2019년 5월 29일
I've resorted to workaround on occasion --
>> polyvalz=@(p,x,mu) polyval(p,(x-mu(1))/mu(2))
polyvalz =
function_handle with value:
@(p,x,mu)polyval(p,(x-mu(1))/mu(2))
>>
Then you don't have to compute z yourself, just pass original x and the statistics and let the function deal with it. How polyfit should have been implemented from the beginning imo...
This oneliner doesn't handle the extra inputs, though...that takes a more complicated function to handle the various possibilities.

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

카테고리

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

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by