Tuesday, June 7, 2016

Maxima - Interpolation

In science, you often have to made analysis of a set of data, a huge set of data. There are many analysis you can do with them, but an interesting thing you can do with an application like Maxima, is the interpolation to know the value of intermediary or external values of the given set. In this article, we'll see four methods: the lineal interpolation (with the command linearinterpol()); the spline interpolation (with the command cspline()); the Lagrange interpolation (with the command lagrange()); and the least squares (with the command lsquares_estimates()). As said before, I'm not going to explain the mathematical methods, but only how to use them in an application like Maxima.

The three interpolation methods (the lineal, spline and Lagrange), can help you a lot when you desire is getting a value in the middle of the set of data. But when you're trying to find a value outside the limits of your set, you'll have a more accurate result with the lest square method (depending on the variation of the function near the limits of our set). The interpolation methods need a specific package to be imported before use them:
After that, the three functions are available for use. Both three methods, only have a single input argument, the set of data, and as an output, you will have a function which defines the interpolation. Let's see an example with the three usages:
As you see the usage is quite simple. Let's see the result:

For this particular example (with so few points), we see that both the three methods converge quite well to the function (which was similar to 2*sin(x)).

Let's now see the fourth method, the least squares method. This function also requires a particular package to be imported (load(lsquares)$). The usage of the least squares method is different from the others. You must have an idea of the curve your set represents, with some unknown values. The inputs for this functions are different from the others. First, it needs a matrix, not a list of value. The matrix will have the values for each variable per row. Second, we specify which variable corresponds to each row of the matrix. Third, the model which represents (approximately) our function with the unknown values. And fourth, the list of the unknown values. Let's see the previous example with this method:



In the first line, we load the required package to use the function lsquares_estimates(). The second line, show our data, and the next how we insert it in a matrix with another row with only indexes starting at 0. Then we transpose the matrix to have the values per row as a set of key-value. We then use the lsquares_estimates() and stores its output. The output are the values of the given unknown constants A, B and C, which we make the substitution on our approximate model, and finally plot the result.


No comments:

Post a Comment