# Best way to approximate a curve?

I have a curve like this one:

I need a function to approximate this curve, but I need that the function be a low order function (less than 5). What is a good way to obtain what I expect? Thanks!

• How low is low order? – CMDoolittle Aug 6 '15 at 21:44
• I would like to find an approximation of an order less than 5 – Alexander Leon VI Aug 6 '15 at 21:46
• you're gonna first choose what form of approximation. if "order" means about the same thing it would mean with polynomials, you have as many degrees of freedom (knobs you can twist) as is the order of the approximating function. perhaps one more (a 5th-order polynomial has 6 coefficients). then you have to choose how you define how well your approximating function does in approximating your data. this is what "Least-Squares" ($L^2$ norm) or "Minimax" ($L^\infty$ norm) or other norms are about. so what's important to you? that your total square error is minimum? – robert bristow-johnson Aug 6 '15 at 22:37
• No really, actually I am looking for fast computation – Alexander Leon VI Aug 6 '15 at 23:05
• It reminds me of a chi-squared distribution, except with the axis flipped. – curiousStudent Aug 7 '15 at 2:49

So, you have a bunch of datapoints of the form (x,y), and considering all of those datapoints together, you have the vectors $x$ and $y$.

To do a curve fit, you would like to solve the equation $Aw=y$ for the column vector $w$, which holds the coefficients of your curve fit polynomial. These coefficients are also the weights of the basis vectors in the matrix $A$.

The matrix $A$, for an $n^{th}$ order approximation, will hold $n+1$ columns and have a number of rows equal to the length of $x$ and/or $y$ (the number of datapoints).

The first column of $A$ is the vector $x$ raised element-wise to the power of 0. So, it's just a vector of 1's. This is the basis associated with the offset or y-intercept. The second column of $A$ is the vector $x$ raised element-wise to the power of 1. So, it's just the vector $x$. The third column of $A$ is the vector $x$ raised element-wise to the power of 2. You see the pattern?

To solve for the weight vector, $w=pinv(A)y$, the pseudoinverse of $A$ times $y$. Note, this doesn't actually solve the equation $Aw=y$, but performs an optimal projection of your data onto the vector space described by $A$.

EDIT: $pinv(A)=(A^TA)^{-1}A^T$

• Ok, this was a nice sollution!, It could work well! – Alexander Leon VI Aug 6 '15 at 22:42
• Now need an algorithm for calculate the pseudoinverse :( – Alexander Leon VI Aug 6 '15 at 22:47
• The pseudoinverse of $A$ is equal to $(A^TA)^{-1}A^T$ – CMDoolittle Aug 6 '15 at 22:48
• What is the difference between your way and curve fitting polyfit() or cubic spline? – gmotree Aug 7 '15 at 2:22
• @Alexander De Leon VI :this way have a bunch of discountinue point. It means that no smoothness. – gmotree Aug 7 '15 at 2:37