help-octave
[Top][All Lists]
Advanced

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

Fwd: Re: Passing global variables into sqp


From: Martin Helm
Subject: Fwd: Re: Passing global variables into sqp
Date: Tue, 28 Dec 2010 16:09:12 +0100
User-agent: KMail/1.13.5 (Linux/2.6.34.7-0.5-desktop; KDE/4.5.4; x86_64; ; )

Betreff: Re: Passing global variables into sqp
Datum: Dienstag, 28. Dezember 2010, 13:44:26
Von: Agustin Lebron <address@hidden>
An:  Martin Helm <address@hidden>

Thanks so much for your help, I like this anonymous function approach much 
better.

Agustin

--- On Tue, 12/28/10, Martin Helm <address@hidden> wrote:

> From: Martin Helm <address@hidden>
> Subject: Re: Passing global variables into sqp
> To: address@hidden
> Cc: "Alebron" <address@hidden>
> Date: Tuesday, December 28, 2010, 4:30 AM
> Am Dienstag, 28. Dezember 2010,
> 11:01:05 schrieb Alebron:
> > Is it possible to pass global variables into the cost
> function (phi) in
> > sqp? This code doesn't work:
> > 
> > global a;
> > a = 3;
> > global b;
> > b = 4;
> > 
> > function r = phi(x)
> >   a
> >   b
> >   x
> >   r=(x(1)-a)^2+(x(2)-b)^2
> > endfunction
> > 
> > x0=[0;0];
> > 
> > sqp(x0, @phi, [], [])
> > 
> > I get: "error: `a' undefined near line 7 column 3"
> > 
> > Any ideas?
> > 
> > Thanks,
> > 
> > Agustin
> 
> You need to declare also in the function a and b as global
> 
> function r = phi(x)
>   global a
>   global b
>   x
>   r=(x(1)-a)^2+(x(2)-b)^2
> endfunction
> 
> But global variables are nearly always a bad idea use
> anonymous functions for 
> that.
> 
> function r = phi(x, a, b)
>   x
>   r=(x(1)-a)^2+(x(2)-b)^2
> endfunction
> 
> x0=[0;0];
>  sqp(x0, @(x) phi(x, 3, 4), [], [])
> 
> 
> 

Please keep the list on cc so that others know that the problem is solved.


reply via email to

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