Skip to main content
Engineering LibreTexts

12.3: Optimization - Parameter Estimation for Nonlinear Functions

  • Page ID
    122646
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    Introduction to Optimization: Parameter Estimation for Nonlinear Functions

    Welcome! This document will introduce you to the fascinating and incredibly useful field of optimization, specifically focusing on its application in parameter estimation for nonlinear functions. Whether you're analyzing scientific data, designing engineering systems, or building predictive models, understanding these concepts is fundamental to extracting meaningful insights and creating efficient solutions.

    What is Optimization?

    At its core, optimization is the process of finding the "best" solution from a set of available alternatives. In a mathematical context, it typically involves either:

    • Minimizing an objective function (e.g., minimizing cost, error, or weight).
    • Maximizing an objective function (e.g., maximizing profit, efficiency, or performance).

    This "best" solution is found by systematically adjusting a set of "decision variables" while adhering to various "constraints" that define the boundaries of what is feasible or acceptable.

    What is Parameter Estimation?

    Parameter estimation is the process of determining the values of parameters within a mathematical model that best describe a given set of observed data. Imagine you have a set of measurements, and you believe there's an underlying mathematical relationship connecting your independent and dependent variables. Parameter estimation helps you find the specific numbers (the parameters) that make your model's predictions align as closely as possible with your actual observations.

    Nonlinear optimization is a fundamental tool across many engineering disciplines due to the inherent nonlinearities in real-world systems. Here are several prominent examples:

    1. Aerospace Engineering:
      • Aircraft and Rocket Trajectory Optimization: Calculating the most fuel-efficient flight paths for aircraft or optimal trajectories for rockets and satellites to minimize fuel consumption or maximize payload, often considering complex aerodynamic forces, gravity, and thrust, which are highly nonlinear.
      • Aircraft Wing Design: Optimizing the shape of an aircraft wing (airfoil) to maximize lift-to-drag ratio at various flight conditions, which involves nonlinear fluid dynamics equations.
      • Satellite Constellation Design: Determining the optimal number, orbits, and phasing of satellites to provide continuous coverage over a specific area with minimal cost.
    2. Mechanical Engineering:
      • Robotics: Optimizing robot arm movements for speed, energy efficiency, or precision, considering the complex kinematics and dynamics of multi-jointed systems.
      • Engine Design: Optimizing engine parameters (e.g., fuel injection timing, valve lift) for maximum power output, fuel efficiency, or minimal emissions, where combustion processes and fluid flows are highly nonlinear.
      • Vibration Control: Designing passive or active vibration damping systems to minimize unwanted oscillations in structures or machinery.
      • Mechanism Design: Optimizing the dimensions of linkages and gears in mechanical systems to achieve desired motion profiles or force transmission characteristics.
    3. Chemical Engineering:
      • Process Optimization: Maximizing the yield of a chemical reaction, minimizing energy consumption in a separation process (like distillation), or optimizing reactor design parameters (temperature, pressure, catalyst concentration) to improve efficiency and product quality. Reaction kinetics are often nonlinear.
      • Supply Chain Optimization: Determining optimal production schedules, inventory levels, and transportation routes for chemical products to minimize costs and meet demand, often with nonlinear cost functions or capacity constraints.
    4. Electrical Engineering:
      • Antenna Design: Optimizing the geometry of antennas to achieve desired radiation patterns, gain, and impedance matching.
      • Circuit Design: Minimizing power consumption or maximizing signal integrity in complex electronic circuits, where component behaviors can be nonlinear.
      • Power Systems: Optimizing power flow in electrical grids to minimize transmission losses, ensure stability, and manage load distribution efficiently.
    5. Civil and Structural Engineering:
      • Bridge and Building Design (as discussed previously): Minimizing material weight while satisfying complex stress, deflection, and stability constraints under various loading conditions.
      • Foundation Design: Optimizing the dimensions and placement of foundations to minimize settlement and ensure structural stability given nonlinear soil-structure interaction.
      • Water Resources Management: Optimizing the operation of reservoir systems to meet water demand for irrigation, power generation, and flood control, considering nonlinear flow dynamics and demand patterns.
    6. Materials Science and Engineering:
      • Material Design: Optimizing the composition and microstructure of new materials to achieve desired properties (e.g., strength, conductivity, thermal resistance), often involving complex nonlinear relationships between composition and properties.
      • Manufacturing Process Optimization: Fine-tuning parameters in manufacturing processes (e.g., temperature profiles in heat treatment, pressure in molding) to minimize defects and maximize product quality.

    In all these examples, the objective function (what you want to minimize or maximize) and/or the constraints (the rules that must be followed) involve nonlinear relationships, making nonlinear optimization techniques indispensable.

    Why Nonlinear Functions?

    Many real-world phenomena exhibit relationships that cannot be accurately described by simple straight lines or linear combinations. In these cases, nonlinear functions are essential for capturing the true complexity of the system. A nonlinear function is simply one where the relationship between the independent variables and the dependent variable is not a straight line, and the parameters themselves might be part of exponents, products, or other complex terms.

    The Goal: Best Fit Parameters

    When we combine optimization with parameter estimation for nonlinear functions, our primary goal is to find the set of parameters for our chosen nonlinear model that provides the "best fit" to our observed data. This "best fit" is typically defined by minimizing the difference (or "error") between the values predicted by our model and the actual observed values.

    This process often involves an iterative approach, where an optimization algorithm systematically adjusts the parameters, evaluates how well the model fits the data (e.g., by calculating the sum of squared errors), and then continues to refine the parameters until the error is minimized to an acceptable degree. This allows us to derive a robust mathematical representation of the underlying process, enabling predictions, analysis, and informed decision-making.

    SciPy Optimize Curve_fit

    To find the coefficients for a nonlinear function given independent and dependent data in Python, you should use the scipy.optimize.curve_fit function.

    The scipy.optimize.curve_fit is a powerful function that performs nonlinear least squares to fit a user-defined function (your nonlinear model) to data. It works by minimizing the sum of the squared residuals, where a residual is the difference between the observed (dependent) data and the values predicted by your model.

    How it Works

    1. Define your nonlinear function: You need to express your nonlinear model as a Python function. This function will take the independent variable(s) as the first argument, followed by the parameters (coefficients) you want to fit.
    2. Provide your data: You'll supply your independent data (x-values) and dependent data (y-values) to curve_fit.
    3. Initial guess: curve_fit often benefits from an initial guess for the parameters. While optional, a good initial guess can help the optimization algorithm converge more quickly and avoid local minima, especially for complex nonlinear functions.
    4. Output: curve_fit returns two primary things:
    5. An array containing the optimal values for the parameters (coefficients) of your function.
      • popt: An array containing the optimal values for the parameters (coefficients) of your function.
      • pcov: The estimated covariance of popt. This matrix can be used to calculate the standard errors of your fitted parameters.

    Example Usage

    Example \(\PageIndex{1}\)

    Curve Fit 

    Let's say you want to fit an exponential function of the form \(y = e^{bx}\) to some data.

    import numpy as np
    from scipy.optimize import curve_fit
    import matplotlib.pyplot as plt
    
    # 1. Generate some sample data (for demonstration purposes)
    # In a real scenario, you'd load your actual independent and dependent data
    x_data = np.linspace(0, 4, 50)
    true_a = 2.5
    true_b = 1.3
    y_data = true_a * np.exp(true_b * x_data) + 10*np.random.normal(0, 0.5, x_data.size) # Added some noise
    
    # 2. Define your nonlinear function
    def exponential_function(x, a, b):
        return a * np.exp(b * x)
    
    # 3. Use curve_fit to find the coefficients
    # p0 is an optional initial guess for the parameters [a, b]
    initial_guess = [1.0, 1.0]
    popt, pcov = curve_fit(exponential_function, x_data, y_data, p0=initial_guess)
    
    # Extract the optimized coefficients
    a_fit, b_fit = popt
    
    print(f"Fitted 'a' coefficient: {a_fit:.4f}")
    print(f"Fitted 'b' coefficient: {b_fit:.4f}")
    print(f"Covariance matrix diagonal values: {np.diag(pcov)}")
    
    # 4. Plot the original data and the fitted curve
    plt.figure(figsize=(8, 6))
    plt.scatter(x_data, y_data, label='Original Data')
    plt.plot(x_data, exponential_function(x_data, a_fit, b_fit), color='red', label='Fitted Curve')
    plt.title('Nonlinear Curve Fitting with scipy.optimize.curve_fit')
    plt.xlabel('X-axis')
    plt.ylabel('Y-axis')
    plt.legend()
    plt.grid(True)
    plt.show()
    Fitted 'a' coefficient: 2.7119
    Fitted 'b' coefficient: 1.2775
    Covariance matrix diagonal values: [0.01276081 0.0001289 ]
    

    Considerations

    • Model Selection: The most critical step is choosing the correct nonlinear function (model) that best describes the relationship between your independent and dependent variables. Incorrect model selection will lead to poor fits.
    • Initial Guesses (p0): For some complex models, providing a reasonable p0 (initial guess for the parameters) is crucial for curve_fit to converge to the correct solution. If curve_fit struggles, try different initial guesses or visualize your data to estimate starting values.
    • Bounds: You can also specify bounds for your parameters in curve_fit if you know that certain coefficients must fall within a specific range. This can help guide the optimization.
    • Error Analysis: The pcov output allows you to estimate the uncertainties of your fitted parameters, which is important for understanding the reliability of your model. A covariance matrix with large diagonal terms is an indication of an incorrect model for the data or too many terms in the model.

    This page titled 12.3: Optimization - Parameter Estimation for Nonlinear Functions is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by Carl Greco.