1
votes

I have 5 matrices with 4 cells each. I need to fit each cell using a function in GNUplot. So, for eg. y data corresponding to the first cell will be the value of the first cell in the 5 matrices. How should I arrange this data in a file such that GNUplot automatically identifies the x and y data and fits for each cell, giving the fitted parameters as a matrix in the same order as the cells? Also, how would a script for such a fitting be written?

An example is as follows:

Matrix 1: 2 4
          3 6
Matrix 2: 5 7
          9 1

Like this 5 matrices exist Now the first set of y data would be 2,5,...; the second set would be 4,7,...; third set would be 3,9,...; and fourth set 6,1,... Suppose my x values are 0,1,2,4,8. Now I fit the first set of y values along with these x values to some function f(x) and get a parameter value of 4. Similarly for the other three sets I get 7,9 and 6 as the parameter value. So I would want the result to look like:

Result: 4 7
        9 6
1
Your question is not clear to me. Do you mean 5 2x2 matrices? How does your y data look like? What do you want to fit? Please show a minimal, simplified example... with some data and functions. And then maybe we can make suggestions how to arrange the data differently. - theozh
@theozh I have edited the question to show a simple example - Shakti
Still not clear. Do I understand correctly that you have four data sets, call them A B C D, each of which contains five "y" values. You want to fit a function f(a,x) = y for each of them. For some reason you plan to print the resulting estimates for "a", call them a(A), a(B), a(C), a(D) in a square, but that is not relevant to the calculation. Where is the program supposed to find the x values for this fit? - Ethan

1 Answers

2
votes

Well, it's still difficult to understand. But nevertheless, let me try and let's hope that we are getting closer to that what you want.

For illustration purpose and for an easier understanding, the values in the example matrices below are chosen as follows: <MatrixNo><RowNo><ColNo>. After your comments it's now 3x2 matrices.

Data:

# Matrix1
111  112
121  122
131  132

# Matrix2
211  212
221  222
231  232

# Matrix3
311  312
321  322
331  332

# Matrix4
411  412
421  422
431  432

# Matrix5
511  512
521  522
531  532

I guess the above data would probably be better organized in the way as it is in the example code below. Of course, you can reformat the data with an external tool or even with gnuplot. Then, you are doing fits within a loop. In order to remember the fit parameter values, I used arrays (I guess requires gnuplot 5.0 or 5.2). In gnuplot console e.g. type help fit or any other keyword to get more information.

Code: (Edited for I x J matrices)

### fits in a loop
reset session

$Data <<EOD
0  111  112  121  122  131  132
1  211  212  221  222  231  232
2  311  312  321  322  331  332
4  411  412  421  422  431  432
8  511  512  521  522  531  532
EOD

M = 5    # number of matrices
I = 3    # number of rows of each matrix
J = 2    # number of columns of each matrix
N = I*J  # number of y values

array aList[N]
array bList[N]

# fitting in a loop
f(x,a,b) = a*sqrt(x) + b
set fit nolog quiet
do for [i=1:N] {
    fit f(x,a,b) $Data u 1:(column(i+1)) via a,b
    aList[i] = a
    bList[i] = b
}

# prepare the matrix output 
do for [i=1:I] {
    line = ''
    do for [j=1:J] {
        line = line.sprintf("%g ",bList[(i-1)*J+j])
    }
    print line
}

set key bottom right
plot for [i=1:N] $Data u 1:(column(i+1)) w p ti sprintf("Values %d",i), \
     for [i=1:N] f(x,aList[i],bList[i]) w l ti sprintf("Fit %d",i)

### end of code

Result:

97.1388 98.1388 
107.139 108.139 
117.139 118.139 

Plot:

enter image description here