I have a curve like this one:enter image description here

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!

  • $\begingroup$ How low is low order? $\endgroup$ Aug 6, 2015 at 21:44
  • $\begingroup$ I would like to find an approximation of an order less than 5 $\endgroup$ Aug 6, 2015 at 21:46
  • $\begingroup$ 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? $\endgroup$ Aug 6, 2015 at 22:37
  • $\begingroup$ No really, actually I am looking for fast computation $\endgroup$ Aug 6, 2015 at 23:05
  • $\begingroup$ It reminds me of a chi-squared distribution, except with the axis flipped. $\endgroup$ Aug 7, 2015 at 2:49

1 Answer 1


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$

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.