next up previous
Next: Difference Equations & Filters Up: Data Analysis Previous: More Statistical Functions -


Curve Fitting (Regression)

I will show two examples of fitting a curve for a given set of data points - Polynomian Regression and Linear in the Parameter Regression. A discussion follows the source code snippet.

% Hints: use ; at end of line to prevent debug'ish output from being printed

% Initial plot of just the data points
t = [0 .3 .8 1.1 1.6 2.3]';
y = [0.5 0.82 1.14 1.25 1.35 1.40]';
plot(t,y,'o'), grid on

X = [ones(size(t))  t  t.^2]
a = X\y

% Fit curve of the data points 
T = (0:0.1:2.5)';
Y = [ones(size(T))  T  T.^2]*a
plot(T,Y,'-',t,y,'o'), grid on

%% Increase degree of polynomial to increase accuracy of fit OR use linear in parameter regression (below)

X = [ones(size(t))  exp(-t)  t.*exp(-t)]
a = X\y
T = (0:0.1:2.5)';
Y = [ones(size(T))  exp(-T)  T.*exp(-T)]*a
plot(T,Y,'-',t,y,'o'), grid on

In the polynomian regression code snippet, we've tried to fit an equation of the form:

\begin{displaymath}y = a_0 + a_1t + a_2t^2\end{displaymath}

$a_0$, $a_1$ and $a_2$ are found using the backslash operator. It is evident that we do not get that good a fit, curve misses most of the data points by a considerable margin. The accuracy can be increased by using a higher order polynomial. Or the next method could be used...

In the second example shown, instead of a polynomial function, I've used a function that is linear-in-the-parameters - an exponential function of the form:

\begin{displaymath}y = a_0 + a_1 \exp{-t} + a_2t\exp{-t}\end{displaymath}

Again $a_0$, $a_1$ and $a_2$ are found using the backslash operator. But we get a much better fit with this method. Refer the Matlab documentation [1] for more information on curve fitting, they have some nice examples!

This is the output you can expect to see when you run the above shown .m file.

X =

    1.0000         0         0
    1.0000    0.3000    0.0900
    1.0000    0.8000    0.6400
    1.0000    1.1000    1.2100
    1.0000    1.6000    2.5600
    1.0000    2.3000    5.2900

a =

    0.5318
    0.9191
   -0.2387

Y =

    0.5318
    0.6213
    0.7060
    0.7860
    0.8612
    0.9316
    0.9973
    1.0582
    1.1143
    1.1656
    1.2121
    1.2539
    1.2909
    1.3231
    1.3506
    1.3733
    1.3912
    1.4043
    1.4127
    1.4163
    1.4151
    1.4091
    1.3984
    1.3829
    1.3626
    1.3375

X =

    1.0000    1.0000         0
    1.0000    0.7408    0.2222
    1.0000    0.4493    0.3595
    1.0000    0.3329    0.3662
    1.0000    0.2019    0.3230
    1.0000    0.1003    0.2306

a =

    1.3974
   -0.8988
    0.4097

Y =
    0.4986
    0.6212
    0.7286
    0.8226
    0.9047
    0.9764
    1.0390
    1.0934
    1.1408
    1.1818
    1.2174
    1.2482
    1.2747
    1.2976
    1.3172
    1.3339
    1.3482
    1.3604
    1.3707
    1.3793
    1.3866
    1.3926
    1.3976
    1.4017
    1.4050
    1.4077
Figure 3: Plot of data points
\begin{figure}\epsfig{file=src/curve_fit1.eps, width=13cm}
\end{figure}
Figure 4: Case 1: Polynomial Regression (Quite Inaccurate)
\begin{figure}\epsfig{file=src/curve_fit2.eps, width=13cm}
\end{figure}

Figure 5: Case 2: Linear in Parameters Regression (More Accurate)
\begin{figure}\epsfig{file=src/curve_fit3.eps, width=13cm}
\end{figure}


next up previous
Next: Difference Equations & Filters Up: Data Analysis Previous: More Statistical Functions -
Arvind Gopu 2006-03-24