optimizing error: undefined argument

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

optimizing error: undefined argument

DGati
Hi octave users,

I'm currently trying out the different optimizers in the optim-package on Octave 3.2.4, such as minimize(), line_min, or nmsmax. The file I'm minimizing (or in case of nmsmax, I'm maximizing the file*-1) is for simplicity's sake:

function result = func(x)
result=0.5*(x)^2-10;
endfunction

The error that I keep getting is

error: 'x' undefined near line 2, column 7
error: evaluating argument list element number 1
error: evaluating argument list element number 1

I don't understand that. How more should I define 'x'  when it is simply the argument of the function?
Thanks for tips!
Reply | Threaded
Open this post in threaded view
|

Re: optimizing error: undefined argument

Doug Stewart-4


On Tue, Aug 16, 2011 at 4:57 AM, DGati <[hidden email]> wrote:
Hi octave users,

I'm currently trying out the different optimizers in the optim-package on
Octave 3.2.4, such as minimize(), line_min, or nmsmax. The file I'm
minimizing (or in case of nmsmax, I'm maximizing the file*-1) is for
simplicity's sake:

function result = func(x)
result=0.5*(x)^2-10;
endfunction

The error that I keep getting is

error: 'x' undefined near line 2, column 7
error: evaluating argument list element number 1
error: evaluating argument list element number 1

I don't understand that. How more should I define 'x'  when it is simply the
argument of the function?
Thanks for tips!


To use your function you must pass a number to it like:

func(6)

or

w=6
func(w)

this will not give an error, But

func
will give the error that you are seeing.


Doug
 

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: optimizing error: undefined argument

DGati
Doug Stewart-4 wrote
To use your function you must pass a number to it like:

func(6)

or

w=6
func(w)

this will not give an error, But

func
will give the error that you are seeing.


Doug

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Yes, but if I try minimizing a function or maximizing it, I shouldn't pass it a number since I don't know at which value the function will be minimal. This is exactly the point.

minimize(func, x) is the syntax to 'minimize', where x is the list of arguments to func (in this case, only one). Surely I wouldn't need to pass a number for x?
Reply | Threaded
Open this post in threaded view
|

RE: optimizing error: undefined argument

Allen.Windhorn-2

Doug Stewart-4 wrote:
 
>> To use your function you must pass a number to it like:
>>
>> func(6) ...

-----Original Message-----
From: [hidden email] On Behalf Of DGati

> Yes, but if I try minimizing a function or maximizing it, I
> shouldn't pass it a number since I don't know at which value
> the function will be minimal.  This is exactly the point.
>
> minimize(func, x) is the syntax to 'minimize', where x is the
> list of arguments to func (in this case, only one). Surely I
> wouldn't need to pass a number for x?

I looked at the documentation for minimize and it wasn't at all
clear to me what is expected, but maybe it is looking for an
initial guess in which case you would have to give it numbers.
It also looked as if it wanted a string for the function (e.g.
minimize("func", x), or maybe a function pointer.  I haven't
looked at the code though.

Have you considered sqp or one of the other optimization
routines as an alternative?

Regards,
Allen
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: optimizing error: undefined argument

Jordi Gutiérrez Hermoso-2
In reply to this post by DGati
On 16 August 2011 03:57, DGati <[hidden email]> wrote:

> I'm currently trying out the different optimizers in the optim-package on
> Octave 3.2.4, such as minimize(), line_min, or nmsmax. The file I'm
> minimizing (or in case of nmsmax, I'm maximizing the file*-1) is for
> simplicity's sake:
>
> function result = func(x)
> result=0.5*(x)^2-10;
> endfunction
>
> The error that I keep getting is
>
> error: 'x' undefined near line 2, column 7
> error: evaluating argument list element number 1
> error: evaluating argument list element number 1
>
> I don't understand that. How more should I define 'x'  when it is simply the
> argument of the function?
> Thanks for tips!

If you write

    some_func(func, some_parameter)

and func is a function, that is equivalent to

    some_func(func(), some_parameter)

i.e. it's equivalent to calling func() without parameters and passing
that result to some_func. In this case, since func requires that
parameter, you are seeing the error that the parameter is undefined.
What you instead want to write is

    some_func(@func, some_parameter)

i.e. create a handle to func with the @func syntax, and pass that to
some_func. Most, perhaps all Octave functions also accept the name of
the function in a string instead of a handle, so you can also write
this instead:

     some_func("func", some_paramter)

HTH,
- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: optimizing error: undefined argument

DGati
@Allen: Yes, I also tried nmsmax but not not sqp; thanks for the reference.

Jordi Gutiérrez Hermoso-2 wrote
What you instead want to write is

    some_func(@func, some_parameter)

i.e. create a handle to func with the @func syntax, and pass that to
some_func. Most, perhaps all Octave functions also accept the name of
the function in a string instead of a handle, so you can also write
this instead:

     some_func("func", some_paramter)

HTH,
- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
This helps if I work with nmsmax (obviously with function rewritten as *-1). However, minimize still isn't quite doing what it should. This is the documentation for it:

[x,v,nev,...] = minimize (f,args,...) - Minimize f

 ARGUMENTS
 f    : string  : Name of function. Must return a real value
 args : list or : List of arguments to f (by default, minimize the first)
        matrix  : f's only argument

 RETURNED VALUES
 x   : matrix  : Local minimum of f. Let's suppose x is M-by-N.
 v   : real    : Value of f in x0
 nev : integer : Number of function evaluations
     or 1 x 2  : Number of function and derivative evaluations (if
                 derivatives are used)
--> This means args should list the arguments. My function, x^2-10, only has one argument. Thus I typed

octave-3.2.4.exe:37> minimize(@func,x)
error: for A^b, A must be square
error: called from:
error: D:\octwork\func.m at line 2, column 7

Putting a number instead of x won't do either. Thus, minimize(@func,1) yields ans =-1, which is interesting not only because it's wrong, but also because it doesn't follow the syntax minimize is supposed to have, the returned value is completely different from how it should look.
Reply | Threaded
Open this post in threaded view
|

Re: optimizing error: undefined argument

Jordi Gutiérrez Hermoso-2
On 16 August 2011 13:46, DGati <[hidden email]> wrote:
> Thus I typed
>
> octave-3.2.4.exe:37> minimize(@func,x)
> error: for A^b, A must be square
> error: called from:
> error: D:\octwork\func.m at line 2, column 7

My guess is that that minimize function expects a vectorised function.
It's passing it a vector argument and then the function is trying to
square a vector, which doesn't make linear algebraic sense. Rewrite
your function to use the elementwise .^ power operator instead of
matrix ^ power  operator.

- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave