Thread Subject: question about passing a value of a parameter to functions for optimization

Subject: question about passing a value of a parameter to functions for optimization

From: Hailey Yang

Date: 9 Jul, 2008 14:31:02

Message: 1 of 6

Hi,

I am a beginner, not quite familiar with MATLAB programming
yet. I am stuck with a basic question that I couldn't find
an answer for:

Say that I have a function:

function f=profit(x,y,a,b)
f=x*a+y*b

I want to optimize this function. But here actually a and b
are not variables; they are just parameters that I will
determine the value outside of the optimization. How can I
pass the value of parameters into functions so that the
optimization only runs over the variables?

Thanks!

Subject: question about passing a value of a parameter to functions for optimization

From: Titus

Date: 9 Jul, 2008 14:44:10

Message: 2 of 6


"Hailey Yang" <haileyyang@hotmail.com> schrieb im Newsbeitrag
news:g52i36$4ai$1@fred.mathworks.com...
> Hi,
>
> I am a beginner, not quite familiar with MATLAB programming
> yet. I am stuck with a basic question that I couldn't find
> an answer for:
>
> Say that I have a function:
>
> function f=profit(x,y,a,b)
> f=x*a+y*b
>
> I want to optimize this function. But here actually a and b
> are not variables; they are just parameters that I will
> determine the value outside of the optimization. How can I
> pass the value of parameters into functions so that the
> optimization only runs over the variables?
>
> Thanks!
>
>

Hi,
use anonymous functions for this:

a = 42;
b = 23;
f = @(x,y) profit(x,y,a,b);

and pass this f to the optimizer. f now is a function of two variables
(which has the values a=42 and b=23 implicitly stored). You can try to call
it yourself:

val = f(2,3)
val =
   153

Titus

Subject: question about passing a value of a parameter to functions for optimization

From: Hailey Yang

Date: 9 Jul, 2008 15:22:02

Message: 3 of 6

Thank you very much! It worked!

But this can be very inefficient if I have many parameters
in my function, in which case I will still need to list all
the parameters as arguments of the function and then pass
the values to them. Is there any way to simplify this?

I remember there is "global" in MATLAB, which can declare
variables to be used in all functions. Could this help in
that if I declare some parameters/variables as global
upfront, I don't need to list them as arguments of a
function when I make the function and their values will be
carried to the function? Or this is totally a wrong
understanding of "global"?

Yingxia




"Titus" <titus.edelhofer@mathworks.de> wrote in message
<g52irq$e1s$1@fred.mathworks.com>...
>
> "Hailey Yang" <haileyyang@hotmail.com> schrieb im
Newsbeitrag
> news:g52i36$4ai$1@fred.mathworks.com...
> > Hi,
> >
> > I am a beginner, not quite familiar with MATLAB
programming
> > yet. I am stuck with a basic question that I couldn't
find
> > an answer for:
> >
> > Say that I have a function:
> >
> > function f=profit(x,y,a,b)
> > f=x*a+y*b
> >
> > I want to optimize this function. But here actually a
and b
> > are not variables; they are just parameters that I will
> > determine the value outside of the optimization. How
can I
> > pass the value of parameters into functions so that the
> > optimization only runs over the variables?
> >
> > Thanks!
> >
> >
>
> Hi,
> use anonymous functions for this:
>
> a = 42;
> b = 23;
> f = @(x,y) profit(x,y,a,b);
>
> and pass this f to the optimizer. f now is a function of
two variables
> (which has the values a=42 and b=23 implicitly stored).
You can try to call
> it yourself:
>
> val = f(2,3)
> val =
> 153
>
> Titus
>
>

Subject: question about passing a value of a parameter to functions for optimization

From: Titus

Date: 9 Jul, 2008 15:28:19

Message: 4 of 6


"Hailey Yang" <haileyyang@hotmail.com> schrieb im Newsbeitrag
news:g52l2q$fqo$1@fred.mathworks.com...
> Thank you very much! It worked!
>
> But this can be very inefficient if I have many parameters
> in my function, in which case I will still need to list all
> the parameters as arguments of the function and then pass
> the values to them. Is there any way to simplify this?
>
> I remember there is "global" in MATLAB, which can declare
> variables to be used in all functions. Could this help in
> that if I declare some parameters/variables as global
> upfront, I don't need to list them as arguments of a
> function when I make the function and their values will be
> carried to the function? Or this is totally a wrong
> understanding of "global"?
>
> Yingxia
>
>
>
>
> "Titus" <titus.edelhofer@mathworks.de> wrote in message
> <g52irq$e1s$1@fred.mathworks.com>...
>>
>> "Hailey Yang" <haileyyang@hotmail.com> schrieb im
> Newsbeitrag
>> news:g52i36$4ai$1@fred.mathworks.com...
>> > Hi,
>> >
>> > I am a beginner, not quite familiar with MATLAB
> programming
>> > yet. I am stuck with a basic question that I couldn't
> find
>> > an answer for:
>> >
>> > Say that I have a function:
>> >
>> > function f=profit(x,y,a,b)
>> > f=x*a+y*b
>> >
>> > I want to optimize this function. But here actually a
> and b
>> > are not variables; they are just parameters that I will
>> > determine the value outside of the optimization. How
> can I
>> > pass the value of parameters into functions so that the
>> > optimization only runs over the variables?
>> >
>> > Thanks!
>> >
>> >
>>
>> Hi,
>> use anonymous functions for this:
>>
>> a = 42;
>> b = 23;
>> f = @(x,y) profit(x,y,a,b);
>>
>> and pass this f to the optimizer. f now is a function of
> two variables
>> (which has the values a=42 and b=23 implicitly stored).
> You can try to call
>> it yourself:
>>
>> val = f(2,3)
>> val =
>> 153
>>
>> Titus
>>
>>
>

Hi,
yes, if you have many parameters, this might be tedious. One (although not
nice) solution are globals like you describe, but I would perhaps then use
nested functions: the nested function profit "knows" all parameters from the
main function.

function nested
a = 23;
b = 42;
y = profit(2,3)
  function f=profit(x,y)
    f=x*a+y*b;
  end
end

Titus

Subject: question about passing a value of a parameter to functions for optimization

From: Hailey Yang

Date: 9 Jul, 2008 15:40:19

Message: 5 of 6

This is great! Thank you so much!

"Titus" <titus.edelhofer@mathworks.de> wrote in message
<g52lek$l86$1@fred.mathworks.com>...
>
> "Hailey Yang" <haileyyang@hotmail.com> schrieb im
Newsbeitrag
> news:g52l2q$fqo$1@fred.mathworks.com...
> > Thank you very much! It worked!
> >
> > But this can be very inefficient if I have many
parameters
> > in my function, in which case I will still need to list
all
> > the parameters as arguments of the function and then
pass
> > the values to them. Is there any way to simplify this?
> >
> > I remember there is "global" in MATLAB, which can
declare
> > variables to be used in all functions. Could this help
in
> > that if I declare some parameters/variables as global
> > upfront, I don't need to list them as arguments of a
> > function when I make the function and their values will
be
> > carried to the function? Or this is totally a wrong
> > understanding of "global"?
> >
> > Yingxia
> >
> >
> >
> >
> > "Titus" <titus.edelhofer@mathworks.de> wrote in message
> > <g52irq$e1s$1@fred.mathworks.com>...
> >>
> >> "Hailey Yang" <haileyyang@hotmail.com> schrieb im
> > Newsbeitrag
> >> news:g52i36$4ai$1@fred.mathworks.com...
> >> > Hi,
> >> >
> >> > I am a beginner, not quite familiar with MATLAB
> > programming
> >> > yet. I am stuck with a basic question that I couldn't
> > find
> >> > an answer for:
> >> >
> >> > Say that I have a function:
> >> >
> >> > function f=profit(x,y,a,b)
> >> > f=x*a+y*b
> >> >
> >> > I want to optimize this function. But here actually a
> > and b
> >> > are not variables; they are just parameters that I
will
> >> > determine the value outside of the optimization. How
> > can I
> >> > pass the value of parameters into functions so that
the
> >> > optimization only runs over the variables?
> >> >
> >> > Thanks!
> >> >
> >> >
> >>
> >> Hi,
> >> use anonymous functions for this:
> >>
> >> a = 42;
> >> b = 23;
> >> f = @(x,y) profit(x,y,a,b);
> >>
> >> and pass this f to the optimizer. f now is a function
of
> > two variables
> >> (which has the values a=42 and b=23 implicitly stored).
> > You can try to call
> >> it yourself:
> >>
> >> val = f(2,3)
> >> val =
> >> 153
> >>
> >> Titus
> >>
> >>
> >
>
> Hi,
> yes, if you have many parameters, this might be tedious.
One (although not
> nice) solution are globals like you describe, but I would
perhaps then use
> nested functions: the nested function profit "knows" all
parameters from the
> main function.
>
> function nested
> a = 23;
> b = 42;
> y = profit(2,3)
> function f=profit(x,y)
> f=x*a+y*b;
> end
> end
>
> Titus
>
>

Subject: question about passing a value of a parameter to functions for optimization

From: Steven Lord

Date: 9 Jul, 2008 16:43:53

Message: 6 of 6


"Titus" <titus.edelhofer@mathworks.de> wrote in message
news:g52lek$l86$1@fred.mathworks.com...
>
> "Hailey Yang" <haileyyang@hotmail.com> schrieb im Newsbeitrag
> news:g52l2q$fqo$1@fred.mathworks.com...
>> Thank you very much! It worked!
>>
>> But this can be very inefficient if I have many parameters
>> in my function, in which case I will still need to list all
>> the parameters as arguments of the function and then pass
>> the values to them. Is there any way to simplify this?
>>
>> I remember there is "global" in MATLAB, which can declare
>> variables to be used in all functions. Could this help in
>> that if I declare some parameters/variables as global
>> upfront, I don't need to list them as arguments of a
>> function when I make the function and their values will be
>> carried to the function? Or this is totally a wrong
>> understanding of "global"?

*snip*

You _can_ use global variables, but that's generally discouraged because it
can be difficult to debug problems caused by some other function modifying
your function's global variables. The nested function approach Titus
mentioned is one way to handle the situation where you have a lot of
parameters to pass into your function; another method is to pass in a struct
array containing all your parameters and extract them as needed inside your
function.


% Begin example924.m
function f = example924

myparameters.a = 1;
myparameters.b = -2;

% Sample problem: minimize x(1)-2*x(2)
% with the constraints x(1)+x(2) <= 5 and -x(1)+x(2) <= 6
f = fmincon(@(x) myobjective(x, myparameters), [1; 2], [1 1; -1 1], [5; 6]);

function objectiveValue = myobjective(x, parameters)
objectiveValue = x(1)*parameters.a + x(2)*parameters.b;
% The above line could have been replaced by these three, if you were going
to use a and b repeatedly
% a = parameters.a;
% b = parameters.b;
% objectiveValue = x(1)*a + x(2)*b;
% End example924.m


That way you only have to pass around one "box" (the struct array)
containing all your parameters, you can extract what you need when you need
it, and (if you choose your field names well) the struct can be somewhat
self-documenting, like:

myparameters.interestRate = 0.06;
myparameters.durationYears = 15;

--
Steve Lord
slord@mathworks.com

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com