AMAT 2120, Solution 3
Assignment 3 -- Solution and Common Mistakes
Solution (possible program):
main file
file with functions
Here is a
script file and
a printout of Maple session,
where I checked the the results.
- Comments to the tests:
-
Test 1: regular values, no command line arguments
-
Tests 2-3: invalid data (Test 2: invalid x, Test 3: invalid y)
-
Tests 4-9: testing command line argument (CLA) switch
-
Test 4: CLA= 1 (arithmetic only)
-
Test 5: CLA= 2 (geometric only)
-
Test 6: CLA= 3 (harmonic only)
-
Test 7: CLA= 4 (AGM only)
-
Tests 8-9: invalid CLA (Test 8: CLA=-1, Test 9: CLA=5)
- Tests 10-13: numerical stress-tests (trying to fail the program)
-
Test 10: x=2, y=2E+7. The program never ends (failed).
-
Test 11: x=2, y=2E+6: Passed. Results compare well with Maple's.
-
Test 12: (Trying to approach the program's bound closer):
x=2, y=1E+7: Passed. Results compare well with Maple's.
-
Test 13: (switching the arguments):
x=1E+7, y=2: Passed.
Common Mistakes
-
In AGM loop: the sequence
x=a(x,y);
y=g(x,y);
results in the use of already updated value of x
in calculation of the geometric mean.
Mathematically, this piece of code corresponds to
the recurrence
xn+1= a(xn, yn),
yn+1= g(xn+1, yn),
rather than to
xn+1= a(xn, yn),
yn+1= g(xn,
yn).
-
The Harmonic Mean h(x,y) is defined by the equation
1/h= arithmetic(1/x,1/y).
Some students computed the arithmetic mean of the reciprocals
but forgot to invert the result.
-
Some programs aren't mathematically correct. Mistakes could
be found or at least noted had the authors use Maple or
other tools (a handbook, for instance, or the Internet) to check
numerical answers.