Sine and Cosine Calculations in C++???

Im trying to generat sample values of x(t), y(t), and z(t) at a sample
rate of 0.01 for "0 less than or equal to" t and "less than or equal to 6"

Basically trying to store the sampled values in their respective files, but kind of lost on where to start?

x(t) = sin(2*pi*t)
y(t) = 2*sin(16*pi*t/3) + cos (2*pi*t/3)
z(t) = x(t)*z(t)

*all i know is this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <math.h>
#include <iostream>
#include <iomanip>
using namespace std;


#define PI 3.14159265

int main ()
{
double rate, result;
rate = 30.0;
result = sin (rate*PI/180);
cout<<"The sine of " << rate << " degrees is " << result <<endl;
return 0;
}
this is what i have so far:

don't know how to do this inside of the code?
any assistance would be great

1
2
3
4
5
6
7
8
9

double t;
for(t = 0.0; t <= 6.0; t += 0.01)
{
  // Make calculations based on t

  // Write a data point to the file
}
The comments seem pretty explanitory to me...you should at least be able to do the calculations. For the file, read:

http://www.cplusplus.com/doc/tutorial/files/
this is what i have so far:
doesn't run properly though.

also im trying to store the sampled values in their respective files and use excel to plot the sampled x(t), y(t), and z(t)...how do i do that

my code:

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

// Text File Writing (SIN and COS).cpp : main project file.

define _USE_MATH_DEFINES
#include <math.h>
#include <iostream>
#include <iomanip>

using namespace std;




int main()
{

FILE *f;
double x,y,z,t;

f=fopen("c:/data.txt","a+t");
     ///is this where my excel file goes??

for(t = 0.0; t <= 6.0; t += 0.01)
{
  // Make calculations based on t
  x = sin(2*M_PI*t);
  y = 2*sin(16*M_PI*t/3) + cos (2*M_PI*t/3);
  z = x*y; 
  // Write a data point to the file
  fprintf(f,"%.5lf %.5lf %.5lf\n",x,y,z); //5 decimal places of precision
  //want to change this to cout? how do i do this?

}

fclose(f);
Last edited on
can anyone assist me with this program?
this is what i have:

need help on the parts i have put comment statements:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

// Some math headers define M_PI for you; some do not
#ifndef M_PI
const double M_PI = 3.14159265358979323846;
#endif

using namespace std;

int main()
{

    double x, y, z, t;
    double omega = 2.0 * M_PI;
    char *outname = "data.txt";
    ofstream outfile(outname);
    if (!outfile) {
        cout << "Can't open file " << outname << " for writing." << endl;
        exit(EXIT_FAILURE);
    }
    cout << "Opened file " << outname << " for writing." << endl;

    outfile << fixed << setprecision(5);
    cout    << fixed << setprecision(5);

    for (t = 0.0; t <= 6.0; t += 0.01) {
        // Make calculations based on t
        x = sin(omega * t);
        y = 2 * sin(8.0*omega * t / 3.0) + cos(omega * t / 3.0);
        z = x * y;

        // Show the output stuff on the console
        // After debugging, you may want to comment out the following line
        // I'll write the value of t as well as the computed
        // values
        cout << t << " " << x << " " << y << " " << z << endl;

        // Write a data point to the file
        // This puts spaces between the fields.  If you want
        // commas or tabs, then use "," or "\t" between
        // the output values instead of " "
        outfile << t << " " << x << " " << y << " " << z << endl;

    }
    outfile.close();
    return 0;
}

just want to state the problem again:

Write a C++ program to generate the sampled values of x(t), y(t), and z(t) at the sample rate of 0.01 for 0 less than or equal to t less than or equal to 600. Store the sampled values in their respective files and use Excel program to plot the sampled x(t), y(t), and z(t).

x(t) = sin (2*pi*t)
y(t) = 2*sin (16*pi*t/3) + cos (2*pi*t/3)
z(t) = x(t) * y(t)

Do not use for loop:
for (t=0; t less than 600; t=t+0.01)
instead
use for loop:
for (i=0; t less than 600; i++)
{

}

*Send this character to file '/t'
x[i] << '/t' << y[i] << '/t' << z[i] << endl;
Topic archived. No new replies allowed.