1

I am plotting a hyperbola between two fixed points and having a sample point somewhere near it. I am trying to find the shortest distance from the hyperbola to the sample point.

The code works, except when the sample point is halfway between the two fixed points; that makes sense, since the hyperbola becomes 'smooshed' and values become non existent. However, if I use a script such as plot_hyperbola, the values work but I can't exclude the two axes that cross it.

Here, I use ezplot to plot the function, however when the sample point is at or close to halfway, the values are non-existent. Is there a way to extract the x,y values from the explot in which values still exist when the sample point is near the center or is it just an issue with ezplot? If it is, is there another way to plot the hyperbola with the axes in which it is easy to get the x,y values?

Here's my code, the a and b values are calculated using the x_s and y_s but I left that out. Thanks in advance!

% 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
Jack L.
  • 405
  • 2
  • 13
  • 31

1 Answers1

1

the following answer is similar to my answer here. you can just rearrange your hyperbola equation in a quadratic equation form:

% 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

Community
  • 1
  • 1
user2999345
  • 4,195
  • 1
  • 13
  • 20
  • Thank you so much! Just one question, if it the hyperbola is shifted up m units up, we have (y - m)^2/b^2. When expanding into general form, does this affect the C term? And lastly, when using this for vertical hyperbola, it doesn't change much, except for the x and y are switched? – Jack L. Apr 13 '17 at 06:35
  • Also, I forgot to specify, the a and b value specified are not relative to the shape of this hyperbola. Since the hyperbola is supposed to touch or run hear the point, if the sample point is in the middle, the hyperbola is supposed to be really flat. The problem I had was the points were undefined, is this the same here or will it guarantee to generate a pair of xy points? – Jack L. Apr 13 '17 at 06:43
  • 1. yes, as you can see it's a part of the `C` term. 2. yes. 3. if the hyperbola exist it is guarantee to generate a pair of xy points. – user2999345 Apr 13 '17 at 07:17