leasqr with bounds

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

leasqr with bounds

mrodrig
Hello,

I am new to the use of octave and I have tried to use the leasqr function
for the adjustment of some experimental data. Given the characteristics of
the adjustment function, some of the parameters to be adjusted must be
within certain limits (since they have a certain physical meaning). I wanted
to use the "bounds" option to limit the possible values that these
parameters can have.

In order to test the operation of the leasqr function I tried with the
following

x=[1:10]
y= [10,8,5.5,3.5,0,1.2,2.5,4.6,7.5,9.8]
F=@(x, p) (p (1) * x .^ 2 + p (2) * x + p (3))
a=1
b=1
c=1
p = [a,b,c];

wt=ones(size(x));
iter=400;
tol=0.000000001;
dFdp="dfdp";
dp=0.001*ones(size(p));
[f, pfit, kvg, iter, corp, covp, stdresid, Z, r2] = leasqr (Temp_bkg,
Int_Prom_bkg, par, F,tol, iter,wt, dp, dFdp);

and gives this results;

f =

   10.8900
    7.2773
    4.5502
    2.7086
    1.7527
    1.6824
    2.4977
    4.1986
    6.7852
   10.2573

pfit =

    0.44280
   -4.94114
   15.38833

kvg = 1
iter =  6

Assuming I want parameter 3 to be within the limit -1 and 1

bounds =[-5,5; -5,5; -1,1 ]

it gives me

[f, pfit, kvg, iter, corp, covp, stdresid, Z, r2] = leasqr (x, y, p, F,tol,
iter,wt, dp, dFdp, bounds)
 CONVERGENCE NOT ACHIEVED!
f =

   10.8516
    7.2616
    4.5519
    2.7225
    1.7732
    1.7041
    2.5153
    4.2067
    6.7783
   10.2302

pfit =

    0.44011
   -4.91026
   15.32171

kvg = 0
iter =  6

As I said, this is a function invented to test the operation.
I have tried with my experimental data and it does not work either.

I expected that by limiting the possible values of parameter 3, the
iterations would give a function that may not be the best, but that has the
parameter 3 with values within the allowed ones

Somebody could help me?

Thanks in advance



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


-----------------------------------------
Join us March 12-15 at CERN near Geneva
Switzerland for OctConf 2018.  More info:
https://wiki.octave.org/OctConf_2018
-----------------------------------------
Reply | Threaded
Open this post in threaded view
|

Re: leasqr with bounds

Przemek Klosowski-7
On 01/25/2018 10:09 PM, mrodrig wrote:

> In order to test the operation of the leasqr function I tried with the
> following
>
> x=[1:10]
> y= [10,8,5.5,3.5,0,1.2,2.5,4.6,7.5,9.8]
> F=@(x, p) (p (1) * x .^ 2 + p (2) * x + p (3))
> a=1
> b=1
> c=1
> p = [a,b,c];
>
> wt=ones(size(x));
> iter=400;
> tol=0.000000001;
> dFdp="dfdp";
> dp=0.001*ones(size(p));
> [f, pfit, kvg, iter, corp, covp, stdresid, Z, r2] = leasqr (Temp_bkg,
> Int_Prom_bkg, par, F,tol, iter,wt, dp, dFdp);
>
> and gives this results;
I did a lot of data fitting and so I'd like to help you on this. Thank
you for providing the sample code you are working with---it makes it
easy to engage. I think you mean to use x,y instead of what I assume are
your original fitting data tables Temp_bkg and Int_Prom_bkg:

[f, pfit, kvg, iter, corp, covp, stdresid, Z, r2] = leasqr (x,y, par,
F,tol, iter,wt, dp, dFdp)

because this gives the results you quote. Next, it seems, you need to do
something like

options.bounds=[-5,5; -5,5; -1,1]
par=[1,1,1] # avoid exceeding the bounds in initial parameters
[f, pfit, kvg, iter, corp, covp, stdresid, Z, r2] = leasqr (x,y, par,
F,tol, iter,wt, dp, dFdp, options)

and indeed we get no convergence.

I think that's because your function, with the parameter bounds you
propose, will not fit the data.
I concluded that by plotting your function with some values of
parameters in your proposed range:
plot(x,y,'o',x,F(x,[3,-15,1]))

Perhaps I missed something, but I ran out of time I could spend on it
because I had to start by figuring out the small problems with running
the code from the problem as you posed it: that's why it's important to
cross-check and test the code you are about to publish, so that others
don't have to.

I think that you need to play with the function F and the parameter
limits by plotting and inspecting it, to find the reasonable values of
initial parameters, in the way I showed above.
Good luck and please come back with more questions as you gain
experience with this problem.

Greetings

p



-----------------------------------------
Join us March 12-15 at CERN near Geneva
Switzerland for OctConf 2018.  More info:
https://wiki.octave.org/OctConf_2018
-----------------------------------------
Reply | Threaded
Open this post in threaded view
|

Re: leasqr with bounds

Francesco Potortì
In reply to this post by mrodrig
>I am new to the use of octave and I have tried to use the leasqr function
>for the adjustment of some experimental data. Given the characteristics of
>the adjustment function, some of the parameters to be adjusted must be
>within certain limits (since they have a certain physical meaning). I wanted
>to use the "bounds" option to limit the possible values that these
>parameters can have.
>
>In order to test the operation of the leasqr function I tried with the
>following
>
>x=[1:10]
>y= [10,8,5.5,3.5,0,1.2,2.5,4.6,7.5,9.8]
>F=@(x, p) (p (1) * x .^ 2 + p (2) * x + p (3))
>a=1
>b=1
>c=1
>p = [a,b,c];
>
>wt=ones(size(x));
>iter=400;
>tol=0.000000001;
>dFdp="dfdp";
>dp=0.001*ones(size(p));
>[f, pfit, kvg, iter, corp, covp, stdresid, Z, r2] = leasqr (Temp_bkg,
>Int_Prom_bkg, par, F,tol, iter,wt, dp, dFdp);
>
>and gives this results;
>
>f =
>
>   10.8900
>    7.2773
>    4.5502
>    2.7086
>    1.7527
>    1.6824
>    2.4977
>    4.1986
>    6.7852
>   10.2573
>
>pfit =
>
>    0.44280
>   -4.94114
>   15.38833
>
>kvg = 1
>iter =  6
>
>Assuming I want parameter 3 to be within the limit -1 and 1
>
>bounds =[-5,5; -5,5; -1,1 ]
>
>it gives me
>
>[f, pfit, kvg, iter, corp, covp, stdresid, Z, r2] = leasqr (x, y, p, F,tol,
>iter,wt, dp, dFdp, bounds)
> CONVERGENCE NOT ACHIEVED!
>f =
>
>   10.8516
>    7.2616
>    4.5519
>    2.7225
>    1.7732
>    1.7041
>    2.5153
>    4.2067
>    6.7783
>   10.2302
>
>pfit =
>
>    0.44011
>   -4.91026
>   15.32171
>
>kvg = 0
>iter =  6
>
>As I said, this is a function invented to test the operation.
>I have tried with my experimental data and it does not work either.
>
>I expected that by limiting the possible values of parameter 3, the
>iterations would give a function that may not be the best, but that has the
>parameter 3 with values within the allowed ones

I have used leasqr in the past, but never with bounds.  Leasqr is a
complicated beast.  I would try with a more gradual approach.  As far as
I understand, when unbounded you obtain p3=15.3.  Then instead of
forcing [-1,1] bounds try something easier, for example [-15,15] and see
what happens.

Also, try playing with the convergence options, maybe there is a
possibility of increasing the number of iterations, or you should change
the fractional precision or step change?

As a last suggestion, maybe you can sqp, described in Octave's manual at
"Nonlinear programming", or have a look at the many alternatives offered
by the optim package at
<https://octave.sourceforge.io/optim/package_doc/index.html>.  I haev no
experience with any of these alternatives.

--
Francesco Potortì (ricercatore)        Voice:  +39.050.621.3058
ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa         Skype:  wnlabisti
(entrance 20, 1st floor, room C71)     Web:    http://fly.isti.cnr.it



-----------------------------------------
Join us March 12-15 at CERN near Geneva
Switzerland for OctConf 2018.  More info:
https://wiki.octave.org/OctConf_2018
-----------------------------------------
Reply | Threaded
Open this post in threaded view
|

Re: leasqr with bounds

mrodrig
Although the example was not very happy in terms of convergence, the idea was
to show how my real problem is.

With the use of options instead of bounds I managed to adjust the function
to the desired parameters and have a meaning

Thank you very much for the help

mauricio



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


-----------------------------------------
Join us March 12-15 at CERN near Geneva
Switzerland for OctConf 2018.  More info:
https://wiki.octave.org/OctConf_2018
-----------------------------------------
Reply | Threaded
Open this post in threaded view
|

Re: leasqr with bounds

mrodrig
Hello,

Again I bother with the following

1) I'm writing a script where I want the file name (excel matrix) as input
and the script read the data.

to access the data I'm using A = xlsread ("filename.xlsx");

I found this on the web but it did not work

arg_list = argv ()

filename = argv {1}

load -ascii filename

Any suggestions?

2) The script reads the matrix and generates a graph of x and y. The
experimental data can be interpreted as the sum of four peaks, for example
Gaussian.

for this I defined four functions of the style

 F1 = @ (x, par) (par (1) ... par (4))
 F2 = @ (x, par) (par (5) ... par (8))
 F3 = @ (x, par) (par (9) ... par (12))
 F4 = @ (x, par) (par (13) ... par (16))

 with par = [par (1), par (2), par (3), par (4), par (5), par (6) .... par
(16)]
and then I get the sum function

Fsum = F1 (x, par) + F2 (x, par) + F3 (x, par) + F4 (x, par)

When I plot the experimental data and F1, F2, F3, F4 and Fsum it seems
reasonable.

But when I try to make the following fitting it throws me error

wt = ones (size (x));
iter = 20;
tol = 0.00001;
dFdp = "dfdp";
dp = 0.001 * ones (size (pair));

[f, pfit, kvg, iter] = leasqr (x, y, par, Fsum, tol, iter, wt, dp, dFdp);


error: F (301.01, _): subscripts must be either integers 1 to (2 ^ 31) -1 or
logicals
error: called from
    leasqr at line 329 column 9

Any ideas?

The next steps will be useing the bounds to include in the fitting

Thanks very much

Mauricio



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


-----------------------------------------
Join us March 12-15 at CERN near Geneva
Switzerland for OctConf 2018.  More info:
https://wiki.octave.org/OctConf_2018
-----------------------------------------