Trouble plotting a simple sin function

AI Thread Summary
The discussion centers on plotting the function y = (2sin(3w))/w, where w represents angular frequency. The initial attempt to plot this function using a range from 0 to 20 results in an empty graph due to a division by zero at w = 0, leading to a NaN value. A suggestion is made to modify the code by changing the division to element-wise operation (using ./) to avoid this issue. Additionally, it is recommended to start the range from 0.01 instead of 0 to prevent encountering NaN values. The adjustments ensure that the graph displays correctly while addressing potential problems with subsequent calculations involving y.
Lolsauce
Messages
21
Reaction score
0
So all I'm trying to do is plot y = (2sin(3w))/w, w is the angular frequency. I basically want this graph in radians. So this is what I did at first:

Code:
>> x = [0:.01:20] ;    //want bounds from 0 to 20
y = 2.*((sin(3.*x))/x);
plot(y)

But when I do this I get a graph of nothing, I assumed it was in radians by default. Next I tried this

Code:
>> x = [0:.01:20] ;    //want bounds from 0 to 20
y = 2.*((sin(3.*(2.*pi)/x))/x);
plot(y)

I knew angular frequency is 2pi/T, so I tried converting the values of x to radians, but it didn't work either. Any tips?
 
Physics news on Phys.org
Your y is only 1 value at the end change your ))/x to ))./x and your graph comes out correctly.

Also you might want to change your starting index from 0 to 0.01, since you get y(1) = NaN. The plot still comes out correctly but if you're doing anything else with y you will run into problems.
 

Similar threads

Replies
2
Views
2K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
3
Views
2K
Back
Top