Fitting of a data set with interpolation condition
3 views (last 30 days)
Show older comments
Simone Guadagni
on 15 Mar 2020
Edited: Simone Guadagni
on 17 Jul 2020
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 Comments
dpb
on 15 Mar 2020
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.
Accepted Answer
Ameer Hamza
on 15 Mar 2020
Edited: Ameer Hamza
on 17 Mar 2020
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).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/277282/image.png)
2 Comments
More Answers (1)
Simone Guadagni
on 21 May 2020
8 Comments
John D'Errico
on 26 May 2020
Edited: John D'Errico
on 26 May 2020
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/301573/image.jpeg)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/301578/image.jpeg)
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.![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/301583/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/301583/image.jpeg)
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.
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox 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!