How to use lsqlin for linear contraint through points and constrained derivative at those points?

2 views (last 30 days)
How to combine the two to both force the fit through a point and force the derivative at that point to be some value?

Accepted Answer

John D'Errico
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
Oleg Zhukov
Oleg Zhukov on 11 Sep 2015
Edited: Oleg Zhukov on 12 Sep 2015
John! I am a big fan, I've read a lot of your answers :-). Sadly, it has to be a polynomial, with two point location and derivative constraint, basically similar to Cubic Bezier curves. The flexibility is another issue.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!