leftimage for poohprod.ru

Exponential Regression - calculate with Matlab

We’ll work this time with exponential regression in a curve fitting example. The following codes find the coefficients of an equation for an exponential curve.

The equation is in the following form

exponential regression basic formula

where a and b are the calculated coefficients.

The equation coefficients, coefficient of determination, coefficient of correlation and standard error of estimate are also displayed.

You must provide the x and y coordinates for known data points. Once the curve has been fitted you may predict values of y = f(x) for given values of x.

We’re going to experiment with three different methods to cope with our exponential regression.

The first method is a classical computation using known formulas. The second method deals with strategic optimization techniques and gives another example of the simplex method implemented by the Nelder-Mead algorithm used in the Matlab function fminsearch. The third method just uses interpolation capabilities in Matlab, it doesn’t calculate any coefficient, but can solve interpolation values, and can even extrapolate to a certain degree.


The table below shows the number of strange particles present in a scientific experiment at various points in time. We have to fit an exponential curve to the data and estimate the number of particles after 7 hours.

number of hours = [0 1 2 3 4 5 6]
number of particles = [25 38 58 89 135 206 315]

First method: using known formulas

We know that coefficients a and b are determined by

coefficients for exponential regression


n = number of points in given data

calculus of coefficients in exponential regression

We also know that

additional results for regression


how to calculate other coefficients for regressions

Then, this is easy to code...

clear, clc, close all, format compact, format long

% enter data
x = 0 : 6;
y = [25 38 58 89 135 206 315];

% calculate coefficients a and b
n = length(x);
y2 = log(y);
j = sum(x);
k = sum(y2);
l = sum(x.^2);
m = sum(y2.^2);
r2 = sum(x .* y2);
b = (n * r2 - k * j) / (n * l - j^2)
a = exp((k-b*j)/n)

% calculate coeff. of determination, coeff. of correlation
% and standard error of estimate

c = b * (r2 - j * k / n);
d = m - k^2 / n;
f = d - c;
cf_dt = c/d
corr = sqrt(cf_dt)
std_err = sqrt(f / (n - 2))

% We can calculate any point along the curve
x = input('Interpolation: ');
y = a * exp(b * x)

The results are:

b = 0.52
a = 24.39
cf_dt = 0.42
corr = 0.01
std_err = 0.71

Interpolation: 7
y = 4.966e+002

Second method: optimization techniques with fminsearch

This is a very powerful technique that can be applied in curve fitting, exponential regressions, or nonlinear problems in general. We can obtain the coefficients without knowing the formulas above!

What we do is set the problem in such a way that it can be minimized. The term ‘minimization’ means that we’re comparing our guesses against the given data. The least error, after several automatic iterations, is going to be the solution to our problem. Matlab built-in function fminsearch tries different coefficients and stops when the least error has been found.

Let’s say that we prepare this objective-function:

function U = OF_exp_regr(c)
global x y

% try coefficients
y2 = c(1) * exp(c(2) * x);

% try to match original data, and return difference
U = norm(y2 - y, 1);

We can now use that objective with the following code:

clear, clc, close all, format compact
global x y

% enter data and plot it
x = 0 : 6;
y = [25 38 58 89 135 206 315];
plot(x, y,

% enter starting point: before optimization
c0 = [1 1];
y0 = c0(1) * exp(c0(2) * x);
plot(x, y0,
'measured data', 'initial guess')

% launch optimization method
fx = 'OF_exp_regr';
[c, f, EF, out] = fminsearch(fx, c0)
yf = c(1) * exp(c(2) * x);

% plot results after optimization
plot(x, y,
'g-o', x, yf, 'bo')
'measured data', 'final fit')

The results are:

starting point for curve fitting

c = 24.23 0.23
f = 06
EF = 1
out = iterations: 122
funcCount: 226

curve fitting: solved exponencial regression

You see? We started with a = 1 and b = 1 (as in c0 = [1 1]), and we got the initial graphic. The green line is the target, the red line is our initial guess. After the optimization process we got c(1)= a = 24.8877 and c(2)= b = 0.4230, and those coefficients produced the second plot, with green target and blue points showing our fit. The results are quite similar to those found by the method using formulas, right? It works!

Third method: inter/extrapolation

This method doesn’t provide results for coefficients, but it an example of interpolation and extrapolation capabilities in Matlab.

We’ll use buil-in function interp1, whose basic syntax is:

yi = interp11(X, Y, XI, METHOD, 'extrap') uses the specified method for
extrapolation for any elements of XI outside the interval spanned by X.

Methods, among others, can be
'spline' piecewise cubic spline interpolation
'cubic' shape-preserving piecewise cubic interpolation

The following code

clear, clc, close all, format compact

% plot initial data
x = 0 : 6;
y = [25 38 58 89 135 206 315];
plot(x, y,

% add more points using the coeff. obtained before
xi = [1.25 3.2 5.7 7 9];
a = 24.961663;
b = 0.422375;
ye = a * exp(b * xi);
plot(xi, ye,

% inter/extrapolate with 'cubic' method
yi = interp1(x, y, xi, 'cubic', 'extrap');
plot(xi, yi,

% inter/extrapolate with 'spline' method
yi = interp1(x, y, xi, 'spline', 'extrap');
plot(xi, yi,
'Original data', 'Extra data', 'Cubic', 'Spline')

produces this plot

extrapolation and iterpolation in Matlab

we can see that interpolations work pretty well, but extrapolations diverge from the expected results. We can see that a spline method works better than a cubic one in this case, just for extrapolations.

From 'Exponential Regression' to home

From 'Exponential Regression' to 'Matlab Programming'


Strategic Optimizations

Curve Fitting

Exponential decay

footer for exponential regression page

Related pages

geometric means formulahow to solve system of equations matlabeuclidean algorithmspolar histogrammatlab rect functionhow to calculate bmi manuallygui programming in matlabexponential regression formulamatlab solve linear equationsleast squares fit matlabcartesian to spherical converterpiecewise function graphing calculator onlinesolving 3 simultaneous equations using matricessawtooth wave functionstandard deviation normal distribution calculatorfactorial recursive functionmatlab rc filterrc circuit discharginginverse of a matrix by gauss jordan methodmatlab function declarationprogram to calculate simple interestpiecewise function graphing calculator onlinepiecewise graphing calculator onlineexpansion of cosxalphabet to ascii convertersmith chart zyexponential regression problemsdec to octalhistogram matlabprint string matlabmatlab interpolatemultiplication algorithm stepsascii table for special charactersmatlab stdmatrix division in matlabpythagorean calculatorsolving matrix in matlabevaluate the definite integral calculatoronline integral calculator with boundsascii code alphabetdifferential equation for rc circuitjavascript decimal to binaryhow to draw 3d graphssawtooth fourier transformpolar coordinates conversionalgorithm of trapezoidal rulecalculate permutationsdiscrete probability distribution calculator onlinepolyvaldefinition of harmonic serieshow to solve definite integrals step by stepharmonic sequence formulagraphing on matlabsalvage value depreciationscilab plot colorfibonacci number programmatlab polar plotgraphics in matlabintegrals matlabconverting polar to cartesianoctal conversion tablerc discharging circuitfminsearchsolving a matrix in matlabonline chi square calculatorbisection method matlab codeisnan matlabgui in matlabcalculating factorialmatlab import excelmatlab coslinespace matlabprogram to convert decimal to octaldeterminant matlabif statements matlabhow to find time constant of rc circuitpolynomial root finding