How do I constrain a fitted curve through specific points like the origin in MATLAB, also implementing gradient

百般思念 提交于 2019-12-23 04:45:49

问题


I know, I know, that there are several similar Questions already on this and other forums. I read and tried them all...Didn't work for me though.

I followed this MATLAB post to solve my problems, here the code

x0 = Xh(end,1); %end point of previous curve to add on
y0 = fh(end,1); %end point of previous curve to add on

x = A.data(co2:end,1); %a 17280 x 1 double of real data (shaky)
y = A.data(co2:end,31); %a 17280 x 1 double of real data (shaky)
% 'C' is the Vandermonde matrix for 'x'
n = 25; % Degree of polynomial to fit
V(:,n+1) = ones(length(x),1,class(x));
for j = n:-1:1
     V(:,j) = x.*V(:,j+1);
end
C = V;
% 'd' is the vector of target values, 'y'.
d = y;
%%
% There are no inequality constraints in this case, i.e., 
A = [];
b = [];
%%
% We use linear equality constraints to force the curve to hit the required point. In
% this case, 'Aeq' is the Vandermoonde matrix for 'x0'
Aeq = x0.^(n:-1:0);
% and 'beq' is the value the curve should take at that point
beq = y0;
%% 
p = lsqlin( C, d, A, b, Aeq, beq )
%%
% We can then use POLYVAL to evaluate the fitted curve
yhat = polyval( p, x );
%%
% Plot original data
plot(x,y,'.b-') 
hold on
% Plot point to go through
plot(x0,y0,'gx','linewidth',4) 
% Plot fitted data
plot(x,yhat,'g','linewidth',2) 
hold off

This code works perfect for me in terms of fitting the curve and forcing it to go through my starting point. But in terms of adding the curve to a previous smoothly, the starting point should have the same gradient than the previous curve ended on. Also it should end on a fixed point wiht fixed gradient.

So the implementations I need are:


add more then one fixed point ([x0,y0],[x1,y1],...)

set the gradient at the fixed x0,x1,...

I know polyfix did this before, but the fitting process in this code doesn't work in my case. the results of lsqlin are much better. Still this is kind of what I'm looking for.

Can you help me to edit the code above to add those features?


回答1:


You should add more constraint equation to your optimisation problem, f.e.:

Aeq(1, :) = x0.^(n:-1:0);
beq(1, :) = x0;
Aeq(2, :) = x1.^(n:-1:0);
beq(2, :) = y1;
Aeq(3, 1:end-1) = x0.^(n-1:-1:0) .* (n:-1:1);
beq(3, :) = dy0;
Aeq(4, 1:end-1) = x1.^(n-1:-1:0) .* (n:-1:1);
beq(4, :) = dy1;

To derive the equation of the first derivative constraint, it is a good idea to try it first by hand for a small polynomial order.

Example

The following input:

p_exact = [1 2 3 4 5 6];
x0 = 0;
y0 = 0;
dy0 = 0;
x1 = 1;
y1 = 10;
dy1 = 100;

x = (0:0.001:1)';
y = polyval( p_exact, x )+randn(size(x));
n = 7; % Degree of polynomial to fit

generates this output:

You clearly see the effect of the constraints on your fitted curve, i.e. compare the red and green curve.



来源:https://stackoverflow.com/questions/45095979/how-do-i-constrain-a-fitted-curve-through-specific-points-like-the-origin-in-mat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!