How do I use fminsearch to get a better fit to my data?
10 views (last 30 days)
Show older comments
I have a function of two variables(ErrConstFlowT.m), theta and tau, and I need to find the optimum values for these variables that will minimize the error between the model and the data. My function and the script used to call it seem to be working as I want, but the fit is very poor. What am I doing wrong?
2 Comments
John D'Errico
on 25 Mar 2015
Why should we presume that just because you WANT this relation to fit your data, that it will?
Accepted Answer
John D'Errico
on 25 Mar 2015
And then I looked at your data. Eventually I stopped laughing, rolling around on the floor. It did take a while though before the mirth died down enough to be able to type.
If I ignore the uselessness of those first 5 data points, where the relationship is apparently a constant for y==8, then you have a problem that is a function of one variable.
So next, plot the data that you have that does anything meaningful.
plot(xyz(6:end,1),xyz(6:end,3),'o')
A very nice piecewise linear function. But not something that has the shape of an exponential rise to an asymptote.
The model I get is...
pfun = @(x) (x>=0).*x;
zfun = @(x) 39.411 + 1.876*pfun(x - 7.8472) + -1.6701*pfun(x - 15.564) + -0.19575*pfun(x - 34.797);
You can kid yourself, that the data in your plot is actually representable by an exponential rise to an asymptote. Something like this...
zfun2 = @(x) max(39.411,-70.927*exp(-x/6.2027) + 57.884)
But to be serious, the data is just a few piecewise linear segments. A spline fit of linear segments makes more sense.
More Answers (0)
See Also
Categories
Find more on Interpolation 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!