# How to fit a curve to my data

I have a datasheet. It looks like an hyperbola. How can I fit a curve to it? And how can I plot a curve of the first derivative?

Time (x)second  Normalized data (y)

0            100.00%
100            102.60%
200            104.39%
300            105.53%
400            106.80%
500            107.49%
1000            113.03%
1500            113.13%
2000            113.30%
2500            113.63%
3000            113.78%
3500            113.92%
4000            113.92%

-
You use regression methods. But first, where did your data come from? – J. M. Dec 14 '11 at 0:34
If I plot the data it looks more like two straight lines, one from 0-1000 sec, and one from 1000-4000 sec. Hyperbolas don't have that sharp a corner. – Ross Millikan Dec 14 '11 at 5:03

You can use least-squares fitting if you can express the curve that you want to fit as $$y = \sum_{i=1}^{n} a_i f_i(x),$$ where $a_i$'s are the unknowns that you would like to estimate from your data and $f_i$'s are known functions of $x$.

Let $Y = \begin{bmatrix}y_1 \\ \vdots \\ y_m\end{bmatrix}$, $A = \begin{bmatrix} a_1 \\ \vdots \\a_n\end{bmatrix}$, and $F = \begin{bmatrix} f_1(x_1) \ldots f_n(x_1) \\ f_1(x_2) \ldots f_n(x_2) \\ \ldots \\ f_1(x_m) \ldots f_n(x_m) \end{bmatrix}$, then the least squares solution is given by "inverting" $Y = F\cdot A$ to obtain an estimate $\hat{A}$ of $A$: $$\hat{A} = (F^{T}F)^{-1} F^{T} Y$$

The first derivative is simply given by $$\dfrac{dy}{dx} = \sum_{i=1}^{n} \hat{a}_i \dfrac{df_i(x)}{dx}$$

-
This is assuming that the model function is linear in its parameters. If it's nonlinear, then this doesn't apply at once. Without knowing where the data comes from, we can never be sure... – J. M. Dec 14 '11 at 3:50
Also, the normal equations aren't often the best way to solve least-squares problems... – J. M. Dec 14 '11 at 5:01

For any functional form, you can do a multidimensional minimization over the parameters of the form you choose. Any numerical analysis book will have a description. I like sections 10.4 through 10.7 of Numerical Recipes (obsolete versions are free). Then for the derivative you can take the derivative of the form you find. But it may not be as accurate as the function values-differentiation amplifies high frequency noise.

-

I wrote a tutorial article on the topic:

Linear and Non-Linear Least Squares with Math.NET

Of course, you need to know the type of curve beforehand. That is the model function with parameters you would like to fit, e.g.

$$y = ax^{2}+b$$

Of course, you can interpolate the data with some piecewise polynomial or spline.

-

If you have access to Excel, use the "Trend Line" function to do the fitting. You can choose several types of functions to use for fitting. No sense writing fitting algorithms yourself unless your objective is to learn how. I agree with Ross M's comment above -- the data looks like two straight lines, so fitting any kind of curve to it is going to be tricky. But, it depends what characteristics you want in the fit. Using Excel will let you play around with several different options easily, and you might be able to find a fit that you like. If you do, then Excel will give you the equation of the curve, and then you can easily get the derivative.

-