1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import os import matplotlib.pyplot as plt import numpy as np import math os.chdir('/home/vg/Downloads/projects/ex10') os.getcwd() plt.figure(figsize=(10,8)) N = 100 start = 0 end = 2 A = np.random.rand() + 0.5 B = np.random.rand() x = np.linspace(start,end,N) y = B*np.exp(A*x) y = y + np.random.randn(N)/5 p = np.polyfit(x,np.log(y),1) plt.plot(x,y,'o',label='Given data: A=%.2f. B=%.2f' % (A,np.exp(B))) plt.plot(x,np.exp(np.polyval(p,x)),'-',label='Linear regression: A=%.2f. B=%.2f' % (p[0],np.exp(p[1]))) plt.legend(loc='best') plt.grid() plt.title('Linear regression, $y=Be^{Ax}$') plt.savefig("example10.png", dpi=100) plt.show() plt.close() <img class=" wp-image-69 aligncenter" src="https://gantovnik.com/bio-tips/wp-content/uploads/2018/12/example10.png" alt="example10" width="606" height="485" /> |
Recent Comments