COMSOL Version 6.1 Least-Squares Fit Function

Technology continues to evolve, making our lives a lot easier in many ways. And, when it comes to Multiphysics, the Least-Squares Fit function of COMSOL 6.1 is no exception. Before this function existed, engineers had to develop their own coding to perform this type of fitting. But, in our latest Tips & Tricks video, we show you how this feature of COMSOL 6.1 allows you to easily perform this operation.

To help you follow along, we have included the video transcription below.

AltaSim’s upcoming training offerings will support you, the COMSOL user, in utilizing best practices with many of the COMSOL modules. Please click here to visit our training calendar and find the course that is best for you.

COMSOL 6.1 Least-Squares Fit Function Transcription:

In this video we briefly demonstrate a new function which is available in COMSOL 6.1 version. And the name of this function is Least-Squares Fit function. You can add this function under Global Definitions or under Component Definitions. This function can be used for preprocessing or postprocessing. And the input for this function can be text file, local table, or result table. Now let’s see how it works. Let’s assume you have some model, you have results data, and we create 1D Plot with some parameter along the X-axis and function along the Y-axis.  What we want to do is to approximate this set of data points by continuous function.  The first step would be to export these data points to the text file or to the result table. Let’s use option to export these data points to the text file.  So, we right-click Global Plot node, select Add Plot Data to Export, then click Browse button, define file name, and then click Export button.  Now, to add the Least-Squares Fit function node right-click Global Definitions/Functions node and select Least-Squares Fit. Then go to the Load From File, and select data text file we just created. This is the input data. Click Plot button to plot these data points. And now we want to interpolate or approximate these data points by some curve, say polynomial curve.  The first thing to do is to define name of the argument of this curve, say X, and then define function itself.  Let’s start with the quadratic polynomial.  So, expression would be A0 plus A1 times X, plus A2 times X squared. We have three interpolation parameters A0, A1, and A2. On the Parameters section, type the names of these parameters.  Black color indicates that everything is defined correctly.  And now we are ready to calculate the values of the parameters A0, A1, and A2.  To do that, click Fit Parameters button to perform calculations. The output of this calculations are values of these three interpolation parameters.  And now you can plot interpolation polynomial using calculated parameters.  Click Plot button to visualize a second order interpolation polynomial.  You see that approximation of these data points is not good. So, what we might want to do we might use another polynomial, say third order polynomial, to do a better job for approximation.  Add another term, plus A3 times X in power 3.  And we add this A3 parameters to the parameters list.  Now again, expression for the function color changed to black meaning that everything is defined, and we are ready to compute.  Click Fit Parameters button and after a second calculation is completed, parameters are defined, and now we are ready to plot this new interpolation function.  Click Plot button. We can see that third order polynomial approximation of the input data points is not so bad given the huge scattering of the data points. We were using polynomial interpolation; however, it turns out that you can use any other function. As an example, let’s use a sine function for the least-squares fit.  Let’s add another Least-squares Fit node. Right-click Global Definitions and select Functions/Least-Square Fit. Click Load from File to load input data points. Now you need to define the function.  Let’s use sine function, so we type A times sine function with argument X, and plus B. We have two fitting parameters A, and B. Color is orange, meaning that something is not right. Argument should be X not X1. After correction, color is black, and we are ready to continue.  Click Fit Parameters button. After calculation is done, parameters A and B are evaluated, and now we can plot this new approximation function. Click Plot button and again you see that this sine function does quite a good job for approximation of the input data.  Before 6.1 release, you would need to develop your own coding to perform this fitting. Now this operation can be conveniently performed using built-in Least-Squares Fit function.  Thank you.