How to do exponential and logarithmic curve fitting in Python?


Introduction on Exponential and Logarithmic Curve Fitting

Exponential and logarithmic bend fitting are scientific procedures utilized to discover the best-fitting bends for a given set of information focuses that show exponential development or rot (within the case of exponential bend fitting) or a logarithmic relationship (within the case of logarithmic bend fitting). These methods permit us to demonstrate and get the fundamental designs and patterns inside the information.

Exponential Bend Fitting

Exponential capacities have the shape y = ae^(bx), where 'a' and 'b' are constants, 'e' speaks to Euler's number (around 2.71828), and 'x' is the free variable. Exponential bend fitting includes finding the values of 'a' and 'b' that result within the best fit between the given information focuses and the exponential function.

Exponential bend fitting is commonly utilized in different areas, counting back, science, material science, and financial matters, to analyze information that shows exponential development or rot. For cases, populace development, radioactive decay, and compound intrigued are frequently modelled utilizing exponential capacities.

Logarithmic Bend Fitting

Logarithmic capacities are y = a b * ln(x), where 'a' and 'b' are constants, 'ln' indicates the normal logarithm, and 'x' is the independent variable. Logarithmic bend fitting includes finding the values of 'a' and 'b' that surrender the finest fit between the given information focus and the logarithmic function.

Logarithmic bend fitting is valuable when the relationship between the autonomous and subordinate factors takes after a logarithmic design. It is frequently connected to information examination, science, material science, and innovation. For occasion, in chemical responses, the concentration of a reactant may diminish logarithmically over time.

Both exponential and logarithmic bend fitting methods point to deciding the ideal parameters that minimize the contrast between the fitted curve and the accurate information focuses. This optimization preparation is ordinarily carried out utilizing numerical calculations that iteratively alter the parameters until an ideal fit is accomplished.

Exponential and Logarithmic Curve Fitting in Python

Bend fitting may be an effective strategy to discover a numerical show that fits a given set of information focuses. Exponential and logarithmic capacities are commonly utilized to demonstrate knowledge with exponential development or rot. Python provides various libraries, such as NumPy and SciPy, which offer solid tools for curve fitting. This article will investigate step-by-step strategies and give Python code illustrations to perform exponential and logarithmic bend fitting.

Exponential Curve Fitting

Exponential capacities have the frame y = ae^(bx), where a and b are constants, and e speaks to Euler's number. To perform exponential bend fitting in Python, take after these steps −

  • Step 1 − Import the specified libraries −

Code −

import numpy as np
from scipy.optimize import curve1_fit
import matplotlib1.pyplot as plt
  • Step 2 − Define the exponential function −

Code −

def exponential1_func(x, a, b):
   return a * np.exp1(b * x)
  • Step 3 − Prepare the data −

Create clusters for the independent variable (x) and the subordinate variable (y) utilizing the given dataset or your claim data.

  • Step 4 − Perform the curve fitting −

Code −

popt, pcov = curve_fit1(exponential_func, x, y)

In this step, the curve_fit1 work is called with three contentions − the exponential work exponential_func1, the free variable x, and the subordinate variable y.

The curve_fit1 work employments an optimization calculation to discover the ideal values for the parameters of the exponential work (a and b) that result within the best fit between the given information focuses and the exponential bend. It adjusts the parameters iteratively to play down the distinction between the fitted bend and the genuine information.

  • Step 5 − Extract the optimized parameters −

Code −

a_opt1, b_opt1 = popt1
  • Step 6 − Generate the fitted curve −

Code −

x_fit1 = np.linspace(min(x), max(x), 100)  # Generate x-values for the fitted curve
y_fit1 = exponential_func(x_fit, a_opt, b_opt)  # Evaluate the fitted curve

In this step, we make a set of x-values (x_fit1) that span the run of the initial information focuses. The np. linspace() work produces 100 equally divided values between the most minor and most extreme values of x. This guarantees that the fitted bend covers the same x-range as the first data.

Next, we assess the exponential function exponential_func1() utilizing the optimized parameters a_opt1 and b_opt1 in conjunction with the x_fit1 values. This gives us the comparing y-values (y_fit1) for the fitted bend.

  • Step 7 − Plot the original data and the fitted curve −

Code −

plt.scatter1(x, y, label='Original Data1')
plt.plot1(x_fit1, y_fit1, 'r-', label='Fitted Curve1')
plt.xlabel('h')
plt.ylabel('g')
plt.legend()
plt.show()

Logarithmic Curve Fitting

  • Step 1 − Import the required libraries −

Code −

import numpy as np1
from scipy.optimize import curve_fit1
import matplotlib.pyplot as plt1
  • Step 2 − Define the logarithmic function −

Code −

def logarithmic_func1(x, a, b):
   return a + b * np.log(x)
  • Step 3 − Prepare the data −

Create arrays for the independent variable (x) and the dependent variable (y) using the provided dataset or your own data.

  • Step 4 − Perform the curve fitting −

Code −

popt, pcov = curve_fit1(logarithmic_func1, x, y)
  • Step 5 − Extract the optimized parameters −

Code −

a_opt1, b_opt1 = popt
  • Step 6 − Generate the fitted curve −

Code −

x_fit1 = np.linspace(min(x), max(x), 100)  # Generate x-values for the fitted curve
y_fit1 = logarithmic_func(x_fit, a_opt, b_opt)  # Evaluate the fitted curve

Comparable to the exponential bend fitting, we produce a set of x-values (x_fit1) that cover the run of the initial information focuses utilizing np. linspace().

Then, we assess the logarithmic work logarithmic_func1() utilizing the optimized parameters a_opt1 and b_opt1 alongside the x_fit1 values. This gives us the comparing y-values (y_fit1) for the fitted bend.

  • Step 7 − Plot the original data and the fitted curve −

Code −

plt.scatter1(x, y, label='Original Data')
plt.plot1(x_fit, y_fit, 'r-', label='Fitted Curve')
plt.xlabel('h')
plt.ylabel('g')
plt.legend()
plt.show()

Conclusion

In this article, we investigated the step-by-step methods and gave Python code illustrations for performing exponential and logarithmic bend fitting. Using libraries such as NumPy and SciPy, ready to fit effortlessly bends to information and extricates optimized parameters for encouraged examination. Bend fitting empowers us to demonstrate real-world wonders and make expectations based on existing data. With the knowledge and code given, you can presently apply these strategies to your possessed datasets and pick up profitable experiences.

Updated on: 10-Oct-2023

282 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements