Design and construct a computer program in one of the approv
Design and construct a computer program in one of the approved languages (e.g., C, C++, Java, or Pascal) that will illustrate the effects of rounding errors and truncation errors. Be sure to follow the documentation and programming style policies of the Computer Science Department.
The following is a plot of the function cosh(x) as a function of x: In order to illustrate the effects of the two major error sources, rounding and truncation, attempt to determine an approximation to the derivative of cosh(x) at x = 0.881373587019543 radians using the difference approximation given below.
(The true answer is about 1.0). Use the formula: f\'(x) ~ (f(x+h) - f(x)) / h with h=1, 0.5, 0.25, ... 1.8189894035459e-12 (i.e., keep halving h until it is less than 2.0e-12.)
Print out the values of h, your approximation to f\'(x), and the error in the approximation for each value of h used. This error will include the effects of both truncation and rounding.
Write down (on your output) any conclusions that you can make from these experiments .
Solution
############################################################
## Programmer: Michael Garza
## Course: CSCI 3321 Numerical Methods
## Date: 9-16-2015
##
## Assignment: 1
## Environment: Windows 7
## Files included: none
##
## Purpose: Design and construct a computer program in one of
## the approved languages (e.g., C, C++, Java, or
## Pascal) that will illustrate the effects of
## rounding errors and truncation errors.
##
## Input: a constant float at 0.881373587019543
## a float(h) starting from 1.0 and halfed to a very small num
##
## PreConditions: The program will generate rounding and truncation
## errors.
##
## Output: The values of h, the approximation to f\'(x), and the
## error in the approximation for each value of h used.
##
## Formulas: (f(x+h) - f(x)) / 2 where f(x) = cosh(x)
##
## Algorithm:
## h = 1.0
## while x is less than 2 x 10^-12
## print value of h
## calc formula and print
## half h
################################################################
import math
# method for determing the error approcimation
def formula(x,h):
return (math.cosh(x+h) - math.cosh(x)) / h
#declarations
x = 0.881373587019543
h = 1.0
maxValueTest = (2.0 * math.pow(10,-12))
print(\"The approximation to the derivative of cosh(x) is 1.0\")
print(\"The values below is the error in approximation.\")
print(\"------- h value --------\")
# main loop
while h > maxValueTest:
print(\"When h is \" + str(h) + \", the answer is:\")
print(\"{0:.40f}\".format(formula(x,h)))
print()
h = h / 2
OUTPUT:
The approximation to the derivative of cosh(x) is 1.0
The values below is the error in approximation.
------- h value --------
When h is 1.0, the answer is:
1.9432331928617097016598336267634294927120
()
When h is 0.5, the answer is:
1.4031713527991356293966873636236414313316
()
When h is 0.25, the answer is:
1.1881485947761660781907266937196254730225
()
When h is 0.125, the answer is:
1.0911096985304880035982932895421981811523
()
When h is 0.0625, the answer is:
1.0448597306568423448425164679065346717834
()
When h is 0.03125, the answer is:
1.0222616536002888665279897395521402359009
()
When h is 0.015625, the answer is:
1.0110894588419370165865984745323657989502
()
When h is 0.0078125, the answer is:
1.0055344723830614839243935421109199523926
()
When h is 0.00390625, the answer is:
1.0027646825096780958119779825210571289062
()
When h is 0.001953125, the answer is:
1.0013817041540278296452015638351440429688
()
When h is 0.0009765625, the answer is:
1.0006906929666001815348863601684570312500
()
When h is 0.00048828125, the answer is:
1.0003453067261034448165446519851684570312
()
When h is 0.000244140625, the answer is:
1.0001726434265947318635880947113037109375
()
When h is 0.0001220703125, the answer is:
1.0000863192290125880390405654907226562500
()
When h is 6.103515625e-05, the answer is:
1.0000431589942309074103832244873046875000
()
When h is 3.0517578125e-05, the answer is:
1.0000215793406823650002479553222656250000
()
When h is 1.52587890625e-05, the answer is:
1.0000107896339613944292068481445312500000
()
When h is 7.62939453125e-06, the answer is:
1.0000053948024287819862365722656250000000
()
When h is 3.81469726562e-06, the answer is:
1.0000026974012143909931182861328125000000
()
When h is 1.90734863281e-06, the answer is:
1.0000013486715033650398254394531250000000
()
When h is 9.53674316406e-07, the answer is:
1.0000006742775440216064453125000000000000
()
When h is 4.76837158203e-07, the answer is:
1.0000003371387720108032226562500000000000
()
When h is 2.38418579102e-07, the answer is:
1.0000001685693860054016113281250000000000
()
When h is 1.19209289551e-07, the answer is:
1.0000000838190317153930664062500000000000
()
When h is 5.96046447754e-08, the answer is:
1.0000000409781932830810546875000000000000
()
When h is 2.98023223877e-08, the answer is:
1.0000000149011611938476562500000000000000
()
When h is 1.49011611938e-08, the answer is:
1.0000000000000000000000000000000000000000
()
When h is 7.45058059692e-09, the answer is:
1.0000000000000000000000000000000000000000
()
When h is 3.72529029846e-09, the answer is:
1.0000000000000000000000000000000000000000
()
When h is 1.86264514923e-09, the answer is:
1.0000000000000000000000000000000000000000
()
When h is 9.31322574615e-10, the answer is:
1.0000000000000000000000000000000000000000
()
When h is 4.65661287308e-10, the answer is:
1.0000000000000000000000000000000000000000
()
When h is 2.32830643654e-10, the answer is:
1.0000000000000000000000000000000000000000
()
When h is 1.16415321827e-10, the answer is:
1.0000000000000000000000000000000000000000
()
When h is 5.82076609135e-11, the answer is:
1.0000000000000000000000000000000000000000
()
When h is 2.91038304567e-11, the answer is:
1.0000000000000000000000000000000000000000
()
When h is 1.45519152284e-11, the answer is:
1.0000000000000000000000000000000000000000
()
When h is 7.27595761418e-12, the answer is:
1.0000000000000000000000000000000000000000
()
When h is 3.63797880709e-12, the answer is:
1.0000000000000000000000000000000000000000
()



