另一种提取隐式函数的x,y值的方法

时间:2017-04-13 04:27:21

标签: matlab localization

我正在两个固定点之间绘制双曲线,并在其附近有一个采样点。我试图找到从双曲线到采样点的最短距离。

代码有效,除非采样点位于两个固定点之间;这是有道理的,因为双曲线变得“sm”,价值变得不存在。但是,如果我使用plot_hyperbola这样的脚本,值可以工作,但我不能排除穿过它的两个轴。

在这里,我使用ezplot绘制函数,但是当样本点处于或接近中途时,值不存在。有没有办法从样本中提取x,y值,当样本点靠近中心时,值仍然存在,或者它只是ezplot的一个问题?如果是,是否有另一种方法可以将双曲线绘制成容易获得x,y值的轴?

这是我的代码,a和b值是使用x_s和y_s计算的,但我把它留了出来。提前谢谢!

% this is the sample point 
x_s = .5; y_s = .65;
% this is determined using x_s y_s (omitted)
a = .23; b = .35;
h = @(x, y) ((x-.5).^2)/a^2 - ((y - 0).^2) / b^2 - 1;
p = ezplot(h);

% gets the x-y value of the hyperbola then I use this information
tmp = get(p,'contourMatrix'); % issue: tmp 12 is empty with .5
xdata = tmp(1,:);
ydata = tmp(2,:);

% stores the 2-norm of the value of hyperbola to the sample point
% finds and calculates the shortest 2-norm, gets the index.
distance = sqrt((xdata(1) - x_s)^2 + (ydata(1) - y_s)^2);
index = 1; % stores the index of the smallest value
for i = 2:size(tmp(1,:), 2)
    if sqrt((xdata(i) - x_s)^2 + (ydata(i) - y_s)^2) <= distance % if found smaller value 
        distance = sqrt((xdata(i) - x_s)^2 + (ydata(i) - y_s)^2);
        index = i;
    end
end

1 个答案:

答案 0 :(得分:1)

以下答案与我的回答类似here。你可以用二次方程式重新排列双曲线方程:

% this is the sample point
x_s = .5; y_s = .65;
% this is determined using x_s y_s (omitted)
a = .23; b = .35;
h = @(x, y) ((x-.5).^2)./(a^2) - ((y - 0).^2) ./ (b^2) - 1;
% quadratic equation form
h = @(x, y) (1/a^2)*x.^2 - (1/a^2)*x + (0.25/a^2 - ((y - 0).^2) ./ (b^2) - 1);
y = linspace(-3,3,1000);
A = (1/a^2);
B = -(1/a^2);
C = (0.25/a^2 - ((y - 0).^2) ./ (b^2) - 1);
% solve regular quadratic equation
dicriminant = B.^2 - 4*A.*C;
xdata_1 = (-B - sqrt(dicriminant))./(2*A);
xdata_2 = (-B + sqrt(dicriminant))./(2*A);
xdata_1(dicriminant < 0) = nan;
xdata_2(dicriminant < 0) = nan;
y(dicriminant < 0) = nan;
ydata = [y,nan,y];
xdata = [xdata_1,nan,xdata_2];
% plot
plot(xdata,ydata,'b')
distances = sqrt((xdata(:) - x_s).^2 + (ydata(:) - y_s).^2);
[distance,index] = min(distances);
hold on;
plot([x_s xdata(index)],[y_s ydata(index)],'*r-')

enter image description here