Error while fitting: X, Y and WEIGHTS cannot have Inf values.
조회 수: 25 (최근 30일)
이전 댓글 표시
I get a series of curves obtained by numerically integrating an ODE, for different values of some parameter. They look very linear on a log-log plot, so I try to fit them with a straight line:
y = log(originalY);
[curve2,gof2] = fit(x', y','poly1', 'Exclude', x < 0.73 & x > 0.75)
However, for the last curve (which has very small numbers), the fitting shows me the error: Error while fitting: X, Y and WEIGHTS cannot have Inf values. Indeed, y has -Inf values:
y
...
Columns 121 through 140
-Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf
Columns 141 through 152
-Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf
What is going on, and how can I solve this? I even tried restricting the fitting zone (see code above) to try and avoid the -Inf values. Could this be due to finite machine precision while calculating the log? Below, in the curves (with fits where possible), you can also see that the numerical integration does not extend over the full domain (upto x=1). Maybe that is also due to machine precision.
댓글 수: 0
채택된 답변
James Kennedy
2019년 11월 7일
Can you verify that you are excluding all 'Inf' y-data with your restriction on x values?
The exclude option works when using a simple case:
% Test data
x = 1:10;
y = [1 2 3 4 5 Inf Inf Inf Inf Inf]
When including all the y data, MATLAB throws the error:
'X, Y and WEIGHTS cannot have Inf values.'
[curve2,gof2] = fit(x',y','poly1')
However, when excluding all values of x greater than 5 (which covers all the Inf values for y), the fit function completes.
[curve2,gof2] = fit(x',y','poly1','Exclude',x > 5)
댓글 수: 3
James Kennedy
2019년 11월 8일
In that case, you could find the indices of the non-infinite values before plugging into you the fitting function.
For example,
% Test data
x = 1:10;
y = [1 2 3 4 5 Inf Inf Inf Inf Inf]
indx = find(~isinf(y))
[curve2,gof2] = fit(x(indx)',y(indx)','poly1')
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Interpolation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!