How do I constrain a fitted curve through specific points like the origin in MATLAB?
Afficher commentaires plus anciens
I would like to use the 'polyfit' function or the Curve Fitting Toolbox to impose linear constraints on fitted curves to force them to pass through specific points like the origin.
Réponse acceptée
Plus de réponses (2)
Are Mjaavatten
le 28 Nov 2015
Modifié(e) : MathWorks Support Team
le 15 Mai 2023
9 votes
2 commentaires
Are Mjaavatten
le 10 Fév 2016
Hello Sheila,
Yes, you may specify more than one point, as demonstrated by example 1 in the help text. But note that if you specify a polynomial degree <= the number of contraints, the data points are ignored.
Example:
x = linspace(0,4,200)';y = sin(pi*x)+ 0.1*randn(200,1);
p = polyfix(x,y,3,[0,2,4],[0,0,0]);plot(x,y,'.',x,polyval(p,x));
You get a better result by using a polynomial of degree 7:
p = polyfix(x,y,7,[0,2,4],[0,0,0]);plot(x,y,'.',x,polyval(p,x));
However: Take care when you use polynomials of such high degree. They may not behave as you expect, especially outside the range of the fixed points.
Diaa
le 26 Mai 2020
Thanks for the great function. It is a life saver.
John D'Errico
le 27 Mar 2015
Well, actually, this problem can be solve quite trivially in a way that does not require lsqlin, or anything fancy. There really is no requirement for a linear constraint, since the constraint will be that the constant parameter in the polynomial model is just zero. So use backslash to estimate the model coefficients, and just leave out the constant term. Note that this works ONLY to force a point through the origin, since the point (x,y) = (0,0) in a polynomial model implies that the constant term MUST be identically zero.
So, given a model of the form
y = a1*x^3 + a*x^2 + a3*x
we can fit it simply as
a123 = [x.^3, x.^2, x]\y;
where x and y are column vectors of data.
You can even use polyval to evaluate the polynomial, simply by appending a zero to the end of that vector f coefficients, thus
P = [a123;0];
If you wanted to force the mode to pass through the point (x0,y0), where these values are not the origin, then we need to work slightly more, but not a lot. While you can do so with lsqlin, if you don't have that tool (or my own LSE, as found on the file exchange) just do this simple transformation:
xtr = x - x0;
a123 = [xtr.^3, xtr.^2, xtr]\(y - y0);
Essentially, we have now fit the model
y - y0 = a1*(x - x0)^3 + a*(x - x0)^2 + a3*(x - x0)
You can see that when x == x0, y must be predicted as y0.
Evaluation of this model is just as easy. You can still use polyval, just remember to subtract off x0 from x first.
P = [a123;y0];
ypred = polyval(P,xpred - x0);
What happens when we have a more complicated problem, where maybe you need to force TWO points on the curve? Well, then, you can use either lsqlin or my own LSE. (Note that LSE runs in R2015a with no warning messages.)
Or, if you have a trigonometric model? If the model has nonlinear parameters in it, then you will need to use a nonlinear optimization. And depending on the model, to force it through a given point, that may require a nonlinear tool that can handle an equality constraint, so possibly fmincon. But sometimes, again, depending on the model, there may be simpler solutions. It all depends on the model.
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!