<jupyter_start><jupyter_text>**Exercise 1**: Try to approximate $f'(1)$ where $f(x)=e^x$ and determine (using your code) the optimal step size in achieving the best accuracy.---<jupyter_code>import math
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>$f(x) = e^x$<jupyter_code>def f(x):
  return math.e ** x<jupyter_output><empty_output><jupyter_text>Consider the formula for approximating 1st derivative:$$f'(x_0) = \frac{-2f(x_0-3h)+9f(x_0-2h)-18f(x_0-h)+11f(x_0)}{6h} + O(h^3)$$<jupyter_code>def fp(x,i):  
  h = 10 ** (-i)
  return ( -2*f(x-3*h)+9*f(x-2*h)-18*f(x-h)+11*f(x) ) / (6*h)<jupyter_output><empty_output><jupyter_text>Approximate $f'(-1)$ and the error, and draw the "error graph"<jupyter_code>x = []
y = []
for i in range(1,17):
  #print("h = ",10**(-i),":")
  x.append(-i)
  #print("f'(-1) = ",fp(1,i))
  e = abs(fp(1,i) - (math.e)**1)
  #print("error = ", e) #誤差
  y.append(math.log10(e))
  #print("----")
fig = plt.plot(x,y,label='error')
plt.xlabel('log10(h)', fontsize=20)
plt.ylabel('log10(error)', fontsize=20)
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>結論 從圖上可看出當h近似於$10^{-4}$時，有最小誤差。<jupyter_code>print(fp(1,4)-math.e)<jupyter_output>-6.248335182590381e-13<jupyter_text>估計出的$f'(1)$<jupyter_code>print(fp(1,4))<jupyter_output>2.7182818284584203
{'fraction_non_alphanumeric': 0.2283763277693475, 'fraction_numerical': 0.06980273141122914, 'mean_word_length': 6.804733727810651, 'pattern_counts': {'":': 1, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '187185', 'license': 'MIT', 'n_tokens_mistral': 679, 'n_tokens_neox': 644, 'n_words': 83, 'path': 'sec5exercise01.ipynb', 'repo_name': 'teshenglin/computational_mathematics', 'size': '1318'}