Why does the Y value change strangely?

71 Views Asked by At

Q. Calculate and plot the true relative errors of the approximations of cos(π/3) using N-th (N = 0, 1, 2, .... 10) order Taylor series expansions of cos(x) at x = π/4. Plot the true relative errors(y-axis) as a function of N(x-axis), with the y-axis(true relative error) set as log-scale

A.

import numpy as np
import matplotlib.pyplot as plt

def cos_taylor(x, N):
    s = 0
    for n in range(N+1):
        term = ((-1)**n)*(x**(2*n))/np.math.factorial(2*n)
        s += term
    return s
    
true_value = np.cos(np.pi/3)
N_values = np.arange(11)
true_relative_errors = np.zeros(N_values.size)
    
for i, N in enumerate(N_values):
    approx_value = cos_taylor(np.pi/4, N)
    true_relative_errors[i] = np.abs((true_value - approx_value)/true_value)
        
plt.semilogy(N_values, true_relative_errors, 'bo-')
plt.grid()
plt.yscale('log')
plt.xlabel('Nth-order Taylor series')
plt.ylabel('err')
plt.show()

This is the code I wrote and I want the output to look like this: enter image description here

However, the value of Y appears different from the answer. I think there is nothing wrong with the code, can you tell me why this result is appearing? This is my code's result : enter image description here

enter image description here

I want the values of Y to gradually decrease like in this graph. (I want the value of the error to be reduced.)

this is all the question : enter image description here

1

There are 1 best solutions below

0
B Remmelzwaal On

The code is nearly right, and I'm positive the person who gave you this assignment has made a mistake somewhere. The difference between cos(π/3) and cos(π/4) is about -0.2, which will never give an error to the order of 10^-11, even for perfect approximations.

Using π/3 in both cosine functions gives a graph that is much more like the desired result, though the error seems off. Given cos(π/3) = 0.5 and the first order Taylor series expansion equaling 1, the error should be 1 since |1-0.5|/0.5 = 1, and the target graph somehow gets an error greater than 10.

Furthermore, the correct Taylor series nearly converges at n = 9, whilst the target graph still seems far from converging.

I would suggest contacting whoever gave the assignment and making sure that both the question and the true relative error are correct, as doing the calculations properly does not give the same graph.

Below, I have made small tweaks to your code that include:

  • Using np.float128 for extended precision when calculating smaller Taylor series terms;
  • Correcting the formula for the true relative error (though the results of both are the same);
  • Putting constants in a different variable;
  • Removing the redundant enumerate;
  • Making the line black.
import numpy as np
import matplotlib.pyplot as plt

def cos_taylor(x, N):
    s = 0
    for n in range(N+1):
        term = np.float128(((-1)**n)*(x**(2*n))/np.math.factorial(2*n))
        s += term
    return s

cos_val = np.pi/3
data_points = 11

N_values = np.arange(data_points)
true_relative_errors = np.zeros(data_points)

true_value = np.cos(cos_val)
    
for i in N_values:
    approx_value = cos_taylor(cos_val, i)
    true_relative_errors[i] = np.abs(approx_value - true_value)/true_value

plt.semilogy(N_values, true_relative_errors, 'ko-')
plt.grid()
plt.yscale('log')
plt.xlabel('Nth-order Taylor series')
plt.ylabel('err')
plt.show()

This gives the following plot: