Fwd: Re: Passing global variables into sqp

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Fwd: Re: Passing global variables into sqp

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

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

Agustin

--- On Tue, 12/28/10, Martin Helm <[hidden email]> wrote:

> From: Martin Helm <[hidden email]>
> Subject: Re: Passing global variables into sqp
> To: [hidden email]
> Cc: "Alebron" <[hidden email]>
> 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.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave