We are coding with python in my numerical analysis class and I am quite new to it, so I am not very sure I did it right and would appreciate your help. With python I need to compute:
i) 0.01^2 and enter the answer rounded to 5 significant figures
ii) f(0.000122) where f(x) = cosh(x) + cos(x) - 2, and round your answer to 18 decimal places.
iii)Calculate the absolute error | f(x) - p_4(x) | at the point 0.981 if p_4(x) is the 4th-degree Taylor polynomial approximating f(x) = exp( pi*x ) with x_0 = 0
What I did:
i) My code would simply be
x=0.001**2:
print(x)
Here I get 0.0001 but how should I round this now to 5 figures?
ii) This time I had:
import numpy as np
x = 0.000122
y = np.cosh(x) + np.cos(x) - 2
print(round(y, 18))
But the solution gives me 0.0 and this seems wrong because... again it says round it to 18 decimal places.
iii) Here I have just started with the taylor series:
import math
x = math.pi*0.981
p4 = x**0/math.factorial(0) + x**1/math.factorial(1) + x**2/math.factorial(2) + x**3/math.factorial(3) + x**4/math.factorial(4)
print(p4)
Is this ok? Where do I use that $x_0=0$? I am always unsure, when I don't use every hint. My next step then would be:
error=abs(math.e**(math.pi*0.981)-p4)
Is this ok?
round()). You can round only for display purposes by using string formatting of floating point numbers. Tryf"{1/3:.18f}"(with the leading f before the string - it's an f-string) in a Python version 3.6 or newer to see one third rounded to 18 decimal places.eformat specifier?especifier forces e-notation (i.e.x * 10^mformat), if you specifyf"{0.001:.18e}", you'd get the 18 decimals after the most significant non-zero digit, whereas forf"{0.001:.18f}"you get the 18 decimals after the unit position. I assumed the latter to be the most likely interpretation, but you're of course correct that the other one could be a possible interpretation, too.fspecifier it can be useful, depending on the magnitude of the value. Granted, whoever posed the question might not have known that fact.