Error while fitting: X, Y and WEIGHTS cannot have Inf values.

29 次查看(过去 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.
graphs.png

采纳的回答

James Kennedy
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
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 CenterFile Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by