user1691278
user1691278

Reputation: 1885

Graphing a hyperbola in Matlab

I'm trying to graph a solution obtained through the quadratic formula in Matlab. Since it's obtained by the quadratic formula, there are two parts: plus and minus. The graph should be a hyperbola. How can I place the upper part and the bottom part on the same graph?

Upvotes: 0

Views: 6148

Answers (1)

Simon
Simon

Reputation: 32883

There are different ways. Let's say you want to plot the solution of y^2 = x, that is y = ±sqrt(x):

  1. You can plot the two parts with the same color using a plot once…

    x = 0:0.1:10;
    plot(x, sqrt(x), 'k', x, -sqrt(x), 'k')
    
  2. …or twice:

     x = 0:0.1:10;
     plot(x, sqrt(x), 'k')
     hold on
     plot(x, -sqrt(x), 'k')
     hold off
    
  3. Or you can plot everything in one go like you might draw it with a pen:

     x = [10:-0.1:0 0.1:0.1:10];
     y = [-sqrt(10:-0.1:0) sqrt(0.1:0.1:10)];
     plot(x, y)
    

Upvotes: 1

Related Questions