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

31 views (last 30 days)
Anshuman Pal on 7 Nov 2019
Commented: Anshuman Pal on 9 Nov 2019
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.

James Kennedy on 7 Nov 2019
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 CommentsShow 1 older commentHide 1 older comment
James Kennedy on 8 Nov 2019
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')
Anshuman Pal on 9 Nov 2019
Great! Thank you.

### Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!