scientific-programming-exer.../ex_30.py

22 lines
457 B
Python
Raw Normal View History

2019-01-09 13:19:33 +00:00
from random import uniform
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize
def f(n, f0=100):
result = f0
for i in range(n):
result *= np.exp(0.2 + uniform(-0.04, 0.04))
return result
X = np.arange(0, 100, 1)
Y = np.array([f(int(x)) for x in X])
fitf = lambda x, k, a: k*np.exp(x*a)
(k, a), pcov = scipy.optimize.curve_fit(fitf, X, Y, p0=[100, 0.2])
fitted = lambda x: k*np.exp(x*a)
plt.plot(X, Y, X, fitted(X))
plt.show()