How to limit an attached curve through certain points, such as origin in MATLAB, also implementing a gradient

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 is 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 is great for me in terms of fitting the curve and forcing it to go through my starting point. But in terms of adding a curve to the previous one smoothly, the starting point should have the same gradient as the previous curve. It should also end at a fixed point with a fixed gradient.

So, I need the following implementations:


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

x0, x1,...

polyfix , . lsqlin . , .

, ?

0
1

, 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;

, .

:

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

:

enter image description here

, .. .

+1

Source: https://habr.com/ru/post/1685534/


All Articles