For loop help - estimating integral of cosine

Hello all I am writing a program to estimate the integral of cosine and compare it to the value of sine, then list the error. This is done by calculating them both from 0 to 2pi, in 10, 20, 100, and 1000 increments(1000 increments is more accurate, the purpose of this assignment is to show this). In my code is an estimation using 10 increments (as you can see it is increasing by pi/5 each time) and it works fine, I just need to be able to re run the loop using different values of m (pi/10, pi/50, pi/500) and then output all of the information to the file at once without manually changing the value of m and clicking "run" each time. I tried putting the existing for loop inside another with m = to the different values but doesn't work. Im thinking an array of m values somehow but the loop wont run with m as an array.
Any help would be greatly appreciated!


This is an example of what the file will look like, this is part of the data from a 100 increment run, but I would like to be able to scroll up and view for 20 inc and 10 inc


i x[i] sin(x) est sin(x) error
1 0.0628319 0.0627905 0.0627079 8.26508e-05
2 0.125664 0.125333 0.0623364 0.0629968
3 0.188496 0.187381 0.0617189 0.125662
4 0.251327 0.24869 0.0608579 0.187832
5 0.314159 0.309017 0.0597566 0.24926
6 0.376991 0.368125 0.0584196 0.309705
7 0.439823 0.425779 0.056852 0.368927
8 0.502655 0.481754 0.05506 0.426694
9 0.565487 0.535827 0.0530507 0.482776
10 0.628319 0.587785 0.050832 0.536953
11 0.69115 0.637424 0.0484128 0.589011
12 0.753982 0.684547 0.0458024 0.638745
13 0.816814 0.728969 0.0430114 0.685957
14 0.879646 0.770513 0.0400505 0.730463


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 <math.h>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;

int main(void)

{
    
    string fname = "DataFile.txt";
    double pi = 3.141592653589;
    double value;
    double error = 0, maxerror = 0, meanerror = 0, toterror = 0;
    double x = 0;
    double n = (2*pi);
    double m = pi/5;
    double sum = 0;
    int it = 0;
    ofstream ofs;
    
    ofs.open(fname);
    if (!ofs) {
        cerr << "ERROR, the file cannot be opened.";
        return(1);
    }
    
    ofs << "i" << "   " << "x[i]" << "       " << "sin(x)" << "       " << "est sin(x)" << "       " << "error" << endl;
    
    for ( x = m; x <= n; x += m ) {
        it = it + 1;
        sum = cos(x) * (m);
        value = sin(x);
        cout << "Iteration: " << it << endl;
        cout << "Taking sin of: " << x << endl;
        cout << "sin value: " << value << endl;
        error = abs(value - sum);
        if (error > maxerror)
            maxerror = error;
        toterror = toterror + error;
        meanerror = toterror / it;
        cout << "Integral Approximation: " << sum << endl << "Current Error: " << error << endl << "Mean Error: " << meanerror << endl << "Max Error: " << maxerror << endl;
        cout << endl;
        ofs << it << "   " << x << "   " << value << "   " << sum << "   " << error << endl;
    }
    
    ofs.close();
    cout << "File successfuly opened and closed." << endl;
    
    return(0);
}
Last edited on
Hi,

Try to avoid using doubles in a for loop like that, they aren't exact. Denis Ritchie will be rolling in his grave ! You could be 1 iteration short, or go past the limit by 1 iteration. This is because the difference between a particular number representation and it's exact value may differ by say +/-2e-15, causing the current value to be that much less or more than the end value. Instead, convert to a while loop, with a little function to make it stop in the right place:

1
2
3
4
5
6
7
8
9
10
11
12
x = m;
while (near (x, n) ) { // good enough to being equal
// ...

x +=m; // increment
}

const double Precision = 1e-6;

bool near(const double x, const double n) {
  return abs(x - n) < Precision ? true : false; // ternary operator  condition ? do if true statement : do if false statement
}


To answer your question, try putting the code into a function that takes an argument double m Then calculate different values of m and call the function repeatedly with them.

good luck !!
math.h and cmath are redundant, just use cmath.

as for looping over the values, you can also do something silly like

for(int i = 0; i < 361; i++)
{
dostuff for i*degreestoradians;
}

Topic archived. No new replies allowed.