An archive of Mark's Spring 2018 Numerical Analysis course.

Section 1.1 Question 6

brian

Section 1.1

Exercise 6

Compute the absolute error and relative error in approximations of p by \widetilde{p}.

(a) p=\sqrt{2}, \widetilde{p}=1.414
(b) p=10^{\pi}, \widetilde{p}=1400
(c) p=9!, \widetilde{p}=\sqrt{18\pi}(9/e)^{9}

CestBeau

I wrote some python code to calculate the absolute and relative error.

import numpy as np
import math
a = (2)**(1/2)
atilda = 1.414
absolute_error_a = atilda - a
print("a) Absolute Error:",absolute_error_a)
print("   Relative Error:",absolute_error_a / a)
b = (10)**np.pi
btilda = 1400
absolute_error_b = b-btilda
print("b) Absolute Error:",absolute_error_b)
print("   Relative Error:",absolute_error_b/b)
c = math.factorial(9)
ctilda = ((18*np.pi)**(1/2))*(9/np.e)**9
absolute_error_c = c-ctilda
print("c) Absolute Error:",absolute_error_c)
print("   Relative Error:",absolute_error_c/c)

# Out:
# a) Absolute Error: -0.00021356237309522186
#    Relative Error: -0.00015101140222192286
# b) Absolute Error: -14.544268632989315
#    Relative Error: -0.010497822704619136
# c) Absolute Error: 3343.1271580516477
#    Relative Error: 0.009212762230080598
mark

@CestBeau Hey, I like your code! Note that you can edit your original post to make things nice and self-contained, as I’ve done.