How do I fit a curve to data while constraining both endpoints of the curve?

15 views (last 30 days)
I am currently optimizing models to fit phase equilibria diagrams. I understand how to use MATLAB to optimize the curve fit, however it is very important that the endpoints be fixed, as each endpoint represents a pure fluid, and the models must get pure fluid properties correct (and do when the parameter I am adjusting is set to zero). The rest of the points are a mixture of the two fluids and just need to be fit as close as possible, but it is these points that need to have the parameter I am adjusting fit to a non-zero number.
  3 Comments
Braden Kelly
Braden Kelly on 29 Apr 2015
Hi Star Strider. The model is a thermodynamic equation of state model. For example it could be the Peng-Robinson. It is too complicated to fit in here. For that reason it is also not useful for me to add the data, since I am not interested in "just" a curve fit, it must be a fit based on an equation of state for the purpose of optimizing the binary interaction parameter.
The parameter is fit however through the common practice of minimizing an error objective function. I know how to use fminsearch for this purpose, however, fminsearch optimizes all of the points and I can't have it change the endpoints.
I think I will need to optimize the second to second last points, and write an if statement in my code saying that at either endpoint use parameter = 0 (which leads to the correct endpoint values), rather than parameter = X (where x is the optimized fit value and leads to the best fit for points between the endpoints).
I am curious as to your thoughts on this?
Star Strider
Star Strider on 29 Apr 2015
The Peng-Robinson Equation of State postdated my college p-chem, so it’s new to me. I’m not exactly certain how you want to fit that model, but if you have the Optimization Toolbox, there are several constrained optimisation functions that would probably work. (The Global Optimization Toolbox adds genetic algorithms, simulated annealing and such, most if not all of which allow constraints.)
You could also include in your objective function, or preferably in an anonymous function that calls it, code that would constrain certain parameters and fit only the ones you want optimised. The anonymous function approach has the advantage of avoiding the need to change your objective function, and would allow you to set your chosen parameters to zero and fit the rest. This is a relatively routine procedure, and easy to code. I am not certain how it would affect any statistics you want to calculate from the fit (such as confidence intervals).
If you don’t have the Optimization Toolbox, there are some File Exchange contributions that appear to offer similar functionality. I will let you search to see if any are appropriate to your problem, since I have no experience with them.
I’m not listing this as an Answer, because it isn’t one.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 29 Apr 2015
Edited: John D'Errico on 29 Apr 2015
Absolutely no problem. Download my SLM toolbox.
x = linspace(0,1,100);
y = sin(x*pi*3/2) + randn(size(x))/10;
slm = slmengine(x,y,'leftvalue',0,'rightvalue',-1,'knots',6,'plot','on','rightslope',0);
The fit looks quite nice. See that I was able to set either endpoint and the end point slope with no problems. There are many other things you can specify.
How about the end point values?
slmeval(0,slm,0)
ans =
0
slmeval(1,slm,0)
ans =
-1
slmeval(1,slm,1)
ans =
2.8386e-17
The last is as close as you will sometimes come to 0. I'll just call it close enough for government work.
Note that while sometimes (depending on your choice of constraints) SLM will need the optimization toolbox, the above call will work without that toolbox.
  2 Comments
Braden Kelly
Braden Kelly on 29 Apr 2015
Hello John,
You have gone above and beyond <-- also the name of a good trance electronic group if you are into that.
To better clarify my intent I must refer to my comment above to Star Strider.
What you have shown looks promising, I am curious though as to how I would optimize a parameter using your method. What your function does looks like it is right up my alley, except I need to be able to make a fit by optimizing a parameter. The models I use are too complicated to put here, however they essentially provide a Y value when given an X value, with the addition of having a parameter in them that can be changed to get a better fit.
Can I use your method to optimize a parameter?
Thanks,
Braden
John D'Errico
John D'Errico on 29 Apr 2015
No. You cannot use a spline to estimate a parameter if you MUST use the model you have posed.
IF your goal is simply prediction, then a spline model is often a far better choice. It will give you a Y value for any X in the interval of fit.
IF your goal is to fit the model you have given, because you also need the parameters from that model for purposes other than prediction, then you need to use a nonlinear optimization tool, like fmincon. That tool is necessary because of the equality constraints at the end points.
The fact is however, that in the model you suggested, there are not that many parameters to estimate. If you will force it to go through two points, then you will lose two degrees of freedom. The model simply does not have sufficient flexibility to pass exactly through two points, and ALSO to fit the curve well.

Sign in to comment.

Categories

Find more on Thermodynamics and Heat Transfer 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!