How to use lsqlin for linear contraint through points and constrained derivative at those points?
2 views (last 30 days)
Show older comments
Oleg Zhukov
on 11 Sep 2015
Commented: John D'Errico
on 12 Sep 2015
Linear constraint through a point is in the example below: http://www.mathworks.com/matlabcentral/answers/94272-how-do-i-constrain-a-fitted-curve-through-specific-points-like-the-origin-in-matlab and derivative constraint is: http://www.mathworks.com/matlabcentral/answers/73085-how-to-force-slope-to-be-zero-at-a-particular-point-using-function-polyfit
How to combine the two to both force the fit through a point and force the derivative at that point to be some value?
0 Comments
Accepted Answer
John D'Errico
on 11 Sep 2015
Edited: John D'Errico
on 11 Sep 2015
A general curve fit, IF the curve is nonlinear, will not always be trivial to enforce a constraint. You would need to use a tool like fmincon.
But if you are asking how to fit a polynomial, as those links talk about, then is there some (ANY?) rational reason why you think you need to use a polynomial for this?
The most common reason is you simply don't know of any other fitting tool. The answer to that is to use my SLM toolbox , which lets you do EXACTLY as you desire, plus, it has many more abilities.
Yes, it is trivial to use lsqlin for this purpose. But a sufficiently high order polynomial to give you any real flexibility will be too high an order to cause problems in the fit. High order polynomials are NEVER a good choice.
Do I really need to show you how to solve this using a polynomial fit? Sigh. It is just a bad idea in general. Use SLM instead. Really. You WILL be happier with the result.
Assume the polynomial has order (highest degree) n, and that at some given point a, f(a)=v, and f'(a)=d.
I'll make up some random data (crap data, but who cares?) This is just an example. I'll do it as a quadratic polynomial.
x = rand(10,1);
y = rand(10,1);
n = 2;
a = 0.5;
v = 0.6;
d = 0.1;
p = n:-1:0;
M = bsxfun(@power,x(:),p);
Aeq = [a.^p;p.*a.^max(0,p-1)];
Beq = [v;d];
coef = lsqlin(M,y(:),[],[],Aeq,Beq);
coef
coef =
1.0934
-0.99337
0.82334
Do ya trust me?
polyval(coef,a)
ans =
0.6
polyval(polyder(coef),a)
ans =
0.1
Got lucky I guess. Even so? Use SLM. You WILL be happier with the result.
2 Comments
John D'Errico
on 12 Sep 2015
Oh well, if it MUST be, then it will be so. I did my best to convince you. :)
More Answers (0)
See Also
Categories
Find more on Linear Least Squares 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!