help-octave
[Top][All Lists]
Advanced

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

Re: Passing global variables into sqp


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

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), [], [])




reply via email to

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