One of my preferred task I like to see solved by the powerful computational skills of calculators is the **curve fitting**: we have some measured values and we want to find a function which describe the trend of this values against time. The key question is: what kind of function to optimize should we choose? I will call it **model function** and this will be given to the Matlab/Octave script in order to be fitted to our measured values.

Before going on: about linearisation

If our model function is non-linear we would maybe first linearise it, because some Matlab functions are working better with linear model functions. I take a practical example of linearisation from a project done at the University:

Here the *x* values are the coefficients to be set and the *t* are the measured values converted after the linearisation.

Now we have a lot of solutions. The most easy way is to use the *fitlm* function, which will do all the job, but while this will work perfectly with Matlab, I could not make it work on Gnu Octave:

1 2 3 4 5 |
%I already defined the matrix t, whereby each row refers to a different measurement and each column comes from a different measured value (variables of our function) tbl = table(t(:,1),t(:,2),t(:,3),t(:,4),t(:,5),y,'VariableNames', 't1','t2','t3','t4','t5','y'});%we create a table with all t values where we also give them a names mdl = fitlm(data,'y~1+t4+t1+t4*t1+t3*t1+t2+t4*t2+t5*t2+t3+t5');%Note: the linear equation has to be written with x-coefficients equal 1 ! coeffs = mdl.Coefficients.Estimate';%now 'coeffs' contains all x parameters of our linear model equation |

Note: don’t forget to convert back the parameters found by your Matlab script!

General method in details

Let’s discuss now some other way to find the fitting curve by using a general method which should work also with Gnu Octave. The following code was written with GnuÂ Octave 3.8.1. For simplicity I will not consider here a multi-variable function as before, but just a set of values as function of the time.

Example:

1 2 3 |
y=[1 2 10 25 0.5 8 80 -2 30 12 17 18 19 30 21 50 25 0 -25 -50]; t=linspace(1,5,20); scatter(y,t); |

The scatter function will let us visualize this set of fictitious points:

Now the question is about which **criterion** we want to apply for the curve fitting starting from a model equation: **what do we want to minimize?** In this case I chose to minimize the distance between the model function and the “y” of every point. The Matlab/Octave function used for the minimization is *fminsearch. *Since it depends on some given initial values for the x-parameters (because it use the Nelder & Mead Simplex algorithm which is a derivative-free method) I just used the *for* loop in order to repeat the computation with different random initial values and at the end I just selected the set of parameters which fit better to my criterion.

Here is the full code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
y=[1 2 10 25 0.5 8 80 -2 30 12 17 18 19 30 21 50 25 0 -25 -50]; t=linspace(1,5,20); scatter(t,y); hold on; %we define our model function by summing up 3 sine's functions %we have here 6 x-paramenters to be set properly for the curve fitting f=@(t,x) x(1)*sin(x(2)*t+x(3))+x(4)*sin(x(5)*t+x(6))+x(7)*sin(x(8)*t+x(9)); %we repeat the computation 30 times with different random initial values for the x-parameters fdists=[];%here we store the computed minimisations of the sum of distances between y and f(t,x) xmins=[];%here we store the computed x-parameters according to our criterium for i=1:10 for j=0:3 %in the following line 'rand(1,9)*10*j' ist just a random the set of initial values [xmin, fdist]=fminsearch(@(x) sum(abs(y-f(t,x))), rand(1,9)*10*j); fdists=vertcat(fdists,fdist); xmins=vertcat(xmins,xmin); end end %from all the repeated computations we choose the best one [fdistmin fdistminindex]=min(fdists); bestf=f(t,xmins(fdistminindex,:)); plot(t,bestf);%here we can compare the plot with the original points scatter |

This is the result:

Now it’s up to you to play with the choice of the model function and its x-parameters to use for the curve fitting optimization.

## Leave a Reply