2

I'm new to scilab (I'm using 5.5.2), and I need to make a non linear regression. I have a dataset of points which behaves like a sinewave, so I want to find the parameters of this sinewave.

Here is my dataset :

t1=[11800, 11805, 11810, 11817, 11824, 11829, 11834, 11839, 11844, 11849, 11854, 11859, 11866, 11871, 11878, 11883, 11888, 11893, 11898, 11903, 11908, 11915, 11920, 11928, 11933, 11938, 11943, 11948, 11953, 11958, 11965, 11970, 11975, 11980, 11987, 11992, 11997, 12002, 12007, 12014, 12019, 12024, 12029, 12037, 12042, 12047, 12052, 12057, 12063, 12069, 12074, 12079, 12084, 12091, 12096, 12101, 12106, 12111, 12119, 12123, 12128, 12133, 12138, 12146, 12151, 12156, 12161, 12169, 12174, 12179, 12184, 12188, 12193, 12201, 12206, 12211, 12218, 12223, 12228, 12233, 12238, 12243, 12251, 12256, 12260, 12268, 12273, 12278, 12283, 12288, 12292, 12297, 12302, 12310, 12317, 12322, 12327, 12332, 12337]


v1=[
0.36
0.59
0.81
0.92
0.90
0.76
0.54
0.31
0.17
0.19
0.36
0.59
0.81
0.92
0.90
0.76
0.54
0.31
0.17
0.19
0.36
0.59
0.81
0.92
0.90
0.77
0.54
0.31
0.17
0.19
0.35
0.59
0.81
0.92
0.90
0.77
0.55
0.32
0.18
0.19
0.35
0.59
0.80
0.92
0.90
0.77
0.55
0.32
0.17
0.19
0.35
0.59
0.80
0.92
0.90
0.79
0.55
0.32
0.18
0.18
0.35
0.59
0.80
0.92
0.92
0.79
0.57
0.32
0.18
0.18
0.35
0.58
0.80
0.92
0.92
0.79
0.57
0.32
0.18
0.18
0.35
0.58
0.80
0.92
0.92
0.79
0.57
0.34
0.18
0.18
0.34
0.58
0.80
0.92
0.92
0.80
0.57
0.34
0.18
]

In order to make the non linear regression I added the toolbox which contains the nlinregr function and called it like this :

fun='A*sin(W*t1+P)'
dfun='[sin(W*t1+P), A*t1*cos(W*t1+P), A*cos(W*t1+P)]'

[p, yhat,stat]=nlinregr([t1 v1], 't1 v1', fun, dfun,'A W P', 'v1')

With 'fun' the sinewave function I'm trying to fit, 'dfun' the matrix made of the analytical derivative depending on my parameters A, W and P.

While executing this function I'm getting the error "Incoherent Multiplication" but after 2 hours I'm still not able to point out where the problem is ....

Can someone help me please ?

2
  • I think that should be "'A * sin(W * t1 + P) + offset". Same derivative. Sep 22, 2018 at 13:19
  • Note that a sufficiently high frequency sine wave would be a good fit to almost any data set, so you might want to limit or constrain the maximum frequency. A scatterplot of the data can give you a good idea of this constraint. Sep 22, 2018 at 13:25

1 Answer 1

2

datafit is dedicated to such non-linear fitting. It's a native Scilab function. Code to perform the fitting and to show results:

t1 = [11800, 11805, 11810, 11817, 11824, 11829, 11834, 11839, 11844, 11849, 11854, 11859, 11866, 11871, 11878, 11883, 11888, 11893, 11898, 11903, 11908, 11915, 11920, 11928, 11933, 11938, 11943, 11948, 11953, 11958, 11965, 11970, 11975, 11980, 11987, 11992, 11997, 12002, 12007, 12014, 12019, 12024, 12029, 12037, 12042, 12047, 12052, 12057, 12063, 12069, 12074, 12079, 12084, 12091, 12096, 12101, 12106, 12111, 12119, 12123, 12128, 12133, 12138, 12146, 12151, 12156, 12161, 12169, 12174, 12179, 12184, 12188, 12193, 12201, 12206, 12211, 12218, 12223, 12228, 12233, 12238, 12243, 12251, 12256, 12260, 12268, 12273, 12278, 12283, 12288, 12292, 12297, 12302, 12310, 12317, 12322, 12327, 12332, 12337];

v1 = [0.36 0.59 0.81 0.92 0.9 0.76 0.54 0.31 0.17 0.19 0.36 0.59 0.81 0.92 0.9 0.76 0.54 0.31 0.17 0.19 0.36 0.59 0.81 0.92 0.9 0.77 0.54 0.31 0.17 0.19 0.35 0.59 0.81 0.92 0.9 0.77 0.55 0.32 0.18 0.19 0.35 0.59 0.8 0.92 0.9 0.77 0.55 0.32 0.17 0.19 0.35 0.59 0.8 0.92 0.9 0.79 0.55 0.32 0.18 0.18 0.35 0.59 0.8 0.92 0.92 0.79 0.57 0.32 0.18 0.18 0.35 0.58 0.8 0.92 0.92 0.79 0.57 0.32 0.18 0.18 0.35 0.58 0.8 0.92 0.92 0.79 0.57 0.34 0.18 0.18 0.34 0.58 0.8 0.92 0.92 0.8 0.57 0.34 0.18];

function g = gap(p, Data)
    // v = p(1) + p(2)*sin(p(3)*t+p(4))
    // p = [v_offset,  amplitude, angular_frequency, phase]
    t = Data(1,:)
    v = Data(2,:)
    g = v - (p(1) + p(2)*sin(p(3)*t+p(4)))
endfunction

p0 = [0.50 0.35 2*%pi/50 0.5];  // initial guess of fitting parameters
[p, dmin, status] = datafit(gap, [t1 ; v1], p0, "ar",200)

vfit = p(1) + p(2)*sin(p(3)*t1+p(4));
clf
subplot(2,1,1), plot(t1,v1), title "Given data" fontsize 3
subplot(2,1,2), plot(t1, vfit-v1), title "Fit - data" fontsize 3

Results:

--> [p, dmin, status] = datafit(gap, [t1 ; v1], p0, "ar",200)
 p  = 
   0.572807   0.3879044   0.114452   131.77806
    offset    amplitude   angular      phase
                          frequency

 dmin  = 
   0.0331676         average v distance between data and fit

 status  =
   9.                 means "OK"

enter image description here

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.