NaN computed by model function, fitting cannot continue. Try using or tightening upper and lower bounds on coefficients.

Hello,
I am using the Curve Fitting App to fit this model (from the Sheindorf–Rebuhn–Sheintuch "SRS"):
Q = k1*C1*(a11*C1+a12*C2)^(n1-1)
With these provided data:
% The x
C1 = [0 14.336 18.37041 35.612 48.91934 62.43846 84.19304 80.52791 98.55237 114.74732 121.88417];
% The y
C2 = [0 7.78606 18.16868 35.82219 40.7553 52.15498 58.57749 70.75204 81.02773 85.89886 94.22023];
% The z
Q = [0 35.44 44.81 47.172 55.1 59.22706 65.1487 69.83486 72.37768 75.673 85.26529];
After many attempts, I found that the fitting will not continue unless the (n1-1) >= 0, meaning n1 bounds should be from 1 to inf... which is incorrect in this case because n1 should be between 0 and 1.
Actually, this error is not logical, I mean why n1-1 must be positive? (Am I that bad in math?)
By the way, I tried this on two different versions of MATLAB (R2020a and R2022b).
Regards.
Here are some screenshots for the Fit Options:

 Accepted Answer

It is about the first element 0 presented in each input vector: C1, C2, and Q.
Reasonable results are obtained after removing these zeros.
And the reason why it was hard to be detected is that whenever changing the values of the C1, C2, and Q and loading them using the Select Data icon, it is a must to empty the X data, Y data, and Z data boxes each time before adding the new C1 (the X data), C2(the Y data), Q(the Z data).

More Answers (1)

a11 and a12 must be such that a11*C1 + a12*C2 > 0. Otherwise, the operation ^(n1-1) gives a complex result. The simplest example is (-1)^0.5 which gives the complex unit i.
And your model is overfitted. It can at most have three instead of four independent parameters:
Q = k1*C1*(a11*C1+a12*C2)^(n1-1) = k1*a11^(n1-1)*C1*(C1+a12/a11*C2)^(n1-1)
So K = k1*a11^(n1-1), A = a12/a11 and N = n1 are free parameters.

5 Comments

Thanks.
I think that Matlab curve fitting toolbox has a serious issue, and let me explain to you why.
I tried many different ways to solve this error:
0. I tried your suggested formula,
K1*a11^(n1-1)*C1*(C1+a12/a11*C2)^(n1-1)
and it gave me the same error.
1. I tried the abs() to ignore the NaN values, writing the equation as follows,
K1*C1*abs((a11*C1+a12*C2))^(n1-1)
It worked fine, and I got the results.
2. I used the rule of (x)^(a-b)=x^a/x^b and I wrote the equation as follows,
K1*C1*(((a11*C1+a12*C2)^n1)/(a11*C1+a12*C2)^1)
It worked fine, and I got the results.
3. I tried your suggested formula in this answer "again", and surpriselly, It worked fine and I got the results.
4. I said, if your suggested formula didn't work in the first attempt then maybe the original equation can have another chance too. So I wrote back the equation as I wrote it in the question here,
K1*C1*(a11*C1+a12*C2)^(n1-1)
and guess what! It worked fine too, and I got the results.
Then I wanted to know if all results are similar, so I started trying them again one by one and taking screenshots of the results for comparison... not a single formula is working right now! Whatever I try it gives me the same error unless I make n1 start from 1 to inf or making abs(n1-1).
By the way, the results of attempts 2, 3, and 4 are similar (I hope that Matlab was working correctly) and they are also close to the results we got from the OriginLab software. So I think the complex results are not an issue for MATLAB to fitting them.
I feel like that MATLAB fitting curve App has an issue in the memory storage or something like that, mixing the current formula with the previous ones I guess.
I will try to work on it and I will post the solution if I find one
I hope recieving from you any other suggestions that may help
Regards.
My formula is
%Q = K* C1 *(C1+A*C2)^N
with K, A and N being unknown. So you can only use three instead of four independent parameters.
And depending on the initial values for the parameters, it's not surprising if you get different results.
% The x
C1 = [0 14.336 18.37041 35.612 48.91934 62.43846 84.19304 80.52791 98.55237 114.74732 121.88417];
% The y
C2 = [0 7.78606 18.16868 35.82219 40.7553 52.15498 58.57749 70.75204 81.02773 85.89886 94.22023];
% The z
Q = [0 35.44 44.81 47.172 55.1 59.22706 65.1487 69.83486 72.37768 75.673 85.26529];
fun = @(p)p(1)*C1.*(C1+p(2)*C2).^p(3);
F = @(p)fun(p)-Q;
p0 = [1 1 1];
format long
p = lsqnonlin(F,p0,[0 0 0],[Inf Inf Inf],optimset('MaxFunEvals',10000,'MaxIter',10000))
Local minimum possible. lsqnonlin stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
p = 1×3
0.786417325150726 3.277625902537181 0.000000000372097
norm(F(p0))
ans =
4.349739731093364e+04
norm(F(p))
ans =
51.441852962814274
The Q = K* C1 *(C1+A*C2)^N is working fine of course, but the main purpos of this work is to find out the a11, a12, k1, and n1 values, which is not the case using this formula.
yes, It is logical that the results will not be the same but how come it is possible that Matlab, for the same exact formula, sometimes gives some results and some other times gives an error!
The Q = K* C1 *(C1+A*C2)^N is working fine of course, but the main purpos of this work is to find out the a11, a12, k1, and n1 values, which is not the case using this formula.
I thought that I have shown that it's not possible to fit a11 and a12 independently.
Say the solver tells you that it found a solution for Q = k1*C1*(a11*C1+a12*C2)^(n1-1).
Now choose a11' = const*a11, a12' = const*a12, n1' = n1 and k1' = 1/const^(n1-1) *k1 for an aribtrary constant "const".
a11', a12', n1' and n2' - although different from a11, a12, n1 and n2 - will give the same goodness of fit because they reproduce the same values for Q. That means that your model is ill-posed (it's called "overfitted").
It would be different if a11 and a12 were somehow related, e.g. a11^2 + a12^2 = 1 or something similar.
I found the cause of the error, it is regardless of how the formula is written, It is about the first element 0 presented in each input vector: C1, C2, and Q.
Reasonable results are obtained after removing these zeros.
And the reason why it was hard to be detected is that whenever changing the values of the C1, C2, and Q and loading them using the Select Data icon, it is a must to empty the X data, Y data, and Z data boxes each time before adding the new C1 (the X data), C2(the Y data), Q(the Z data).
Thank you for your time and effort sir.

Sign in to comment.

Categories

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!