How to Plot y=sin(x)/log(x) in MATLAB Without Imaginary Errors?

  • MATLAB
  • Thread starter MathematicalPhysicist
  • Start date
  • Tags
    Plot
In summary, the problem is that x may be negative and the log function will output complex values for that case. To get a good plot, use y=sin(x)./log(x), and remember to include the "./" symbol to make it a matrix.
  • #1
MathematicalPhysicist
Gold Member
4,699
371
Hi I want to plot the function ##y=\sin(x)/\log(x)## in MATLAB but I get the next error message:
Matlab:
Warning: Imaginary parts of complex X and/or Y arguments ignored

Here is the script that I used:
Matlab:
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!
 
Physics news on Phys.org
  • #2
Never mind.

I want to delete this thread but I don't see how...
 
  • #3
What happens to the denominator when ##x=1##?
 
  • #4
@kuruman Yeah, it's not defined there, then how plot this function?
when I type it in google I get a nice plot.
 
  • #6
MathematicalPhysicist said:
@kuruman Yeah, it's not defined there, then how plot this function?
when I type it in google I get a nice plot.
Can you do a piecewise plot? (I am not familiar with MATLAB).
 
  • #7
  • #8
@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.

Matlab:
x = 2:1/100:10;
y = sin(x)/log(x);
plot(x,y)
 
  • #9
MathematicalPhysicist said:
@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.
Use this:
Matlab:
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
 

Attachments

  • Figure 2019-02-14 08_11_58.png
    Figure 2019-02-14 08_11_58.png
    9.5 KB · Views: 823
  • Like
Likes MathematicalPhysicist and jedishrfu
  • #10
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

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

for i in range(0, 100):
    y[i] = x[i] * x[i]
 
  • #11
Ah, yes forgot about the dot thing. Thanks folks!
 
  • #12
jedishrfu said:
as an example:

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

generates the y=x^2 function
Note that the dot also works for exponential operators:
Code:
y = x.^2
 

1. What is the general shape of the plot of ##\sin(x)/\log(x)##?

The plot of ##\sin(x)/\log(x)## is a periodic curve that oscillates between positive and negative values as x increases. It has a peak at x=1 and approaches 0 as x approaches infinity.

2. What is the domain and range of the plot of ##\sin(x)/\log(x)##?

The domain of the plot is all positive real numbers, as the function is undefined for negative values of x and approaches 0 as x approaches infinity. The range is all real numbers, as the function can take on both positive and negative values.

3. Are there any asymptotes in the plot of ##\sin(x)/\log(x)##?

Yes, there is an asymptote at x=0. As x approaches 0, the value of the function approaches infinity, resulting in a vertical asymptote.

4. How does changing the value of ##\sin(x)## affect the plot of ##\sin(x)/\log(x)##?

Changing the value of ##\sin(x)## does not significantly change the overall shape of the plot. It may affect the amplitude of the oscillations, but the general pattern of the curve remains the same.

5. Can the plot of ##\sin(x)/\log(x)## intersect the x-axis?

No, the plot of ##\sin(x)/\log(x)## does not intersect the x-axis. It approaches the x-axis but never crosses it, as the function is undefined at x=0 due to the presence of the logarithm.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
987
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
940
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top