help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: leasqr function - global variables


From: German Ocampo
Subject: Re: leasqr function - global variables
Date: Mon, 1 Jun 2009 10:08:41 +0100

Ivan 

Thank you for your help. I added global dx at the beginning of vel11 function and is working

Regards

German

On Mon, Jun 1, 2009 at 10:05 AM, Ivan Sutoris <address@hidden> wrote:
On Mon, Jun 1, 2009 at 10:45 AM, German Ocampo <address@hidden> wrote:
> Good morning
> I'm new working with Octave and I have a question regarding to pass a
> variable (dx) inside the objective function of the leasqr function. I define
> the variable as global but Octave is giving me errors about this variable is
> not defined inside the function.
> How can I pass this variable? I'm including the code.
> Thanks
> German
> function viblin
>   % test data
>   #data = ""  0  0  0  1  2   5   7  10  13   16  19  21  24  27  30  33
>  35   38  41  43  46  48  49  49  49]';
>   #t    =  [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]';
>   data=[ 1 2 5 7 10 13 16 19 21 24 27 30 33 35 38 41 43 46 48 49]';
>   t =  [ 1 2 3 4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]';
>   pin = [3.5; .45];
>   F = @vel11;
>   global dx;
>   dx=50;
>   global verbose;
>   verbose=1;
>  [f1, p1, kvg1, iter1, corp1, covp1, covr1, stdresid1, Z1, r21] = leasqr (t,
> data, pin, F);
>
> function y = vel11(x,p)
> for i=1:length(x)
> if (x(i)<(p(1)/p(2)))
> y(i)=0.5*p(2)*x(i)^2;
> else
> if (x(i)<(dx/p(1)))
> y(i)=p(1)*x(i)-p(1)*p(1)/(2*p(2));
> else
> y(i)=(dx-p(1)^2/(2*p(2)))-0.5*p(2)*(x(i)-dx/p(1))*(x(i)-dx/p(1))+p(1)*(x(i)-dx/p(1));
> endif
> endif
> endfor
> y=y';
> endfunction
>

Hi

You need to mark dx as global variable also in other functions where
it's used - putting "global dx" in the beginning of vel11 function
seems to fix the problem.

Ivan Sutoris


reply via email to

[Prev in Thread] Current Thread [Next in Thread]