From cc0a9e14d5b310423a34a5bac8bd39aaf009634e Mon Sep 17 00:00:00 2001 From: Jae Yeong Park Date: Wed, 23 Mar 2022 14:25:31 +0900 Subject: [PATCH] linear_model --- curveFitting.py | 33 +++++++++++++++++++++++++++++++++ linear_model.py | 12 ++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 curveFitting.py create mode 100644 linear_model.py diff --git a/curveFitting.py b/curveFitting.py new file mode 100644 index 0000000..1153d03 --- /dev/null +++ b/curveFitting.py @@ -0,0 +1,33 @@ +import matplotlib.pyplot as plt +from scipy.optimize import curve_fit +import numpy as numpy + +#Fitting funtion +def func(x, a, b) + return a*np.exp(b*x) + #return a*x+b + +#Experimental x and y data points +xData = np.array([1,2,3,4,5]) +yData = np.array([1, 9, 50, 300, 1500]) + +#Plot experimental data points +plt.plot(xData, yData, 'bo', label='experimeatal-data') + +# Initial guess for the parameters +#initialGuess = [1, 0, 1, 0] + +#Perform the curve-fit +popt, pcov = curve_fit(func, xData, yData, initialGuess) +print(popt) + +#x values for the fitted function +xFit = np.arange(0.0, 5.0, 0.01) + +#Plot the fitted function +plt.plot(xFit, func(xFit, *popt), 'r', label= 'fit params: a= %5.3f, b=%5.3f' %tuple(popt)) + +plt.xlabel('x') +plt.ylabel('y') +plt.legend() +plt.show() \ No newline at end of file diff --git a/linear_model.py b/linear_model.py new file mode 100644 index 0000000..896ede1 --- /dev/null +++ b/linear_model.py @@ -0,0 +1,12 @@ +from scipy.optimize import curve_fit + +def linear_model(x, a, b): + y= a * x + b + return y + +x = [0, 1, 2, 3, 4, 5] +y = [15, 10, 9, 6, 2, 0] + +popt, pcov = curve_fit(linear_model, x, y) + +print(popt) \ No newline at end of file