Results 1 to 6 of 6

Thread: Sine and Cosine Calculations in C++???

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    35

    Sine and Cosine Calculations in C++???

    Im trying to generate 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

    Code:
    #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;
    }

  2. #2

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    35

    Re: Sine and Cosine Calculations in C++???

    this is what i have so far:

    don't know how to do inside of the code: calculations and writing data points?
    any assistance would be great

    Code:
    double t;
    for(t = 0.0; t <= 6.0; t += 0.01)
    {
      // Make calculations based on t
    
      // Write a data point to the file
    }

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    35

    Re: Sine and Cosine Calculations in C++???

    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:

    Code:
    // 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);

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    35

    Re: Sine and Cosine Calculations in C++???

    can anyone assist me with this program?

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    35

    Re: Sine and Cosine Calculations in C++???

    this is what i have:

    need help on the parts i have put comment statements:

    Code:
    #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;
    }

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    35

    Re: Sine and Cosine Calculations in C++???

    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;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width