Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that demonstrates that the integral of the cosine is the sine. T

ID: 3563307 • Letter: W

Question

Write a program that demonstrates that the integral of the cosine is the sine.

To do this, sum up the (approximate) area under the cosine curve at multiple steps (angles) from 0 to 2 PI, and compare the sum to to the actual sine value for  each angle.   Do this for 10 points,  20 points, 100 points, 1000 points and 10000 points.  Report the average absolute error and the largest absolute error.  (See abs( ) function on p. 102-104 to get the absolute value of a number)

Here are my results (but for powers of 2, not powers of 10) .  I've also calculate the RMS error (aka standard deviation), a common statistical measure of the spread of a measure;  google it up if you wish to calculate it.)

    npts   mean err    max err  RMS err

     8     0.3927     0.7854     0.2994

    16     0.1963     0.3927     0.1437

    64     0.0491     0.0982     0.0350

   512     0.0061     0.0123     0.0043

  4096     0.0008     0.0015     0.0005

For 10, 20 & 100 points, integrate cos(theta) from 0 to 2 PI, and print out the average error and maximum error.  Also, write the following information to a file for each point:  x[i], sin(x[i]), your estimate of sin(x[i]), and the difference (error).  Your tables should look something like this  (except that  I've run with 8 & 16, rather than 10 & 20).  For 100, your error terms should be pretty small (all less than about 0.1).  This will help you verify if your program is working properly, as you can easily work a few terms by hand to check them.

For npts = 8:

  i  x[i]     sin(x) est(sin(x))     error

  0 0.7854     0.7071     0.5554     0.1517

  1 1.5708     1.0000     0.5554     0.4446

  3 3.1416     0.0000    -0.7854     0.7854

...

For npts = 16:

  i  x[i]     sin(x) est(sin(x))     error

  0 0.3927     0.3827     0.3628     0.0199

  1 0.7854     0.7071     0.6405     0.0666

  3 1.5708     1.0000     0.7908     0.2092

...

Here's a plot of what my results look like, except that I ran it for 8, 16, 64, 512 & 4096 steps (Powers of 2 instead of powers of 10).  You can see that for 8 & 16 steps, the estimate is fairly far off, almost correct for 64, and almost exactly correct for 512 and 4096.  You don't need to plot your results (I used matlab, BTW).

Sin(theta) & Est. sin(theta) left, and errors at each point (right)

Explanation / Answer

For 100 pts:

#include <stdio>

const double PI = 3.141592653589;
const double DX = 2PI / 100;
const double FROM = 0;
const double TO = 2PI;

double sum = 0;

for (double x=FROM; x<TO; x+=DX)
     sum += sin(x)*DX;

cout << "Integral approximation: " << sum;

To change number of poitnts, just change DX value

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote