MATLAB Matlab - graph of y=(sin(x)^2)/x^2

  • Thread starter Thread starter sara_87
  • Start date Start date
  • Tags Tags
    Graph Matlab
Click For Summary
The discussion centers around plotting the function y = (sin(x)^2)/x^2 in MATLAB over the interval [0, 4π]. The user initially struggles with MATLAB syntax, particularly with defining the variable x and correctly writing the function. They attempt several incorrect formulations, leading to straight-line outputs instead of the expected graph shape. Key issues identified include the incorrect definition of the x variable and confusion between uppercase and lowercase letters in variable names. Suggestions include correcting the syntax to x = [0:pi/4:4*pi] and ensuring the function is written as y = (sin(x).^2)./x.^2. Additionally, advice is given to plot more points for better accuracy. The user emphasizes the need to express the function in a specific format, indicating a desire for clarity in the mathematical representation.
sara_87
Messages
748
Reaction score
0

Homework Statement


i have to draw the graph of y=(sin(x)^2)/x^2 on MATLAB in interval [0,4pi]


Homework Equations




The Attempt at a Solution


i defined x and y.
>>x:[0:pi/4:4pi]
but i can't write the equation in MATLAB fprm...i don't know where to put the dots and brackets.
i tried:
>>sin(x).^2./x.^2
and i just got a straight line, then i tried:
>>sin(x.*x)./(x.*X)
and i also got a striaght line but when i put the function onto the graphics calculator it gives like a mountain shape and i think that's write.
so how do i type the function onto MATLAB to give the right graph?
can someone please help me? :rolleyes:
 
Physics news on Phys.org
sara_87 said:

Homework Statement


i have to draw the graph of y=(sin(x)^2)/x^2 on MATLAB in interval [0,4pi]


Homework Equations




The Attempt at a Solution


i defined x and y.
>>x:[0:pi/4:4pi]
but i can't write the equation in MATLAB fprm...i don't know where to put the dots and brackets.
i tried:
>>sin(x).^2./x.^2
and i just got a straight line, then i tried:
>>sin(x.*x)./(x.*X)
and i also got a striaght line but when i put the function onto the graphics calculator it gives like a mountain shape and i think that's write.
so how do i type the function onto MATLAB to give the right graph?
can someone please help me? :rolleyes:

I don't have MATLAB near me right now, but I did notice two issues right away:

First off, this isn't going to work in MATLAB: "x:[0:pi/4:4pi]"

You should write it as: x = [0:pi/4:4*pi]

Secondly, this: sin(x.*x)./(x.*X) won't work because X is not the same as x in MATLAB. Try fixing those two issues and see if the plot works.
 
actually i don't have MATLAB infront of me either.. :(
but it's easy to fix the first broblem but for the second, i can change the X into an x but it still gives a straight line.
my problem is that i don't know how to write it in MATLAB because i don't know where to put the * or ^ or .
can u help me please because i have to typeit up tomorrow.
thank u v much :)
 
You did the periods just fine, and had the right idea. Correct the syntax and it will work fine. Try plotting more points.

x=[1:100]/100 * 4 * pi;
y = (sin(x) ./ x) .^2 ;
% Writing y this way is faster since you only square once.
plot(x,y)
 
i see but i can't do that because i don't want to type: (sinx)^2/x^2
i want: sin(x)^2/x^2
i.e. the squared in the numerator is just for x.

help please... ;)
 
sara_87 said:
i see but i can't do that because i don't want to type: (sinx)^2/x^2
i want: sin(x)^2/x^2
You mean sin(x^2)/x^2, then.

I have no idea about matlab, so can't give you any real help!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K