MATLAB Answers

Help fitting data to an implicit equation

64 views (last 30 days)
mura0087 on 13 Jan 2021
Commented: mura0087 on 19 Jan 2021
I need to fit some data to the following implicit equation:
t is a vector containing time values and y is a vector containing current values. for each series of data y vs t, I need to determine b and k
b has to be between 0 and 1, and k needs to be greater than 0.
I have both the optimization and the curve fitting toolboxes.
Any suggestions on what tools to use (lsqcurvefit? something else? would be very appreciated)


Sign in to comment.

Accepted Answer

Jeff Miller
Jeff Miller on 14 Jan 2021
Edited: Jeff Miller on 14 Jan 2021
I would suggest using fminsearch. The error function to be minimized would be something like:
function thiserr = err(x,y,t)
b = x(1);
k = x(2);
thiserr = sum( (((1-y).^(1-b)./y) - exp(-kt))^2 );
You should be able to find examples of how to use fminsearch if you need more detail on how to call it. In your case y and t are "extra parameters". Look here for information on how to handle that.


mura0087 on 15 Jan 2021
Thank you!
Here is the error function I am using:
function thiserr = err(x,y,t)
b = x(1);
k = x(2);
thiserr = sum( (((1-y).^(1-b)./y) - exp(-k*t)).^2 );
And here is how I am calling the function:
fun = @(x)err(x,yvalues,tvalues); %define the function
x0=rand(2,1);%provide a random starting point for your parameters.
options = optimset('MaxFunEvals',1e1000000000000);
I set options "MaxFunEvals" to a very high number because I keep getting the following error:
Exiting: Maximum number of function evaluations has been exceeded
- increase MaxFunEvals option.
Current function value: NaN
UPDATED- I believe the I am getting MaxFunEvals because the function does not fit my data. Fminsearch is doing its job just fine, this is a bad model. Thanks for your help
Jeff Miller
Jeff Miller on 16 Jan 2021
You are welcome. That function value NaN is a bad sign. It means thiserr is NaN for all values of b and k that fminsearch has checked. You don't have any y=0 values, do you? Dividing by 0 would cause nans for all b and k.
mura0087 on 16 Jan 2021
No y=0 values. I even scaled the y values so that everything was nonzero and less than 1, and it still did not fit. I think I need a new function!
I did get this fminsearch method to a fit a different function to different data (that I generated, so I knew the function to fit it to) as a sanity check. I had not fit using the optimization toolbox before, but I now see this is a pretty elegant method for fitting, thanks again for suggesting it!

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 16 Jan 2021
My thought would be the lazy solution. If your model is:
then log the model. That is, we know that
(1-b)*log(1-y) + k*t = log(y)
With one more step, this reduces to
-b*log(1-y) + k*t = log(y) - log(1-y)
You can compute the parameters k and b using a simple linear regression now. Thus, if y and t are column vectors, we have:
bk = [-log(1-y),t] \ (log(y) - log(1-y));
so bk is a vector of length 2, contining the estimates for b and k respectively. If you find that b or k are estimated to be something outside of the valid region, then I would first consider if this is a reasonable model, but then you could just use lsqlin to estimate them, since lsqlin does provide bound constraints.

  1 Comment

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by