How can I plot a hyperbola?

152 views (last 30 days)
Kasper Henriksen
Kasper Henriksen on 17 May 2018
Edited: Umar on 15 Sep 2024
Hi everyone,
I'm a beginner at Matlab, so I don't have much experience. Right now I'm trying to plot a hyperbola that I'm using for Time Difference of Arrival(TDoA), but I've been lost for hours now, and I still can't figure out how to plot it. Any suggestions how to solve this problem?
Here is my code:
hyperbola()
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
function hyperbola()
syms x y ;
f = @(x)0.4829 == sqrt((95-x)^2-(0-y)^2)-sqrt((0-x)^2-(0-y)^2);
fplot(f);
end

Answers (4)

James Jensen
James Jensen on 4 Mar 2022
You can also use the fimplicit() to graph a hyperbola or other types of conics.
  1 Comment
Yue Liang
Yue Liang on 28 Aug 2024
fimplicit() function works, aprreciate it!

Sign in to comment.


Chencho Dorji
Chencho Dorji on 14 Apr 2021
Moved: Image Analyst on 24 Aug 2024
You can try this too
b=1;a=1;
x=-5:0.01:5;
y=b*b*sqrt(1+x.*x/(b*b))
y = 1x1001
5.0990 5.0892 5.0794 5.0696 5.0598 5.0500 5.0402 5.0304 5.0206 5.0108 5.0010 4.9912 4.9814 4.9716 4.9618 4.9520 4.9422 4.9324 4.9226 4.9129 4.9031 4.8933 4.8835 4.8737 4.8639 4.8541 4.8443 4.8346 4.8248 4.8150
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
plot(x,y)
  3 Comments
Yue Liang
Yue Liang on 24 Aug 2024
Moved: Image Analyst on 24 Aug 2024
Hi Sonaa, may I ask how you obtain a hyperbola? I am also using TDOA to estimat the sound source position, but sometimes I couldn't get a solution so I want to plot the hyperbola to see if there are two intersections.
Image Analyst
Image Analyst on 24 Aug 2024
@Yue Liang how are you doing it? Did you use plot? What did you pass to it? What is TDOA?

Sign in to comment.


Walter Roberson
Walter Roberson on 25 Aug 2024
Edited: Walter Roberson on 25 Aug 2024
hyperbola()
sol = 
function hyperbola()
syms x y ;
f = 0.4829 - (sqrt((95-x).^2-(0-y).^2)-sqrt((0-x).^2-(0-y).^2));
sol = solve(f, y)
fplot(sol, [47 48]);
end
  1 Comment
John D'Errico
John D'Errico on 25 Aug 2024
Edited: John D'Errico on 25 Aug 2024
Which points out this appears to not be the equation of a hyperbola at all, but apparently an ellipse, in a subtly disguised form. I'll claim that, with no hyperbole at all.

Sign in to comment.


John D'Errico
John D'Errico on 26 Aug 2024
Just for kicks, we can do a little algebra. Start with the original form.
syms x y
eqn = 0.4829 == sqrt((95-x)^2-(0-y)^2)-sqrt((0-x)^2-(0-y)^2)
eqn = 
Those sqrts make it diffcult to visualize what we have. Also, when we have a sqrt, we need to worry about whether both branches of the sqrt will be taken, thus do you really intend +/-sqrt(stuff), or just the positive branch of the sqrt? That will make a difference, as we will see. Start by squaring both sides, and expand what you get.
eqn2 = expand(eqn.^2)
eqn2 = 
Squaring both sides of the equation does not change anything fundamental about it, except that it can introduce spurious solutions, again, because of those +/- bifurcations. Again, we will see what that means later. Next, isolate the sqrt terms that remain in eqn2, and square it AGAIN.
eqn3 = simplify((eqn2 - 2*x^2 + 190*x + 2*y^2 - 9025)^2)
eqn3 = 
You should recognize this is the equation of an ELLIPSE, NOT a hyperbola. We can draw it all now.
fimplicit(eqn,'b',[46 48 -50 50])
title 'Original equation'
grid on
fimplicit(eqn3,'r',[46 48 -50 50])
title 'Twice squared equation'
grid on
As I said before, the equation is not that of a hyperbola, but an ellipse. In the original form, due to the square roots taking on the positive branch for the square root, we see only one half of a closed ellipse. When I squared things, that effectively introduces spurious solutions that do not actually solve the original equation, where only the positive branch of the square root is assumed.
You should understand this happens because when we write something like this:
V = sqrt(sym(5))
V = 
MATLAB produces the POSITIVE square root, even though we could negate that value, and still have a perfectly valid square root.
Anyway, the original equation is as I said, not the equation of a hyperbola, but the equation of an ellipse, or most accurately, one half of an ellipse.
  3 Comments
John D'Errico
John D'Errico on 14 Sep 2024
Edited: John D'Errico on 14 Sep 2024
@Umar. I'm confused. Why are you posting a comment on my answer, where I rather carefully showed that the equation given by @Kasper Henriksen is not actually the equation of a hyperbola at all? And in your comment, you have a completely different form, which may or not be hyperbolic. It looks sort of hyperbolic, based on the plot you show, but those nested sqrts confuse things a bit, and I'm not going to spend the time to analyze this completely different form too.
Your comment has no relevance to my answer, and is only vaguely relevant to the original question posed by the OP.
Umar
Umar on 15 Sep 2024

Hi @ John D'Errico ,

Please see my response to your comments.

I appreciate your thoughtful analysis of the original equation provided by Kasper. Your insights into the nature of the equation and its transformation into an ellipse are invaluable for anyone trying to understand the intricacies of hyperbolic equations in MATLAB.

Regarding the updated code that I shared in the comments above, effectively addresses the concerns now raised about both the mathematical representation and the MATLAB implementation of a hyperbola. Here's how the refined code meets OP expectations:

Clear Definition of Parameters: The new code explicitly defines the foci of the hyperbola as f1 and f2, which are essential for TDoA applications. This clarity helps users understand where the hyperbola is centered in relation to their specific data.

Vectorization for Performance: By using linspace to create a range of x-values and preallocating y-values with zeros, the updated code enhances performance and avoids warnings about unexpected behavior on array inputs. This is crucial for beginners who may not be familiar with efficient coding practices in MATLAB.

Separate Calculation of Branches: The positive and negative branches of the hyperbola are calculated distinctly within a loop. This approach clarifies how each branch is derived from the hyperbolic equation, making it easier for users to follow along and modify as needed.

Visual Representation: The plotting section effectively visualizes both branches of the hyperbola, using distinct colors and labels to differentiate them. This helps users grasp the concept visually, which is particularly beneficial for those new to MATLAB.

Comprehensive Comments: Each section of the code includes comments explaining its purpose, which is essential for beginners like Kasper who might not fully understand MATLAB's syntax or functionality.

In response to your concerns about my previous comment's relevance, I want to clarify that it was intended to provide constructive feedback on Kasper's initial challenges while also offering an alternative solution that corrects the path towards successfully plotting a hyperbola.

Your detailed algebraic breakdown serves as an excellent educational resource; however, it’s also essential to guide users toward practical solutions when they express confusion or frustration. By providing them with corrected code, we can help them achieve their objectives while ensuring they understand any underlying mathematical principles.

Thank you again for your contributions to this discussion! Your expertise in clarifying mathematical concepts greatly enriches our Mathworks community learning experience.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!