Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Problem regarding Maximizing a function

Subject: Problem regarding Maximizing a function

From: lovish

Date: 30 Mar, 2009 00:17:35

Message: 1 of 6

Hi guys,

I am having problems with maximizing a function using optimization tool. I have to maximize

max f(x)= log (x1) + log (x2) + log (x3)

with the constraints that x1+x2 <=1 ; x1+x3<=1

I'm solving it using optimization tool box using fmin solver. The function I have created is

function f=objfun(x)
f= log(x1)+log(x2)+ log (x3);
f=-z;

I have used f=-z in order to maximize the function as fmin minimizes the function f(x) But it isn't working. Please guide me as to how should I use the toolbox in order to maximize the function.

Thanks

Subject: Problem regarding Maximizing a function

From: John D'Errico

Date: 30 Mar, 2009 00:50:18

Message: 2 of 6

"lovish " <lovishagarwal@gmail.com> wrote in message <gqp32v$kum$1@fred.mathworks.com>...
> Hi guys,
>
> I am having problems with maximizing a function using optimization tool. I have to maximize
>
> max f(x)= log (x1) + log (x2) + log (x3)
>
> with the constraints that x1+x2 <=1 ; x1+x3<=1
>
> I'm solving it using optimization tool box using fmin solver. The function I have created is

There is no solver called fmin in the optimization
toolbox. So it will be difficult to help you.


>
> function f=objfun(x)
> f= log(x1)+log(x2)+ log (x3);
> f=-z;
>
> I have used f=-z in order to maximize the function as fmin minimizes the function f(x) But it isn't working. Please guide me as to how should I use the toolbox in order to maximize the function.
>

What is not working? What is wrong? What has
failed? In what way did it fail?

The crystal ball is cloudy.

John

Subject: Problem regarding Maximizing a function

From: lovish

Date: 30 Mar, 2009 01:18:01

Message: 3 of 6

 I'm working with optimization toolbox using the 'fmincon' solver . The fmincon solver works to minimze a function. There's no inbuilt function to maximize a function.

The problem that I'm encountering is that for instance
when I need to maximize f(x)= x1 + x2
 s.t x1+x2<=2 where x1 and x2>=0

I ain't able to do that. I'm setting the function as

> > function f=objfun(x)
> > f= x1+x2;
> > f=-z;

Instead of getting the values as x1=1 and x2=1 , it it displaying x1=x2=0 and f(x)=0 as it it minimizing the function. How should I modify the function or the conditions in the solver to maximize the function?? I would highly appreciate if someone could help me.

Thanks and Regards,
Lovish


"John D'Errico" <woodchips@rochester.rr.com> wrote in message <gqp50a$i4s$1@fred.mathworks.com>...
> "lovish " <lovishagarwal@gmail.com> wrote in message <gqp32v$kum$1@fred.mathworks.com>...
> > Hi guys,
> >
> > I am having problems with maximizing a function using optimization tool. I have to maximize
> >
> > max f(x)= log (x1) + log (x2) + log (x3)
> >
> > with the constraints that x1+x2 <=1 ; x1+x3<=1
> >
> > I'm solving it using optimization tool box using fmin solver. The function I have created is
>
> There is no solver called fmin in the optimization
> toolbox. So it will be difficult to help you.
>
>
> >
> > function f=objfun(x)
> > f= log(x1)+log(x2)+ log (x3);
> > f=-z;
> >
> > I have used f=-z in order to maximize the function as fmin minimizes the function f(x) But it isn't working. Please guide me as to how should I use the toolbox in order to maximize the function.
> >
>
> What is not working? What is wrong? What has
> failed? In what way did it fail?
>
> The crystal ball is cloudy.
>
> John

Subject: Problem regarding Maximizing a function

From: Roger Stafford

Date: 30 Mar, 2009 01:45:03

Message: 4 of 6

"lovish " <lovishagarwal@gmail.com> wrote in message <gqp32v$kum$1@fred.mathworks.com>...
> .......
> I am having problems with maximizing a function using optimization tool. I have to maximize
>
> max f(x)= log (x1) + log (x2) + log (x3)
>
> with the constraints that x1+x2 <=1 ; x1+x3<=1
> ......

  I assume that you also require that 0<x1, 0<x2, and 0<x3 in order to get finite, real values for the logarithms.

  I am puzzled that you need to use the brute force methods of the Optimization Toolbox for this simple problem. By elementary calculus the maximum value of your function is precisely: log(4/27).

Roger Stafford

Subject: Problem regarding Maximizing a function

From: Matt Fig

Date: 30 Mar, 2009 01:54:02

Message: 5 of 6

"lovish " <lovishagarwal@gmail.com> wrote in message <gqp6k9$q5p$1@fred.mathworks.com>...
> I'm working with optimization toolbox using the 'fmincon' solver . The fmincon solver works to minimze a function. There's no inbuilt function to maximize a function.
>
> The problem that I'm encountering is that for instance
> when I need to maximize f(x)= x1 + x2
> s.t x1+x2<=2 where x1 and x2>=0
>
> I ain't able to do that. I'm setting the function as


Ain't? Please!


> > > function f=objfun(x)
> > > f= x1+x2;
> > > f=-z;
>


As far as your function f, I am wondering why it doesn't error on the last line. Where is z defined in that function? How can you take the negative of an undefined quantity? Actually the same holds for x1 and x2. Where are they defined in that function? Do you mean:

function f=objfun(x)
% Takes a vector with at least two elements.
f = x(1)+x(2);
f = -f;

Subject: Problem regarding Maximizing a function

From: John D'Errico

Date: 30 Mar, 2009 01:59:01

Message: 6 of 6

"lovish " <lovishagarwal@gmail.com> wrote in message <gqp6k9$q5p$1@fred.mathworks.com>...
> I'm working with optimization toolbox using the 'fmincon' solver . The fmincon solver works to minimze a function. There's no inbuilt function to maximize a function.
>

So? Minimizing the negative is sufficient.


> The problem that I'm encountering is that for instance
> when I need to maximize f(x)= x1 + x2
> s.t x1+x2<=2 where x1 and x2>=0
>
> I ain't able to do that. I'm setting the function as
>
> > > function f=objfun(x)
> > > f= x1+x2;
> > > f=-z;

z is undefined. I doubt it will return anything (except
an error) given the code you have shown.

But, you did not say you got an error. So your code
is apparently not what you have shown.

>
> Instead of getting the values as x1=1 and x2=1 , it it displaying x1=x2=0 and f(x)=0 as it it minimizing the function. How should I modify the function or the conditions in the solver to maximize the function?? I would highly appreciate if someone could help me.
>

You have not yet shown us correctly what you
have tried. So how will I know what you have
done wrong?

How for example, did you try to set the
inequality constraint? Did you try?

Did you read the help or the doc for fmincon?
It gives some clear examples of how to use
fmincon.

John

Tags for this Thread

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.

Contact us