简体   繁体   English

MATLAB样条线:沿y轴求值

[英]MATLAB spline: evaluate along y axis

See this graph for an illustration: 请参见此图以获取插图: 之前 . The two red curves are interpolated by using the spline function twice. 使用样条函数两次对两条红色曲线进行插值。 Now I need to find the horizontal shift which aligns the blue points with the two red curves. 现在,我需要找到将蓝点与两条红色曲线对齐的水平移位。 The result has to look like this: 结果必须如下所示: 后 .

Is it possible to find the x coordinates which belong to some given y coordinates for a spline? 是否可以找到属于某个样条曲线的y坐标的x坐标? Then this could be solved very easy. 这样就可以很容易地解决。

Edit: simply changes the x and y axis does not help, because than spline does not give a nice curve for one of the two curves. 编辑:简单地更改x和y轴无济于事,因为比样条曲线不能为两条曲线之一给出漂亮的曲线。

Edit2: I forgot to mention that time is important. Edit2:我忘了提到时间很重要。 I'm looking for a very fast solution. 我正在寻找一个非常快速的解决方案。

Let xBlue and yBlue be the coordinates of the blue dots (n-by-1 vectors), and yRedFun be the spline approximation function, so yRedFun(x) will return the interpolated red line at x . xBlueyBlue是蓝点(n乘1向量)的坐标,并yRedFun是样条近似的功能,所以yRedFun(x)将在返回内插红色线x Eg yRedFun may be an anonymous function handle @(x) ppval(pp,x) . 例如yRedFun可以是匿名函数句柄@(x) ppval(pp,x) Maybe you will need to slightly extrapolate the red line so the yRedFun will be defined on all the xBlue . 也许您需要稍微推断一下红线,以便在所有xBlue上定义yRedFun。

We now may define a minimization function: 现在我们可以定义一个最小化函数:

cost = @(deltaX) norm( yBlue - arrayfun(yRedFun, xBlue + deltaX) )

Its minimum can be found by deltaX = fminsearch(cost, 0) or deltaX = fzero(cost, 0) . 可以通过deltaX = fminsearch(cost, 0)deltaX = fzero(cost, 0) deltaX = fminsearch(cost, 0)来找到其最小值。

Though this may be a too general approach, if fast performance is not needed, it should be OK. 尽管这可能是一种过于笼统的方法,但是如果不需要快速性能,则应该可以。 Also, as the fit between blue and red probably is not exact, the method formalizes the norm you are trying to minimize. 另外,由于蓝色和红色之间的拟合度可能不精确,因此该方法可以使您要最小化的规范形式正式化。


If performance is needed, the next algorithm may be used: 如果需要性能,则可以使用下一个算法:

function deltaX = findDeltaX(xBlue, yBlue, yRedFun, precision)
    deltaX = 0;         % total delta
    deltaDeltaX = Inf;  % delta at each iteration
    yRedFunDer = fnder(yRedFun);

    while(abs(deltaDeltaX) > precision)
        xRed = xBlue + deltaX;
        yRed = fnval(yRedFun, xRed);
        yRedDer = fnval(yRedFunDer, xRed);

        deltaDeltaX = yRedDer \ (yRed - yBlue);

        deltaX = deltaX + deltaDeltaX;
    end
end

Points with low derivative may reduce precision. 低导数点可能会降低精度。 On the first iteration you may pick N points with highest derivative and drop all the others. 在第一次迭代中,您可以选择具有最高导数的N个点,并丢弃所有其他点。 This will also improve performance. 这也将提高性能。

[~, k] = sort(abs(yRedDer), 'descend');  
k = k(1:N);  
yRedDer = yRedDer(k);
xBlue = xBlue(k);  
yBlue = yBlue(k);  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM