Fitting of a data set with interpolation condition
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi everyone, I'm trying to fit a data set on the plane whose trend is concave.
Doing that with "Curve Fitting Toolbox" is quite easy, but I have to add another condition: I need that a assigned point A belong to the fitting curve.
Someone can help me?
Thank you very much
댓글 수: 3
dpb
2020년 3월 15일
Not an implemented constraint. You could try a weighted fit and give the specific point a very high weight and see what happens...otherwise, would have to solve a specific model.
채택된 답변
Ameer Hamza
2020년 3월 15일
편집: Ameer Hamza
2020년 3월 17일
As pointed out by dpb, you can specify the weights of each data point to the fit() function. Following code shows an example
rng(0);
x = linspace(-1, 1, 20);
y = 2 - x.^2 + 0.25*rand(size(x));
w = ones(size(x));
w(11) = 200;
fitCurve1 = fit(x', y', 'poly2');
y_fit1 = fitCurve1(x');
fitCurve2 = fit(x', y', 'poly2', 'Weights', w);
y_fit2 = fitCurve2(x');
plot(x, y_fit1, 'b', x, y_fit2, 'k', x, y, 'r+', 'LineWidth', 2)
hold on
plot(x(11), y(11), 'r+', 'LineWidth', 4, 'MarkerSize', 15);
legend({'Equal Weights', 'Unequal Weights'})
This code gives high weight to the 11th data point (indicated with a big marker below) in the input. The following figure shows both responses (with and without high weight).
추가 답변 (1개)
Simone Guadagni
2020년 5월 21일
댓글 수: 8
John D'Errico
2020년 5월 26일
편집: John D'Errico
2020년 5월 26일
You need to consider if forcing the curve to have a zero slope at exactly x==1 makes sense in terms of that data. For example, if we zoom into your data
I see you have one point at x==1, perhaps it was even artificially inserted. That is, your data has the exact point (x,y) = (1,0), whereas most of the other data has many decimals in it.
However, if we look at the shape of the curve near there, it seem there is a natural maximum around x==1.05, a location that lies between that point and the next. The shape is fairly complex, but if you force the curve to have a zero slope exactly at x==1, this forces what is essentially a second derivative discontinuity into the curve at x==0.
In this plot, the vertical line in black is where the maximum wants to live, in order to be consistent with the data we see. In fact, this is relative nice, smooth data. In fact, the model fits very well, even though there is some interesting behavior around x=1.5 where the curve dips down, and there is one strange point at x=0.4, that seems to be almost another derivative discontinuity. (I wonder how the data was generated.) In the model as created, the data naturally wants the maximum to be around x=1.05. I get 1.0517 from the curve, where that peak lies.
But, now consider the fit when I artifically force the maximum to live exactly at x=1.
Do you see what happens? This is inconsistent with the data itself. In turn, that introduces oscillations into the curve away from that point. I had to force the fit to do something inconsistent with the data.
In order to get the result you want I might as well break the problem into two separate problems, where I model the left and right halves of the curve separately. If I do that, splitting the curve into two halves, but forcing each half to pass through the point at (x,y) = (1,0), then things do fit reasonably well. This implicitly creates a point at x=1 where the curve is continuous but not differentiable.
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!