How do I use fminsearch to get a better fit to my data?

10 views (last 30 days)
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
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?
BG
BG on 25 Mar 2015
John,
After reading your comment, I attempted to convince myself that this relation will indeed fit my data. I then noticed that I forgot a set of parentheses, specifically in ErrConstFlowT.m, in the if statement the PredVals assignment should look like this:
PredVals(ii) = Kp*del*(1-exp(-(ii-theta)/tau))+Toldss;
When I corrected this error, it worked flawlessly. I feel like such a dunce. Thanks for taking the time to help me out, I really do appreciate it.

Sign in to comment.

Accepted Answer

John D'Errico
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.
  1 Comment
BG
BG on 25 Mar 2015
Edited: BG on 25 Mar 2015
John,
I'm glad I could provide some entertainment for you. Your linear spline does an excellent job of fitting the data. However, the data must fit what is called a First Order Plus Dead Time (FOPDT) model:
Y(t) = Kp*del*(1-exp(-(t-theta)/tau)+Y0
The reason it must fit this model is because theta and tau are used to implement control algorithms for the process.
Thank you for taking the time to engage with me, because it did help me figure out what I was doing wrong. Also, I recently found your fminsearchbnd function, and it has been very helpful to me, so thank you for that as well.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!