How to force slope to be zero at a particular point using function PolyFit?

8 ビュー (過去 30 日間)
Harshit Jain
Harshit Jain 2013 年 4 月 22 日
Q1 = 1xn;
T = 1xn;
Finding a curve fitting (T,Q1) such that slope is zero at Q1(i), where i = 1,2,...,n

回答 (4 件)

Teja Muppirala
Teja Muppirala 2013 年 4 月 22 日
If you have LSQLIN in the Optimization Toolbox, this can be done with a little bit of effort, as described here http://www.mathworks.com/support/solutions/en/data/1-12BBUC/
% Making some random data
Q1 = 0:0.01:1;
T = cos(2.1*pi*Q1)+0.2*randn(size(Q1));
plot(Q1,T,'k.');
% Polyfit without constraints
order = 4;
C1 = polyfit(Q1,T,order);
hold on;
plot(Q1,polyval(C1,Q1))
V = bsxfun(@power,Q1(:),order:-1:0); % Make Vandermonde Matrix
% Make Constraints on the derivatives
Aleft = [(order:-1:1).*Q1(1).^(order-1:-1:0) 0];
Aright = [(order:-1:1).*Q1(end).^(order-1:-1:0) 0];
Aeq = [Aleft; Aright];
beq = [0;0]; %Value of the derivative is set to zero
% Call LSQLIN with options to prevent warnings
opts = optimset('lsqlin');
opts.LargeScale = 'off';
C2 = lsqlin(V,T,[],[],Aeq,beq,[],[],[],opts);
plot(Q1,polyval(C2,Q1),'r')
hold off;
legend({'Data' 'Polyfit' 'Constrained Polyfit'},'location','best');
  1 件のコメント
Harshit Jain
Harshit Jain 2013 年 4 月 23 日
thanks for the answer. I have done it through spline. It works fine and is very easy.

サインインしてコメントする。


Matt J
Matt J 2013 年 4 月 22 日
If the slope is zero at all i=1...n, it means you are fitting Q1 with a constant.
p=mean(Q1);
  1 件のコメント
Harshit Jain
Harshit Jain 2013 年 4 月 22 日
Slope is not zero at all the points.
Eg - only at i = 1 and n.

サインインしてコメントする。


Image Analyst
Image Analyst 2013 年 4 月 22 日
編集済み: Image Analyst 2013 年 4 月 22 日
Your assignment says nothing about requiring the polyfit() function. There's no way to have the slope be 0 at "1" and "end" in general, unless you use Matt's solution. For specific functions, e.g. 4th order, you may luck out if your data happens to go in between the right elements, e.g. the two humps of a 4th order, but in general that won't happen. So, I'd probably use a spline. You might have to replicate the first and last value of the array to make sure that the slope is zero there.
  3 件のコメント
Matt J
Matt J 2013 年 4 月 22 日
You might have to replicate the first and last value of the array to make sure that the slope is zero there.
MATLAB's SPLINE command let's you specify the endslopes directly. For more general constrained splines, there is this FEX tool
Image Analyst
Image Analyst 2013 年 4 月 22 日
Thanks for pointing that out. I didn't know that and it's nice to know.

サインインしてコメントする。


Jan
Jan 2013 年 4 月 22 日

カテゴリ

Help Center および File ExchangeLinear Least Squares についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by