nonlin_min function

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

nonlin_min function

steph

I'll appreciate some help with the nonlin_min function

My code is as follows and the objective function is included as a separate file. My problem is that GPL_x, the column vector of unknowns to be solved is returned as GPL_x0, the seed values.

% initialise the GPL parameters
GPL_x = [0; 0; 0];  % in order D0 D1 m
 
% the first estimate of model parameters
% in the GPL model D(t) = D0 + D1*(t)^m
GPL_D0 = 0.0006; %0.000626249601623718;
GPL_D1 = 0.0001; %0.000107587010307332;
GPL_m  = 0.3     %0.312356329396664;     
 
% D_test = fn_GPL_model (GPL_D0, GPL_D1, GPL_m, BBR_tr)
  
GPL_D  = BBR_D(1,:);
GPL_tr = BBR_tr(1,:);
for ii = 2 : rows(BBR_D)
  GPL_D  = [GPL_D, BBR_D(ii,:)];
  GPL_tr = [GPL_tr, BBR_tr(ii,:)];
endfor
 
fun_GPL = @(GPL_x)fn_BBR_GPL_RMSE (GPL_x, GPL_D, GPL_tr);
%GPL_x0_lb = [1000.0; 1.0e6; 0.1; 0.0];
%GPL_x0_ub = [3000.0; 4.0e6; 0.9; 1.0];
GPL_x0 = [GPL_D0; GPL_D1; GPL_m];
[GPL_x, GPL_RMSE, cvg, outp] =...
   nonlin_min(fun_GPL, GPL_x0);
GPL_D0 = GPL_x(1);
GPL_D1 = GPL_x(2);
GPL_m  = GPL_x(3);
 
I'll appreciate some advice, thank you very much.

--
Steph Bredenhann



fn_BBR_GPL_RMSE.m (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: nonlin_min function

siko1056
On 4/5/20 9:59 PM, Steph Bredenhann wrote:

> I'll appreciate some help with the nonlin_min function
>
> My code is as follows and the objective function is included as a
> separate file. My problem is that GPL_x, the column vector of unknowns
> to be solved is returned as GPL_x0, the seed values.
>
> % initialise the GPL parameters
> GPL_x = [0; 0; 0];  % in order D0 D1 m
>  
> % the first estimate of model parameters
> % in the GPL model D(t) = D0 + D1*(t)^m
> GPL_D0 = 0.0006; %0.000626249601623718;
> GPL_D1 = 0.0001; %0.000107587010307332;
> GPL_m  = 0.3     %0.312356329396664;     
>  
> % D_test = fn_GPL_model (GPL_D0, GPL_D1, GPL_m, BBR_tr)
>   
> GPL_D  = BBR_D(1,:);
> GPL_tr = BBR_tr(1,:);
> for ii = 2 : rows(BBR_D)
>   GPL_D  = [GPL_D, BBR_D(ii,:)];
>   GPL_tr = [GPL_tr, BBR_tr(ii,:)];
> endfor
>  
> fun_GPL = @(GPL_x)fn_BBR_GPL_RMSE (GPL_x, GPL_D, GPL_tr);
> %GPL_x0_lb = [1000.0; 1.0e6; 0.1; 0.0];
> %GPL_x0_ub = [3000.0; 4.0e6; 0.9; 1.0];
> GPL_x0 = [GPL_D0; GPL_D1; GPL_m];
> [GPL_x, GPL_RMSE, cvg, outp] =...
>    nonlin_min(fun_GPL, GPL_x0);
> GPL_D0 = GPL_x(1);
> GPL_D1 = GPL_x(2);
> GPL_m  = GPL_x(3);
>  
> I'll appreciate some advice, thank you very much.
>
> --
> */Steph Bredenhann/*
>


Dear Steph Bredenhann,

In your code, the variables "BBR_D" and "BBR_tr" are not defined.
Please, can you send a minimal working example to reproduce your problem?

Kai


Reply | Threaded
Open this post in threaded view
|

Re: nonlin_min function

steph

On 2020/04/06 04:42, Kai Torben Ohlhus wrote:

> On 4/5/20 9:59 PM, Steph Bredenhann wrote:
>> I'll appreciate some help with the nonlin_min function
>>
>> My code is as follows and the objective function is included as a
>> separate file. My problem is that GPL_x, the column vector of unknowns
>> to be solved is returned as GPL_x0, the seed values.
>>
>> % initialise the GPL parameters
>> GPL_x = [0; 0; 0];  % in order D0 D1 m
>>  
>> % the first estimate of model parameters
>> % in the GPL model D(t) = D0 + D1*(t)^m
>> GPL_D0 = 0.0006; %0.000626249601623718;
>> GPL_D1 = 0.0001; %0.000107587010307332;
>> GPL_m  = 0.3     %0.312356329396664;
>>  
>> % D_test = fn_GPL_model (GPL_D0, GPL_D1, GPL_m, BBR_tr)
>>    
>> GPL_D  = BBR_D(1,:);
>> GPL_tr = BBR_tr(1,:);
>> for ii = 2 : rows(BBR_D)
>>    GPL_D  = [GPL_D, BBR_D(ii,:)];
>>    GPL_tr = [GPL_tr, BBR_tr(ii,:)];
>> endfor
>>  
>> fun_GPL = @(GPL_x)fn_BBR_GPL_RMSE (GPL_x, GPL_D, GPL_tr);
>> %GPL_x0_lb = [1000.0; 1.0e6; 0.1; 0.0];
>> %GPL_x0_ub = [3000.0; 4.0e6; 0.9; 1.0];
>> GPL_x0 = [GPL_D0; GPL_D1; GPL_m];
>> [GPL_x, GPL_RMSE, cvg, outp] =...
>>     nonlin_min(fun_GPL, GPL_x0);
>> GPL_D0 = GPL_x(1);
>> GPL_D1 = GPL_x(2);
>> GPL_m  = GPL_x(3);
>>  
>> I'll appreciate some advice, thank you very much.
>>
>> --
>> */Steph Bredenhann/*
>>
>
> Dear Steph Bredenhann,
>
> In your code, the variables "BBR_D" and "BBR_tr" are not defined.
> Please, can you send a minimal working example to reproduce your problem?
>
> Kai

Dear Kai

The variables are as follows, and please note that the values after %
for GPL_Do, etc are the values as calculated with Excel SOLVE function:

BBR_S =

     120.272     93.782     69.465     49.861     34.546     22.970
     276.088    230.073    185.954    145.986    111.780     83.573
     550.313    483.285    414.183    347.850    286.163    228.998
     720.538    671.688    605.032    547.171    480.848    415.814
    1006.866    947.639    886.043    822.754    739.724    667.029

BBR_D = 1./BBR_S

 >> BBR_D
BBR_D =

    0.00831449   0.01066299   0.01439580   0.02005593 0.02894722  
0.04353450
    0.00362203   0.00434644   0.00537768   0.00684999 0.00894616  
0.01196564
    0.00181715   0.00206917   0.00241439   0.00287480 0.00349451  
0.00436684
    0.00138785   0.00148879   0.00165280   0.00182758 0.00207966  
0.00240492
    0.00099318   0.00105525   0.00112861   0.00121543 0.00135186  
0.00149918

 >> BBR_tr
BBR_tr =

      434631.32360     814933.73175    1629867.46349 3259734.92699   
6519469.85398   13038939.70796
       29101.54173      54565.39075     109130.78150 218261.56301    
436523.12602     873046.25204
        1565.26858       2934.87860       5869.75719 11739.51438     
23479.02877      46958.05753
         149.31155        279.95915        559.91830 1119.83659      
2239.67319       4479.34638
           8.00000         15.00000         30.00000 60.00000       
120.00000        240.00000

 >>
--
Steph


Reply | Threaded
Open this post in threaded view
|

Re: nonlin_min function

siko1056
On 4/6/20 2:49 PM, Steph Bredenhann wrote:
>
> [...]
>
> The variables are as follows, and please note that the values after %
> for GPL_Do, etc are the values as calculated with Excel SOLVE function:
>
> [...]

Dear Steph,

I attached all necessary codes as m-files.

Maybe there is a typo in your function "fn_BBR_GPL_RMSE()"?  In short:

   Dcalc = D = D0 + D1*t.^m;
   SRE = (D-Dcalc)./D;

In this case "SRE" is always zero, as "Dcalc = D" and no progress seems
to happen.  Furthermore "D" seems to be an input parameter of
"fn_BBR_GPL_RMSE()", which is unused then.

HTH,
Kai



fn_BBR_GPL_RMSE.m (1K) Download Attachment
call_script.m (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: nonlin_min function

steph
On 2020/04/06 10:54, Kai Torben Ohlhus wrote:
On 4/6/20 2:49 PM, Steph Bredenhann wrote:
[...]

The variables are as follows, and please note that the values after %
for GPL_Do, etc are the values as calculated with Excel SOLVE function:

[...]
Dear Steph,

I attached all necessary codes as m-files.

Maybe there is a typo in your function "fn_BBR_GPL_RMSE()"?  In short:

   Dcalc = D = D0 + D1*t.^m;
   SRE = (D-Dcalc)./D;

In this case "SRE" is always zero, as "Dcalc = D" and no progress seems
to happen.  Furthermore "D" seems to be an input parameter of
"fn_BBR_GPL_RMSE()", which is unused then.

HTH,
Kai
Dear Kai

Thank you very much for taking time to look at my problem, it is appreciated.

There is a typo in my code, it is embarising! I actualy have another function to calculate the GPL D0 + D1*t^m but didn't want to confuse the issue so I replaced the function call with the one line code and then made the typo, my apologies. Removing the typo does not solve my problem, the line should be

Dcalc = D0 + D1*t.^m;

I have added a print statement to print the output of the function fn_BBR_GPL_RMSE:

fn_GPL: RMSE = 1.76664
fn_GPL: RMSE = 1.76566
fn_GPL: RMSE = 1.76762
fn_GPL: RMSE = 1.76433
fn_GPL: RMSE = 1.76895
fn_GPL: RMSE = 1.75902
fn_GPL: RMSE = 1.77424
fn_GPL: RMSE =     Inf
fn_GPL: RMSE = 64468270468788378205787493575484047335386768972351487380052923633120701124491032255846390698082304.00000
fn_GPL: RMSE = 128499488.88036
fn_GPL: RMSE = 61955.28569
fn_GPL: RMSE = 602.30488

the search should be for the min of RMSE, it oscillates (because I start very close to the min as determined by Excel) and then suddenly gets out of control.

Yet, on return nonlin_min still report a successful completion, with one iteration?

VE_BBR: GPL_cvg = 3.00000

>> GPL_outp
GPL_outp =

  scalar structure containing the fields:

    user_interaction =

      scalar structure containing the fields:

        stop = [](0x1)
        info = {}(0x1)

    niter =  1
    nobjf =  6

I am not experienced with nonlin_min and chances are that I misunderstand something.

Kind regards 
--
Steph



Reply | Threaded
Open this post in threaded view
|

RE: nonlin_min function

steph

From: Help-octave <help-octave-bounces+steph=[hidden email]> On Behalf Of Steph Bredenhann
Sent: Monday, April 06, 2020 12:08
To: Kai Torben Ohlhus <[hidden email]>
Cc: [hidden email]
Subject: Re: nonlin_min function

 

On 2020/04/06 10:54, Kai Torben Ohlhus wrote:

On 4/6/20 2:49 PM, Steph Bredenhann wrote:
 
[...]
 
The variables are as follows, and please note that the values after %
for GPL_Do, etc are the values as calculated with Excel SOLVE function:
 
[...]
 
Dear Steph,
 
I attached all necessary codes as m-files.
 
Maybe there is a typo in your function "fn_BBR_GPL_RMSE()"?  In short:
 
   Dcalc = D = D0 + D1*t.^m;
   SRE = (D-Dcalc)./D;
 
In this case "SRE" is always zero, as "Dcalc = D" and no progress seems
to happen.  Furthermore "D" seems to be an input parameter of
"fn_BBR_GPL_RMSE()", which is unused then.
 
HTH,
Kai

Dear Kai

 

Thank you very much for taking time to look at my problem, it is appreciated.

 

There is a typo in my code, it is embarising! I actualy have another function to calculate the GPL D0 + D1*t^m but didn't want to confuse the issue so I replaced the function call with the one line code and then made the typo, my apologies. Removing the typo does not solve my problem, the line should be

 

Dcalc = D0 + D1*t.^m;
 
I have added a print statement to print the output of the function fn_BBR_GPL_RMSE:
 
fn_GPL: RMSE = 1.76664
fn_GPL: RMSE = 1.76566
fn_GPL: RMSE = 1.76762
fn_GPL: RMSE = 1.76433
fn_GPL: RMSE = 1.76895
fn_GPL: RMSE = 1.75902
fn_GPL: RMSE = 1.77424
fn_GPL: RMSE =     Inf
fn_GPL: RMSE = 64468270468788378205787493575484047335386768972351487380052923633120701124491032255846390698082304.00000
fn_GPL: RMSE = 128499488.88036
fn_GPL: RMSE = 61955.28569
fn_GPL: RMSE = 602.30488
 
the search should be for the min of RMSE, it oscillates (because I start very close to the min as determined by Excel) and then suddenly gets out of control.
 
Yet, on return nonlin_min still report a successful completion, with one iteration?
 
VE_BBR: GPL_cvg = 3.00000
 
>> GPL_outp
GPL_outp =
 
  scalar structure containing the fields:
 
    user_interaction =
 
      scalar structure containing the fields:
 
        stop = [](0x1)
        info = {}(0x1)
 
    niter =  1
    nobjf =  6
 
I am not experienced with nonlin_min and chances are that I misunderstand something.
 
Kind regards 
 
Steph
 
Dear Kai
 
I didn’t attach the two files to my previous email, for completeness, here they are:
 
 

--
Steph




call_script.m (1K) Download Attachment
fn_BBR_GPL_RMSE.m (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: nonlin_min function

steph
In reply to this post by siko1056


On 2020/04/06 10:54, Kai Torben Ohlhus wrote:
On 4/6/20 2:49 PM, Steph Bredenhann wrote:
[...]

The variables are as follows, and please note that the values after %
for GPL_Do, etc are the values as calculated with Excel SOLVE function:

[...]
Dear Steph,

I attached all necessary codes as m-files.

Maybe there is a typo in your function "fn_BBR_GPL_RMSE()"?  In short:

   Dcalc = D = D0 + D1*t.^m;
   SRE = (D-Dcalc)./D;

In this case "SRE" is always zero, as "Dcalc = D" and no progress seems
to happen.  Furthermore "D" seems to be an input parameter of
"fn_BBR_GPL_RMSE()", which is unused then.

HTH,
Kai
Dear Kai

I have now abandoned nonlin_min and rather use fsolve, see attached edited .m files

The output is and seems quite good, will test further:

GPL parameters:
D0 = 0.000557218
D1 = 0.000053334
m  = 0.399999
GPL_RMSE  = 1.096952
fsolve outcome: GPL_cvg = 0:  maximum number of iterations exceeded   Iterations = 75

As you can see fsolve terminate with maximum iterations exceeded. Iterations reported = 75, however the fsolve default is 400 and although I increased MaxIter to 1000 the outcome stays the same. I assume the tolerances were met, but I still try to understand the tolerances, I work with defaults.

Any comments/improvements will be appreciated.


--
Steph



call_script.m (2K) Download Attachment
fn_BBR_GPL_RMSE.m (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: nonlin_min function

siko1056
On 4/9/20 4:56 AM, Steph Bredenhann wrote:

>
> I have now abandoned *nonlin_min* and rather use *fsolve*, see attached
> edited .m files
>
> The output is and seems quite good, will test further:
>
> GPL parameters:
> D0 = 0.000557218
> D1 = 0.000053334
> m  = 0.399999
> GPL_RMSE  = 1.096952
> fsolve outcome: GPL_cvg = 0:  maximum number of iterations exceeded  
> Iterations = 75
>
> As you can see fsolve terminate with maximum iterations exceeded.
> Iterations reported = 75, however the fsolve default is 400 and although
> I increased MaxIter to 1000 the outcome stays the same. I assume the
> tolerances were met, but I still try to understand the tolerances, I
> work with defaults.
>
> Any comments/improvements will be appreciated.
>
>
> --
> */Steph/*


Dear Steph,

Sorry for the late reply.  Regarding your first question you should
carefully read the manual [1].  The returned value "0" has two meanings [1]:

    "Iteration limit (either MaxIter or MaxFunEvals) exceeded."

You only increased "MaxIter", but "MaxFunEvals" reaches its default
value "100 * number_of_variables", "300" in your case, earlier.  You can
set this value in your script by adding:

    GPL_options = optimset ("fsolve");
    GPL_options.MaxIter = 1000;
    GPL_options.MaxFunEvals = 4000;


In general, fsolve() is an m-file, thus easy to debug and step through.
 Just set a breakpoint in your call_script.m, where fsolve() is called
and choose "step-in" to see what Octave is doing step-by-step.

HTH,
Kai

[1] https://octave.org/doc/v5.2.0/XREFfsolve.html


Reply | Threaded
Open this post in threaded view
|

Re: nonlin_min function

steph

 

On 2020/04/20 08:01, Kai Torben Ohlhus wrote:

On 4/9/20 4:56 AM, Steph Bredenhann wrote:
 
I have now abandoned *nonlin_min* and rather use *fsolve*, see attached
edited .m files
 
The output is and seems quite good, will test further:
 
GPL parameters:
D0 = 0.000557218
D1 = 0.000053334
m  = 0.399999
GPL_RMSE  = 1.096952
fsolve outcome: GPL_cvg = 0:  maximum number of iterations exceeded  
Iterations = 75
 
As you can see fsolve terminate with maximum iterations exceeded.
Iterations reported = 75, however the fsolve default is 400 and although
I increased MaxIter to 1000 the outcome stays the same. I assume the
tolerances were met, but I still try to understand the tolerances, I
work with defaults.
 
Any comments/improvements will be appreciated.
 
 
-- 
*/Steph/*
 
 
Dear Steph,
 
Sorry for the late reply.  Regarding your first question you should
carefully read the manual [1].  The returned value "0" has two meanings [1]:
 
    "Iteration limit (either MaxIter or MaxFunEvals) exceeded."
 
You only increased "MaxIter", but "MaxFunEvals" reaches its default
value "100 * number_of_variables", "300" in your case, earlier.  You can
set this value in your script by adding:
 
    GPL_options = optimset ("fsolve");
    GPL_options.MaxIter = 1000;
    GPL_options.MaxFunEvals = 4000;
 
 
In general, fsolve() is an m-file, thus easy to debug and step through.
 Just set a breakpoint in your call_script.m, where fsolve() is called
and choose "step-in" to see what Octave is doing step-by-step.
 
HTH,
Kai
 
[1] https://octave.org/doc/v5.2.0/XREFfsolve.html

Dear Kai

 

In the mean time I have read the manuals again and again and searched Google and I have made the suggested change to MaxFunEvals already, have now increased to 4000 as you suggest, see the options as I have it now.

 

GPL_options =

  scalar structure containing the fields:

    AutoScaling = off
    ComplexEqn = off
    FunValCheck = off
    FinDiffType = forward
    Jacobian = off
    MaxFunEvals =  4000
    MaxIter =  1000
    OutputFcn = [](0x0)
    Updating = off
    TolFun =  0.0000000010000
    TolX =  0.0000000010000
    TypicalX = [](0x0)

 

The outcome has now changed to:

 

fsolve outcome: GPL_info = 3:  improvement in objective function less than specified

 

As you can see I have made TolFun and TolX very small.

 

I have also changed my FUN to have a zero RHS as per the manual.

 

It seems to me that the model I am investigating just don’t have a solution with this search algorithm.

 

Thanks for your assistance, it is much appreciated.

 

--
Steph



Reply | Threaded
Open this post in threaded view
|

Re: nonlin_min function

siko1056
On 4/20/20 6:28 PM, Steph Bredenhann wrote:

> Dear Kai
>
> In the mean time I have read the manuals again and again and searched
> Google and I have made the suggested change to MaxFunEvals already, have
> now increased to 4000 as you suggest, see the options as I have it now.
>
> GPL_options =
>
>   scalar structure containing the fields:
>
>     AutoScaling = off
>     ComplexEqn = off
>     FunValCheck = off
>     FinDiffType = forward
>     Jacobian = off
>     MaxFunEvals =  4000
>     MaxIter =  1000
>     OutputFcn = [](0x0)
>     Updating = off
>     TolFun =  0.0000000010000
>     TolX =  0.0000000010000
>     TypicalX = [](0x0)
>
> The outcome has now changed to:
>
> fsolve outcome: GPL_info = 3:  improvement in objective function less
> than specified
>
> As you can see I have made TolFun and TolX very small.
>
> I have also changed my FUN to have a zero RHS as per the manual.
>
> It seems to me that the model I am investigating just don’t have a
> solution with this search algorithm.
>
> Thanks for your assistance, it is much appreciated.
>
> --
> */Steph /*
>


Dear Steph,

Can you attach the latest version of your m-files for debugging?

Thanks,
Kai


Reply | Threaded
Open this post in threaded view
|

RE: nonlin_min function

steph

-----Original Message-----
From: Kai Torben Ohlhus <[hidden email]>
Sent: Thursday, April 23, 2020 09:53
To: Steph Bredenhann <[hidden email]>
Cc: [hidden email]
Subject: Re: nonlin_min function

On 4/20/20 6:28 PM, Steph Bredenhann wrote:

> Dear Kai
>
> In the mean time I have read the manuals again and again and searched
> Google and I have made the suggested change to MaxFunEvals already,
> have now increased to 4000 as you suggest, see the options as I have it now.
>
> GPL_options =
>
>   scalar structure containing the fields:
>
>     AutoScaling = off
>     ComplexEqn = off
>     FunValCheck = off
>     FinDiffType = forward
>     Jacobian = off
>     MaxFunEvals =  4000
>     MaxIter =  1000
>     OutputFcn = [](0x0)
>     Updating = off
>     TolFun =  0.0000000010000
>     TolX =  0.0000000010000
>     TypicalX = [](0x0)
>
> The outcome has now changed to:
>
> fsolve outcome: GPL_info = 3:  improvement in objective function less
> than specified
>
> As you can see I have made TolFun and TolX very small.
>
> I have also changed my FUN to have a zero RHS as per the manual.
>
> It seems to me that the model I am investigating just don’t have a
> solution with this search algorithm.
>
> Thanks for your assistance, it is much appreciated.
>
> --
> */Steph /*
>

Dear Steph,

Can you attach the latest version of your m-files for debugging?

Thanks,
Kai

Dear Kai

m-files attached as requested.

I must just mention that, as the problem is intrinsically a curve fitting problem I have also tried lsqcurvefit function with no improvement, as with fsolve my "guess" must be very close to the real solution (which I have from Excel) and then only 3-5 iterations are completed.

Looking forward to your feedback

Steph



fn_GPL_model.m (308 bytes) Download Attachment
call_script.m (2K) Download Attachment
fn_BBR_GPL.m (743 bytes) Download Attachment