unexpected result from Math.cos()


H

harryos

hi
I was trying to calculate the value of
x(t)=cos(2*pi*10*t)+cos(2*pi*25*t)+cos(2*pi*50*t)+cos(2*pi*100*t) for
some values.I wrote this code

....
public static void main(String[] args) {
double m1=0.0;
double m2=0.0;
double m3=0.0;
double m4=0.0;
double y=0.0;
for(int t=0;t<3;t++){
debug("t="+t);
m1=Math.cos(Math.PI*2*10*t);
m2=Math.cos(Math.PI*2*25*t);
m3=Math.cos(Math.PI*2*50*t);
m4=Math.cos(Math.PI*2*100*t);
y=m1+m2+m3+m4;
System.out.println("m1="+m1);
System.out.println("m2="+m2);
System.out.println("m3="+m3);
System.out.println("m4="+m4);
System.out.println("y="+y);

}

}
....

however ,I got the following output
t=0
m1=1.0
m2=1.0
m3=1.0
m4=1.0
y=4.0

t=1
m1=1.0
m2=1.0
m3=1.0
m4=1.0
y=4.0

t=2
m1=1.0
m2=1.0
m3=1.0
m4=1.0
y=4.0
-----
I don't understand this..For example, the value of cos(2*pi*10*1)
should be cos 62.832 which is .4566 .Instead I am getting the value 1.
can somebody help me find out if I am doing something wrong?
thanks
harry
 
Ad

Advertisements

A

Arved Sandstrom

harryos said:
hi
I was trying to calculate the value of
x(t)=cos(2*pi*10*t)+cos(2*pi*25*t)+cos(2*pi*50*t)+cos(2*pi*100*t) for
some values.I wrote this code
[ SNIP ]
I don't understand this..For example, the value of cos(2*pi*10*1)
should be cos 62.832 which is .4566 .Instead I am getting the value 1.
can somebody help me find out if I am doing something wrong?
thanks
harry

You'll get dozens of answers here, but I see none posted yet - the
argument is in radians, not degrees. So cos (20pi) = 1.

AHS
 
L

Lars Enderin

harryos said:
thanks for the reply..
if I use

m1=Math.cos(Math.PI*2*10*t*180/Math.PI)

will that do the conversion?
What's the point of multiplying, then dividing, with PI?
If t is in degrees, you need to multiply with PI/180 to get radians.
 
A

Andreas Leitgeb

harryos said:
if I use m1=Math.cos(Math.PI*2*10*t*180/Math.PI)
will that do the conversion?

If with 20pi you really thought of an angle in degrees,
(that would be slightly larger than the angles of an
equilateral triangle), then Math.PI*2*10*t*180/Math.PI
or just: 3600*t does it.

Usually it doesn't make any sense in real world to use
Pi-multiples (like 20pi), interprete them as degrees
and then convert them to radians to feed to trigonometric
functions, but if you're just playing around, then such a
sense is of course not a necessity.
 
A

Andreas Leitgeb

Andreas Leitgeb said:
If with 20pi you really thought of an angle in degrees,
(that would be slightly larger than the angles of an
equilateral triangle), then Math.PI*2*10*t*180/Math.PI
or just: 3600*t does it.

Damn, slipped into the trap, myself.
if 20pi was an angle in degrees, then the argument to
cos had rather be Math.PI*2*10*t*Math.PI/180, that
is (pi²/9)·t

sorry for confusion.
 
Ad

Advertisements

J

jimgardener

On Oct 28, 5:17 pm, Andreas Leitgeb
if 20pi was an angle in degrees, then the argument to
cos had rather be Math.PI*2*10*t*Math.PI/180, that
is (pi²/9)·t

the equation looks like an expression for a stationary signal with
frequencies 10,25,50 and 100.(.I am not very familiar with signal
processing stuff.but I remember seeing this as an example for
stationary signal.)If you plot the x(t) against different values of t
(time) then you will only get a straight line parallel to t axis if
you use
m1=Math.cos(Math.PI*2*10*t);
m2=Math.cos(Math.PI*2*25*t);
m3=Math.cos(Math.PI*2*50*t);
m4=Math.cos(Math.PI*2*100*t);
y=m1+m2+m3+m4;

May be you need some conversion to get a good plot..Can someone
familiar with this domain comment?

jim
 
A

Andreas Leitgeb

jimgardener said:
the equation looks like an expression for a stationary signal with
frequencies 10,25,50 and 100.(.I am not very familiar with signal
processing stuff.but I remember seeing this as an example for
stationary signal.)If you plot the x(t) against different values of t
(time) then you will only get a straight line parallel to t axis if
you use
m1=Math.cos(Math.PI*2*10*t);
m2=Math.cos(Math.PI*2*25*t);
m3=Math.cos(Math.PI*2*50*t);
m4=Math.cos(Math.PI*2*100*t);
y=m1+m2+m3+m4;
May be you need some conversion to get a good plot..Can someone
familiar with this domain comment?

Of course it depends on the sample rate of t: is it watched only in
discrete units?

"Hey, I marked the top position of a wheel and then I rolled it two
complete rotations forward, and guess what? The mark was on top,
not only afterwards but also at halftime!"
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top