How to fit a curve above/below another one?
Show older comments
So, I need to tweak a few variables and fit a curve to another one - at least at some region (using MATLAB).
However, I am only interested in fits that are above the curve that I have. Also, what I use to fit is a function (there is no straightforward equation).
I tried checking a few points and giving it a penalty (either by multiplying the difference by a large number or by assigning the difference as infinity). Neither worked as it seems MATLAB optimization algorithms cannot figure out what I am trying to do and falls in terrible local minimas that are not a fit at all.
I know this sounds a little complicated. But, do you have any suggestion to how to do this (maybe a little simpler)?
I understand this is not something limited to MATLAB. But, I googled a little and was not able to find anything similar.
Thanks!
PS. The problem has constrains. I generally use lsqnonlin solver. But, I have tried other solvers, too.
8 Comments
Image Analyst
on 30 May 2019
Help you, without you even giving us any screenshots to visualize what you're describing? How many "fits above the curve" do you want/need to do?
Well, I can't help without plots/diagrams/screenshots, but if you're lucky someone might. Otherwise read this link
Siavash
on 30 May 2019
Jeff Miller
on 31 May 2019
Are you using fminsearch for fitting? If so, I would have expected this to work: "multiplying the difference by a large number".
dpb
on 31 May 2019
"Is this helpful?" Not particularly, no.
You have some understanding of what you're envisioning but we have no klews other than what you tell/show us...and that's precious little.
What is the "curve generating function" and what are its parameters of which you speak? Or, if that is just some wished-for result, at a bare minimum show what generated the existing curve (or is it just data points from somewhere?) and what an acceptable result would look like (and how we would know it is acceptable from some other of an infinite population of possibilities that wouldn't be.
Siavash
on 31 May 2019
Jeff Miller
on 31 May 2019
Siavash,
Having constraints isn't a reason to avoid fminsearch, because you can always build the constraints into the error function. Let fminsearch adjust as many free parameters (x's) as you need, and then compute your "real" constrained parameters with those x's, imposing exactly the constraints you want. E.g.
Realp1 = x(1)^2 + 10; % real parameter 1 must be at least 10
Realp2 = Realp1 - x(2)^2; % real parameter 2 must be less than real parameter 1
...
Once you have the real parameter values, use those to compute your error function, and add in some penalty if the function goes out of the bounds you want (e.g. below the other curve). Using inf as a penalty doesn't work well, though--fminsearch is much more likely to find good solutions if you use an error score that distinguishes results that are only slightly out of bounds from those that are extremely out of bounds.
Siavash
on 31 May 2019
Jeff Miller
on 2 Jun 2019
[Sorry for the slow answer--away from internet for a few days.]
I'm glad you like the fminsearh idea. If the answer helps you, please accept it.
Yes, you are right that local minima may be a problem and that trying different starting points is often the only way to address it. You might generate starting points randomly, or using a grid, or randomly within the cells of a grid, etc.
Accepted Answer
More Answers (1)
Siavash
on 3 Jun 2019
0 votes
Categories
Find more on Linear Algebra 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!