如何通过特定点(例如MATLAB中的原点)约束拟合曲线,同时实现渐变 [英] How do I constrain a fitted curve through specific points like the origin in MATLAB, also implementing gradient

查看:858
本文介绍了如何通过特定点(例如MATLAB中的原点)约束拟合曲线,同时实现渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,我知道,这个论坛和其他论坛上已经存在几个类似的问题.我阅读并尝试了所有内容...虽然对我没用.

我遵循了

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.

所以我需要的实现是:

再加上一个固定点([x0,y0],[x1,y1],...)

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

将梯度设置为固定的x0,x1,...

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

我知道 polyfix 以前是这样做的,但是在我的情况下,这段代码中的拟合过程不起作用. lsqlin的结果要好得多.仍然是我要找的那种.

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.

您能帮我编辑上面的代码以添加这些功能吗?

推荐答案

您应该在优化问题中添加更多约束方程,例如:

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.

示例

以下输入:

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

生成此输出:

您可以清楚地看到约束条件对拟合曲线的影响,即比较红色和绿色曲线.

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

这篇关于如何通过特定点(例如MATLAB中的原点)约束拟合曲线,同时实现渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆