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

  • Context: MATLAB 
  • Thread starter Thread starter MathematicalPhysicist
  • Start date Start date
  • Tags Tags
    Plot
Click For Summary

Discussion Overview

The discussion revolves around plotting the function ##y=\sin(x)/\log(x)## in MATLAB, addressing issues related to complex values and undefined points in the function. Participants explore how to correctly implement the plot while avoiding errors associated with the logarithm function.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant reports receiving a warning about imaginary parts when attempting to plot the function, indicating a potential issue with the input values.
  • Another participant points out that the logarithm function is not defined for negative values, which could lead to complex outputs.
  • There is a discussion about the behavior of the function at ##x=1##, with some participants noting that it is undefined at that point.
  • Suggestions are made to use a piecewise plot to handle the undefined regions of the function.
  • One participant mentions that using the syntax `y = sin(x)./log(x);` allows for element-wise division, which may resolve the plotting issue.
  • Another participant provides an example of element-wise operations in MATLAB, emphasizing the importance of the dot operator for correct evaluations.

Areas of Agreement / Disagreement

Participants generally agree that the function is not defined for certain values (e.g., negative x and x=1), but there is no consensus on the best method to plot the function without encountering errors. Multiple approaches are suggested, and the discussion remains unresolved regarding the most effective solution.

Contextual Notes

Limitations include the need to define the domain of x carefully to avoid complex values and the requirement for element-wise operations in MATLAB to ensure proper plotting.

MathematicalPhysicist
Science Advisor
Gold Member
Messages
4,662
Reaction score
372
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
Never mind.

I want to delete this thread but I don't see how...
 
What happens to the denominator when ##x=1##?
 
@kuruman Yeah, it's not defined there, then how plot this function?
when I type it in google I get a nice plot.
 
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).
 
@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)
 
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: 938
  • Like
Likes   Reactions: 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
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K