Here are some hints, suggestions, and comments on the assignment.
Update to area.cpp
This is just some programming practice. Use other
Session 1 and Session 2 programs for examples.
Also, use a search
engine (e.g., Google) to find examples. E.g., search for
"c++ predefined pi" or "c++ inline square function"
and look at several of the answers ---
not only the first one!
You'll often get pointed to
stackoverflow.com, which is
a great online resource!
Summing up vs. summing down
You should find it useful to think of how the 1 + a + a + ... vs.
a + a + ... + 1 problem from Session 2 relates to this one. (That
might also be a good code to start from in writing one for this
If you only consider relatively small N,
summing up and summing down should both have an error about equal
to the machine precision. (Why?)
So what should your plot show in this region?
Be sure to increase N
until you see a different trend (I used up to 10^8).
If you are looking for the effects of errors over a logarithmic
range (e.g., N from 10^2 to 10^8), you should vary N by multiplying
by a factor each time through a loop and not by adding 1 (or some other
number). That is, don't use:
for (N = 100; N <= 100000000; N++)
which will take an enormous amount of time calculating lots of N values
you don't need. Instead, use something like:
for (N = 100; N <= 100000000; N *= 10)
which just calculates for 100, 1000, 100000, etc.
Using 1000000000 is hard to read (you can't tell at once what power
of 10 this is). What could you replace it with that is much clearer?
When you output relative errors to a file for plotting,
the "scientific" format is usually most useful,
again because you are looking at logarithmic intervals.
(See the codes we've used in class for examples.)
It is only the absolute value of the relative error that is of
interest. So use "fabs" (you need to "input ").
It is particularly important to have only
positive numbers if you are going to plot on logarithmic scales!
Be careful that you don't confuse the machine precision with the
smallest floating point number. For example,
in single precision, the machine
precision is about 10-7, but that does not
mean that 10^-10 is set to zero. It is only when you add
two numbers that differ by a factor of more than
10-7 that the smaller number is effectively zero.
A common bug when you have an outer loop stepping through
N_max and an inner loop that does the sum of 1/n up to N_max
is forgetting to reset your sum to zero when you start the
inner loop. So if your summation variable is sum_up,
make sure that sum_up = 0.; appears within the outer
loop and not just at the beginning of the program.
When interpreting a linear region I'm not looking for
a quantitative interpretation of the slope. [Note: If you print
enough points, the "linear" region just means the envelope of
the largest errors.]
Only a qualitative
interpretation of what you are seeing is needed (e.g., what does linear
mean on a log-log plot?).