Fitting of a data set with interpolation condition
Afficher commentaires plus anciens
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 commentaires
Stijn Haenen
le 15 Mar 2020
Im not sure what you mean, but with the curve fitting toolbox you can generate a script. In this script you can change whatever you want.
Simone Guadagni
le 15 Mar 2020
dpb
le 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.
Réponse acceptée
Plus de réponses (1)
Simone Guadagni
le 21 Mai 2020
0 votes
8 commentaires
Ameer Hamza
le 21 Mai 2020
Try this example
rng(0);
x = linspace(-1, 1, 20);
y = 2 - x.^2 + 0.25*rand(size(x));
w = ones(size(x));
w(11) = 200;
fun = @(a, b, c) a.*x.^2 + b.*x + c;
fun_ = @(p) fun(p(1),p(2),p(3));
p_sol = fmincon(@(p) norm((fun_(p)-y).*w), rand(1,3), [], [], [], [], [], [], @(p) nonlcon(p, x(11)));
plot(x, fun_(p_sol), 'b', x, y, 'r+', 'LineWidth', 2)
hold on
plot(x(11), y(11), 'r+', 'LineWidth', 4, 'MarkerSize', 15);
legend({'Equal Weights', 'Unequal Weights'})
function [c, ceq] = nonlcon(p, peak_x)
ceq = -p(2)/(2*p(1)) - peak_x;
c = [];
end
It uses the fact that the peak of a quadratic equation lies at
.
Simone Guadagni
le 25 Mai 2020
Ameer Hamza
le 25 Mai 2020
If it is not quadratic, then what is the equation of the line?
John D'Errico
le 25 Mai 2020
Modifié(e) : John D'Errico
le 25 Mai 2020
Please don't add an answer just to ask another question. However, since this answer has gotten multiple comments on it, I have no choice.
I don't have your data, so I cannot give a direct example. Yes, you could use tools like fmincon to solve the problem.
Easier however, is my SLM toolbox, which does offer the ability to do pretty much anything you want.
x = rand(1,100);
y = sin(pi*x) + randn(size(x))/10;
slm = slmengine(x,y,'plot','on','xyp',[.5 0],'concavedown','on');
hold on
fplot(@(x) sin(x*pi),[0,1])

So I required the second derivative must be everywhere negative, AND that the yfirst derivative of the function at x==0.5 must be zero. As you can see, these two pieces of information provide a huge regularizing influence on the fit.
This is the entire concept of SLM (Shape Language Modeling) - that you have information about the shape of the result that you know to be true. The problem lies in how to encode that information in a usable form.
SLM is free, download it from the file exchange:
Simone Guadagni
le 26 Mai 2020
Ameer Hamza
le 26 Mai 2020
Simone, you can download John's package (link in his comment), use the code in his comment and try it yourself. If there is an issue, then you can ask here.
John D'Errico
le 26 Mai 2020
Modifié(e) : John D'Errico
le 26 Mai 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

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.
Simone Guadagni
le 29 Mai 2020
Modifié(e) : Simone Guadagni
le 17 Juil 2020
Catégories
En savoir plus sur Get Started with Curve Fitting Toolbox dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

