How I can fix the error in Custom equation

Hello everyone,
I want to fit this function F = a*X^b/(Y^c) to find the values of a,b,c. The input data are X, Y, F.
The error is "Complex value computed by model function, fitting cannot continue. Try using or tightening upper and lower bounds on coefficients."
I tried to adjust the values but it still doesn't work.
Please give me some advices. Thank you.

2 Comments

If your input data for X and Y are all positive, this error can not appear.
If some of the X or Y values are less or equal zero, remove them from the list of inputs for the optimizer.
Okay, I got it. Some values in the data file are negative. I tried to ignore them and it works.
Thank you for your help.

Sign in to comment.

 Accepted Answer

Are you using cftool, or are you building an anonymous function using fittype() and using fit(), or are you building a function as a quoted string using fittype() and using fit() ?
Your code would help.
What is the exact code you use?
Have you considered taking the log?
log(F) = log(a) + b * log(X) - c * log(Y)
This would be a linear fit since you can rewrite it as
LF = log(F)
LX = log(X)
LY = log(Y)
La = log(a)
LF = La + b * LX - C * LY
which you can then solve using
B = log(F);
A = [ones(numel(X),1), log(X(:)), log(Y(:))]
coeffs = A\B;
a = exp(coeffs(1))
b = coeffs(2);
c = coeffs(3);

1 Comment

Yes, I see. That is a good idea. I tried to employ this method and solved the problem.
I use cfit to fit the function above.
thank you very much

Sign in to comment.

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 31 May 2021
Edited: Walter Roberson on 31 May 2021
Hi,
you should check your data first and have a scope of what are your data ranges along x, y, ....
Here is a good discussion of this question:

2 Comments

yes. Thank you for your sharing.
I think the problem is my data contain somes negative values and the discription in the code is not clear.
Based on your advise, I found the solution.
Thanks a bunch.
If your X or Y can go negative, then X^b can be complex valued if b is not an integer. X^b when b is not an integer, is defined as
exp(b * log(X))
but if X is negative then log(X) is complex, and if b is not an integer then multiplying it by b will give you something that exp() will leave as a complex number.

Sign in to comment.

Categories

Find more on Descriptive Statistics and Insights in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!