2 Comments
User's avatar
Orith Loeb's avatar

Hii,

Please hereby my solution:

# DDC-40: Estimating e

import numpy as np

import matplotlib.pyplot as plt

from sklearn.preprocessing import MinMaxScaler

# --- generating the n---

n = np.linspace (1, 50)

# ---the function---

est_func = [(1+1/n)**n for n in n ]

true_e = np.e

# ---converting the list to numpy array ---

estimates =np.array(est_func, dtype = float)

# --- compute descrepancies---

discrepancies = np.abs (estimates-true_e).reshape (-1,1)

# --- Apply min-max scaling to map the discrepancies

scaler = MinMaxScaler()

colors_scaled = scaler.fit_transform (discrepancies).flatten()

# --- scatter plot---

plt.figure (figsize=(12,5))

scatter = plt.scatter (n, estimates, c = colors_scaled, cmap = "plasma", s = 100, edgecolor = "black")

plt.axhline (y = true_e, color ="orange", linestyle ="--", linewidth = 2)

plt.xlabel("n")

plt.ylabel ("estimate of e")

plt.ylim (1.95, 2.8)

plt.gca().spines ["top"].set_visible (False) # hide top border

plt.gca().spines ["right"].set_visible (False) # hide right border

Expand full comment
Mike X Cohen, PhD's avatar

Excellent solution.

Expand full comment