problem with sin equation

Pages: 123
Nov 17, 2011 at 1:39pm
i want to output the answer to a sin equation in a file.
my code is my first attempt i've probably made loads of mistakes.
this is what i have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "stdafx.h"
#include "iostream"
#include "cmath"
#include <iomanip>
#include <fstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	ofstream outputFile;
	outputFile.open("sin_result.txt"); //output file

	int t;
	const double pi = 3.141592654;
	double value;
	double angle;

	for (t = 0; t < 40; t+2) //t goes up in 2's
	{
		angle = 2 * pi * 50 * t; //find the angle

		value = sin(angle); //get sin of angle

		sin(angle)*0.22; //multiply angle by 0.22


	}

	cout << "t" << "\t" << "2*pi*50*t" << "\t" << "sin(2*pi*50*t)" << "0.22*sin(2*pi*50*t)" << endl;
	//list results of t, angle, sin of angle and 0.22*angle


	outputFile.close();

	return 0;
}


so i have T which is going up in 2's
i then need to mulitply T by 2*pi**50
then need to find the sin of that
then multiply than be 0.22

I then want to output each of these results into a file.
It builds put nothing comes up on the screen
Last edited on Nov 17, 2011 at 1:41pm
Nov 17, 2011 at 1:41pm
It builds put nothing comes up on the screen


If you uncomment the cout line, it might put something on screen.
Last edited on Nov 17, 2011 at 1:42pm
Nov 17, 2011 at 1:51pm
sorry that was a mistake. but it still doesnt work
Nov 17, 2011 at 1:56pm
for (t = 0; t < 40; t+2)

Your for loop is broken. Did you mean

for (t = 0; t < 40; t = t+2)
Last edited on Nov 17, 2011 at 1:57pm
Nov 17, 2011 at 2:05pm
yes i want t to go up in 2's ie 0,2,4,6 etc

ok so know its displaying the line cout << "t" << "\t" << "2*pi*50*t" << "\t" << "sin(2*pi*50*t)" << "0.22*sin(2*pi*50*t)" << endl;

i now want to display the results of all the equations i need in the for loop
Last edited on Nov 17, 2011 at 2:05pm
Nov 17, 2011 at 2:06pm
Put a cout in the for loop to display the results of the equations.
Nov 17, 2011 at 2:21pm
ok done that but i think my equations are working right
heres the output

t       2*pi*50*t       sin(2*pi*50*t)  0.22*sin(2*pi*50*t)
0       0               0               0
2       628.319         8.20413e-008            1.80491e-008
4       1256.64         1.64083e-007            3.60982e-008
6       1884.96         2.46124e-007            5.41473e-008
8       2513.27         3.28165e-007            7.21964e-008
10      3141.59         4.10207e-007            9.02455e-008
12      3769.91         4.92248e-007            1.08295e-007
14      4398.23         5.74289e-007            1.26344e-007
16      5026.55         6.56331e-007            1.44393e-007
18      5654.87         7.38372e-007            1.62442e-007
20      6283.19         8.20414e-007            1.80491e-007
22      6911.5          9.02454e-007            1.9854e-007
24      7539.82         9.84496e-007            2.16589e-007
26      8168.14         1.06654e-006            2.34638e-007
28      8796.46         1.14858e-006            2.52687e-007
30      9424.78         1.23062e-006            2.70736e-007
32      10053.1         1.31266e-006            2.88785e-007
34      10681.4         1.3947e-006             3.06834e-007
36      11309.7         1.47674e-006            3.24884e-007
38      11938.1         1.55878e-006            3.42933e-007
Press any key to continue . . .

Last edited on Nov 17, 2011 at 2:27pm
Nov 17, 2011 at 2:28pm
ok done that but i think my equations are working right

Good to hear it. Unless you meant to say you think they're not working right? You did read the documenation on the sin function, yes? So you know that it works in radians? The sin of 2*pi is zero, and so is sin 4*pi, and sin 6*pi, and on and on and on for every multiple of 2*pi. Given that all your sin calculations are for a multiple of 2*pi, your answers look pretty accurate.

The last thing that happens in each loop is t=t+2. Then the condition t<40 is checked, so if t does equal 40 at the start of a loop, that loop will not be run. If you want the loop to run when t=40, you need to change the condition to t<=40
Last edited on Nov 17, 2011 at 2:31pm
Nov 17, 2011 at 5:54pm
ya i meant to say they are not working right because we were given an example of where t was going up in 5's so 2*pi*50*t was π/2(that should be the symbol for pi it doesnt copy over right) then for 10 it was π etc. so how do i get a similar answer?
These points are points for plotting a sin graph so the numbers i have won't work

the graph in the end should look something like these:
http://www.google.ie/imgres?q=sine+wave+graph&hl=en&biw=1360&bih=587&gbv=2&tbm=isch&tbnid=oJ2eIa-eurAqkM:&imgrefurl=http://www.intmath.com/trigonometric-graphs/1-graphs-sine-cosine-amplitude.php&docid=UDPUCTbk1CVn-M&imgurl=http://intmstat.com/trigonometric-graphs/sinx.gif&w=328&h=207&ei=h0rFTtvbK86DhQeyyfHxDQ&zoom=1&iact=hc&vpx=480&vpy=184&dur=156&hovh=165&hovw=262&tx=148&ty=95&sig=103543758652278128934&page=1&tbnh=97&tbnw=153&start=0&ndsp=21&ved=1t:429,r:2,s:0

ie π/2 = 1, π = 0 etc. these are the values to make the wave. I need to get something similar
Last edited on Nov 17, 2011 at 6:00pm
Nov 17, 2011 at 6:40pm
Use much smaller steps.
Nov 17, 2011 at 7:13pm
i need more equations?
Nov 17, 2011 at 7:16pm
No. Smaller steps.

For example,

t=t+0.1
Last edited on Nov 17, 2011 at 7:16pm
Nov 17, 2011 at 7:17pm
what do you mean?
Nov 17, 2011 at 7:20pm
for (t = 0; t < 40; t = t+0.1)
Nov 17, 2011 at 7:23pm
but i was told to use for (t = 0; t < 40; t = t+2) its part of the assignment i got
Nov 17, 2011 at 7:26pm
Is the point of the assignment to show you the effect of aliasing - that is, the mistake that happens if you sample a signal at exactly the same frequency as the signal itself? Because that's the effect you're demonstrating.
Nov 17, 2011 at 7:29pm
no the assignment is to get the plot points for a sine wave using s(t) = (.22)sin(2*pi*50*t). this is where i got my equations from
Last edited on Nov 17, 2011 at 7:30pm
Nov 17, 2011 at 7:33pm
Assuming you're working in radians:

sin(2*pi*50*t) will always, ALWAYS equal zero if t is a whole number. Always always always.

So (.22)sin(2*pi*50*t) will always, ALWAYS equal zero if t is a whole number.

s(t) will thus always ALWAYS equal zero if t is a whole number.
Last edited on Nov 17, 2011 at 7:33pm
Nov 17, 2011 at 7:38pm
is there a way to change the sin function then? i was just told that it takes a single argument, the angle in radians whose sin you require. like i said in the other reply we were given an example using this formula where t was going up in 5's and none of those answers where equal to 0 so why wont it work if i change t to t = t+2?
Nov 17, 2011 at 7:46pm
given an example using this formula where t was going up in 5's and none of those answers where equal to 0


They're all equal to zero.

t = 5 : http://www.wolframalpha.com/input/?i=sin+%282*pi*50*5%29
t = 10 : http://www.wolframalpha.com/input/?i=sin+%282*pi*50*10%29
t = 15 : http://www.wolframalpha.com/input/?i=sin+%282*pi*50*15%29
t = 20 : http://www.wolframalpha.com/input/?i=sin+%282*pi*50*20%29
t = 25 : http://www.wolframalpha.com/input/?i=sin+%282*pi*50*25%29


I suspect you've simply copied down the equation incorrectly. If the assignment is to use this exact equation:
s(t) = (.22)sin(2*pi*50*t)
and t must be an integer, then the assignment is nonsense.

If the assignment is to produce points so you can use them to plot a sin curve, then you can stop messing around with that ridiculous equation and use something like:

s = sin(pi * t)

with t going from zero to 2 in steps of 0.1


Note also that in your original code this line dod nothing:
sin(angle)*0.22; //multiply angle by 0.22

Presumably you meant something like
value = 0.22 * value;
Last edited on Nov 17, 2011 at 7:47pm
Pages: 123