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

MATLAB plot of ##\sin(x)/\log(x)##.

  1. Feb 13, 2019 at 7:54 PM #1

    MathematicalPhysicist

    User Avatar
    Gold Member

    Hi I want to plot the function ##y=\sin(x)/\log(x)## in MATLAB but I get the next error message:
    Code (Matlab M):

    Warning: Imaginary parts of complex X and/or Y arguments ignored
     
    Here is the script that I used:
    Code (Matlab M):

    x = -2*pi:pi/100:2*pi;
    y = sin(x)/log(x);
    plot(x,y)
     
    How to fix this that I'll get the plot on the real line?

    Thanks!
     
  2. jcsd
  3. Feb 13, 2019 at 7:59 PM #2

    MathematicalPhysicist

    User Avatar
    Gold Member

    Never mind.

    I want to delete this thread but I don't see how...
     
  4. Feb 13, 2019 at 8:00 PM #3

    kuruman

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    What happens to the denominator when ##x=1##?
     
  5. Feb 13, 2019 at 8:02 PM #4

    MathematicalPhysicist

    User Avatar
    Gold Member

    @kuruman Yeah, it's not defined there, then how plot this function?
    when I type it in google I get a nice plot.
     
  6. Feb 13, 2019 at 8:07 PM #5

    jedishrfu

    Staff: Mentor

  7. Feb 13, 2019 at 8:09 PM #6

    kuruman

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Can you do a piecewise plot? (I am not familiar with MATLAB).
     
  8. Feb 13, 2019 at 8:11 PM #7

    MathematicalPhysicist

    User Avatar
    Gold Member

  9. Feb 13, 2019 at 8:28 PM #8

    MathematicalPhysicist

    User Avatar
    Gold Member

    @jedishrfu when I try to plot for the domain [2,10] I still don't get a graph, I do get the plot window but without the graph line, it doesn't show up.

    Code (Matlab M):

    x = 2:1/100:10;
    y = sin(x)/log(x);
    plot(x,y)
     
     
  10. Feb 13, 2019 at 8:42 PM #9

    Wrichik Basu

    User Avatar
    Gold Member
    2018 Award

    Use this:
    Code (Matlab M):

    y = sin(x)./log(x);
     
    The thing I added is "./" so that ##y## becomes a matrix. Now you should get a good plot.
    Figure 2019-02-14 08_11_58.png
     
  11. Feb 13, 2019 at 8:56 PM #10

    jedishrfu

    Staff: Mentor

    the dot before the operator says to do an element by element evaluation.

    as an example:

    x=0:0.1:10
    y=x.*x

    generates the y=x^2 function and this plots it

    plot(x,y)

    In other programming languages, this simple convenience would be implemented as a for loop

    Code (Python):

    for i in range(0, 100):
        x[i] = i / 10.0

    for i in range(0, 100):
        y[i] = x[i] * x[i]

     
     
  12. Feb 13, 2019 at 9:51 PM #11

    MathematicalPhysicist

    User Avatar
    Gold Member

    Ah, yes forgot about the dot thing. Thanks folks!
     
  13. Feb 14, 2019 at 12:27 AM #12

    Orodruin

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper
    Gold Member
    2018 Award

    Note that the dot also works for exponential operators:
    Code (Text):

    y = x.^2
     
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?