【问题标题】:Change the grid points of parametric splines in Matlab在 Matlab 中更改参数样条的网格点
【发布时间】:2017-12-01 13:54:02
【问题描述】:

我现在的代码

% Create some example points x and y
t = pi*[0:.05:1,1.1,1.2:.02:2]; a = 3/2*sqrt(2);
for i=1:size(t,2)
    x(i) = a*sqrt(2)*cos(t(i))/(sin(t(i)).^2+1);
    y(i) = a*sqrt(2)*cos(t(i))*sin(t(i))/(sin(t(i))^2+1);
end

请注意:点 (x_i|y_i) 不一定是等距的,这就是为什么 t 是这样创建的。 t 也不应该在进一步的代码中使用,因为我的真正问题是未知的,我最后只是得到一堆 x、y 和 z 值。在这个例子中,我将其缩减为 2D。

现在我正在为 x 和 y 值创建 ParametricSplines

% Spline 
n=100; [x_t, y_t, tt] = ParametricSpline(x, y, n); 
xref = ppval(x_t, tt); yref = ppval(y_t, tt); 

用函数

function [ x_t, y_t, t_t ] = ParametricSpline(x,y,n) 
  m = length(x); 
  t = zeros(m, 1); 
  for i=2:m 
    arc_length = sqrt((x(i)-x(i-1))^2 + (y(i)-y(i-1))^2); 
    t(i) = t(i-1) + arc_length; 
  end
  t=t./t(length(t)); 
  x_t = spline(t, x); 
  y_t = spline(t, y); 
  t_t = linspace(0,1,n); 
end

生成的情节

plot(x,y,'ob',...
    xref,yref,'xk',...
    xref,yref,'-r'),...
    axis equal;

如下所示:Plot Spline

问题:

如何更改代码,以便我始终在最初给定的点 (x_j|y_j)(显示为蓝色 O)上直接有一个结果点 (xref_i|yref_i)(在图中显示为黑色 X),另外 n 个点之间(x_j|y_j)(x_j+1|y_j+1)?

例如n=2 我想得到以下信息:

(xref_1|yref_1) = (x_1|y_1)
(xref_2|yref_2)
(xref_3|yref_3)
(xref_4|yref_4) = (x_2|y_2)
(xref_5|yref_5)
[...]

我想我唯一需要做的就是更改tt 的定义,但我就是不知道如何...谢谢您的帮助!

【问题讨论】:

    标签: matlab grid coordinates spline piecewise


    【解决方案1】:

    使用它作为你的函数:

    function [ x_t, y_t, tt ] = ParametricSpline(x,y,nt) 
    
    arc_length = 0; 
    n = length(x); 
    t = zeros(n, 1);
    
    mul_p = linspace(0,1,nt+2)';
    mul_p = mul_p(2:end);
    tt = t(1);
    
    for i=2:n 
        arc_length = sqrt((x(i)-x(i-1))^2 + (y(i)-y(i-1))^2); 
        t(i) = t(i-1) + arc_length;
        add_points = mul_p * arc_length + t(i-1);
        tt = [tt ; add_points];  
    end
    
    t=t./t(end);
    tt = tt./tt(end);
    x_t = spline(t, x); 
    y_t = spline(t, y); 
    
    end
    

    本质: 您必须以与距离向量 t 相同的方式构造 tt 并在其间添加额外的 nt 点。

    【讨论】:

    • 谢谢!完美运行。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多