Constraining a fitted curve
1 次查看(过去 30 天)
显示 更早的评论
Hi all,
I'm created cubic fitted curves in matlab based on a set of data points. There is however one requirement that I have to meet: the fitted curve can not decrease in y value as x increases. What I would like to happen is that whenever the fitted curve is supposed to decrease in y value, the curve is somehow constrained so that it flattens out (slope of 0). Is there any possible way to do this?
0 个评论
采纳的回答
Richard Willey
2012-1-9
I'd strongly recommend that you look at a file exchange submission by John D'Errico titled "Shape Modeling Language"
0 个评论
更多回答(2 个)
Andrew Newell
2012-1-9
Tricky. Suppose that your fit is
y = p(1)*x^3 + p(2)*x^2 + p(3)*x+p(4);
You'll need to ensure that the first derivative
yp = @(x) 3*p(1)*x^2 + 2*p(2)*x^2 + p(3);
is positive across the entire interval [x1,x2]. Certainly
yp(x1) >= 0 && yp(x2) >= 0
must be satisfied. If the slope doesn't have a minimum in the interval, you're done. Taking the derivative of yp shows that an extremum occurs at
x0 = -p(2)/3/p(1)
(for this analysis it doesn't really matter if it is a minimum or maximum). If
x(1) >= x0 || x0 >= x1
the extremum is outside the interval. Otherwise, you need to calculate the minimum and ensure it is nonnegative:
3*p(1)*p(3)-p(2)^2 >= 0
Putting the constraints together,
-yp(x1) <= 0
-yp(x2) <= 0
(x1 <= x0 && x0 <= x1)*(p(2)^2-3*p(1)*p(3)) <= 0
You could use this to create a constraint function c(p) that you can use in fmincon (you would be minimizing the sum of squares).
On the other hand, if your data are monotonically increasing, you could discard the cubic fit and interpolate using pchip.
0 个评论
Dr. Seis
2012-1-9
Here is an example using "pchip":
a = sort(rand(1,15),'ascend');
b = sort(rand(1,15),'ascend');
x = a(1):0.001:a(end);
y = pchip(a,b,x);
figure; plot(a,b,'b',x,y,'r');
z = sum(diff(y)<0)
It looks like as long as you are interpolating between points (no extrapolation) the result from "pchip" will not give you any decreasing "y" values for increasing "x" values when your data values (i.e., "a" and "b") are also increasing. As a test, the result of "z" should be 0 and it was for all my test runs (ran 10000 tests).
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interpolation 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!