# How to fit a curve above/below another one?

5 Ansichten (letzte 30 Tage)
Siavash am 30 Mai 2019
Beantwortet: Jeff Miller am 4 Jun. 2019
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 Kommentare6 ältere Kommentare anzeigen6 ältere Kommentare ausblenden
Siavash am 31 Mai 2019
Thank you Jeff. This is a brilliant idea. I am playing with fminsearch now.
One question/concern: I am thinking when I multiply error by a large number, and since fminsearch still does not have any heuristic method, it still may fall in a local minima. Error is large. But, it still is a local minima. Isn't this correct? And do you think I can do anything other than using randomly generated starting point to overcome this?
Thanks again!
Jeff Miller am 2 Jun. 2019
[Sorry for the slow answer--away from internet for a few days.]
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.

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Jeff Miller am 4 Jun. 2019
> I am afraid you posted the answer as a comment.
Oops. Let this be the follow-up to the above as an answer.
>do you have a suggestion on how to enfore double constraints (Like, a<x<b)
Well, there is almost always a way to map fminsearch's (-inf,+inf) onto whatever legal set of values that you want. Here's a trick for the a<x<b example:
Frac = x(1)^2 / (1+x(1)^2);
Realp1 = a + (b-a)*Frac;
> enforce a number to only be an integer (choosing between cases 1,2 or 3)
This one is much tougher. Floor definitely won't work--fminsearch gets frustrated/confused if it changes a parameter and the error value doesn't change. If there are really only 3 cases you might run fminsearch three times (with that parameter fixed to a different value each time) and simply see which one produces the smallest error.
With a lot more cases, what I usually do is compute a compromise error function as a weighted average of floor(iParm) and ceil(iParm). For example, suppose fminsearch nominates a value of 4.35 for a parameter that should be an integer. Compute errorBelow as the error at floor(4.35) and errorAbove as the error at ceil(4.35). Then tell fminsearch that the overall error at 4.35 is (0.65*errorBelow+0.35*errorAbove). As long as the error function isn't the same at the two integers, fminsearch will always drift toward the integer with the lower error score. There is a bit more explanation and some code to do this at fminsearcharb (including for problems with more than one integer parameter).
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

Siavash am 3 Jun. 2019
Thank you.
I am afraid you posted the answer as a comment. So, I am unable to accept it as an answer.
Also, do you have a suggestion on how to enfore double constraints (Like, a<x<b) or to enfore a number to only be an integer (choosing between cases 1,2 or 3) using fminsearch similar to the one-way constraint ideas you suggested earlier?
I mean, I can always use floor function. But, will it work with this algorithm?
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Solver Outputs and Iterative Display finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by