Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Graphing using MATLAB

  1. Jul 30, 2008 #1

    For those of you who know how to use Matlab, I want to graph two functions f = [tex]\frac{(2+x)}{(x-2)(x+1)}[/tex] and g = [tex]\frac{1}{\sqrt{(2x^2 - 1)(x^2-1)}}[/tex]

    On my instructions sheet it says; draw f and g on the same grid on a "suitable" domain.

    The domain of f is all the real numbers except for 2 & -1

    The domain of g = [-2,∞)\{±1}

    What is a suitable domain for both functions to put on the graph AND what's the code for specifying the domain in MATLAB?

    could it be: x≥-2\±1 ∩ x = R\{2,-1}?
    Last edited: Jul 30, 2008
  2. jcsd
  3. Jul 30, 2008 #2
    I also need to mention that when I'm trying to graph g=[tex]\frac{1}{\sqrt{(2x^2 - 1)(x^2-1)}}[/tex] I get errors. My code was;

    >> syms x
    >> ezplot 1/sqrt[(2x^2-1)*(x^2-1)]

    I'm sure this is the right code for it so what's wrong?
  4. Jul 30, 2008 #3
    In Matlab, everything is vectors of numbers. So, you might do,

    xx = -2:.1:10;
    g = 1./sqrt((2*xx.^2-1).*(x.^2-1));
    axis([-2 4 -2 6])

    This basically says,
    1) make a list of numbers called xx starting at -2, increasing by .1 each increment until 10. ie: -2, -1.9, -1.8, etc

    2) Make a list of numbers called g whose first value is the equation applied to the first element of xx, second value is the equation applied to the second element of xx, etc. The reasin there are little period dots in there (.* instead of *, ./ instead of /, .^2 instead of ^2), is that tells matlab to do the calculations element by element. Otherwise it might interpret xx*xx to mean take the dot product between the vectors or something like that. Adding the .'s makes sure any operation stays elementwise.

    3) plot the list of xx coordinates against the list of g values

    4) change the x and y mins and maxes on the graph for a better picture. (xmin = -2, xmax = 4, ymin = -2, ymax = 6)
  5. Jul 30, 2008 #4
    Btw, when you write "g = 1./sqrt((2*x.^2-1).*(x.^2-1))", what's the point of having a dot after the x and 1? like 1. & x.

    The graph now looks something like http://img523.imageshack.us/img523/5479/40023260kg1.gif" [Broken] Although the instruction is to draw it on a suitable domain... how do I do that!?!
    Last edited by a moderator: May 3, 2017
  6. Jul 30, 2008 #5
    It's not after the 1, but rather before the division sign: ./
    Same thing for the x, its not after the x, its before the exponent sign: .^

    By the way typo above the single x should be xx, or whatever you name it.

    A suitable domain means you choose where to zoom in on the graph by choosing the bounding box limits for x and y
  7. Jul 30, 2008 #6
    What's the role of the dot that comes before the division sign/exponent sign?

    So, what "suitable domain" would you think is right to choose? What are the codes for doing this?
    Last edited: Jul 31, 2008
  8. Jul 31, 2008 #7
    Normally a/b means to the matrix "division". Ie: a*(b inverse). What you want is to divide element by element, so that you put the dot in there to tell matlab so. Same thing with exponentiation.

    The way to choose the domain is specifing the minimum and maximum x and y values for the graph, which you do with the code:
    axis([xmin xmax ymin ymax])

    Matlab will automatically guess these values if you don't specify them, but for this function automatic range doesn't work well.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook