Scotch Jones
Scotch Jones

Reputation: 25

Plotting Hyperbola in Matlab

I'm trying to plot a hyperbola in Matlab to simulate a hyperbolic trajectory around the Earth.

Since the hyperbolic equation consists of a +/- part (from the square root), I cannot seem to figure out how to get Matlab to plot this.

This is my code so far:

%Plotting Hyperbolic Orbit

 e = 1.05;          %Eccentricity 
 a = -147562.72;    %km
 b = abs(a)*sqrt(e.^2 -1);      %Equation for Semi-Minor Axis, b

 x1 = linspace(0.00001, a);         %From 0 to a, Upper
 x2 = linspace(-a, -0.00001);       %From -a to 0, Lower

 y1 =  sqrt((b.^2)*((x1.^2)/(a.^2)-1));  %Upper Hyperbola Part
 y2 = -sqrt((b.^2)*((x2.^2)/(a.^2)-1));  %Lower Hyperbola Part


 figure
 plot(x1, y1, x2, y2, 'b')
 grid on

I originally tried with one x coordinate from -a to a, but since 0 is included with this it gave me an imaginary number error and ultimately couldn't plot it.

Any help is appreciated, thanks.

Upvotes: 0

Views: 5904

Answers (1)

TroyHaskin
TroyHaskin

Reputation: 8401

For hyperbolas, x values smaller than a (in absolute value) are complex.

Consider the expression: x1.^2/a^2-1. If x1 is smaller than a, their ratio will be less than one, the squared will make it more so, and the whole expression will therefore be negative. And then the y values are defined by the square root of a negative number. So, the plotting bounds are the opposite of what they should be (though, off the cuff, I'd imagine they are perfect for an ellipse).

So this code should be closer to what you are expecting:

 e = 1.05;          %Eccentricity 
 a = -147562.72;    %km
 b = abs(a)*sqrt(e.^2 -1);      %Equation for Semi-Minor Axis, b

 x1 = linspace(   a, 2*a, 1E3);         %From 0 to a, Upper

 ytop =  b*sqrt(x1.^2/a^2-1);  %Upper Hyperbola Part
 ybot = -b*sqrt(x1.^2/a^2-1);  %Lower Hyperbola Part

 figure
 plot(x1, ytop, x1, ybot, -x1, ytop, -x1, ybot);
 grid on

Hyperbola Plot from included script

Upvotes: 2

Related Questions