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:
minimizing a dynamic function

Subject: minimizing a dynamic function

From: Dominik Ballreich

Date: 15 Aug, 2010 14:14:03

Message: 1 of 17

Hi,


I wonder if it's possible to minimze a changing function.

Example:

The basic form of the function is : x(1) + ...+x(i)/ x(1)^2 +...+x(i)^2
I would like the write a mfile, where the function is controlled by a constant i.

If i =3, the mfile automatically minimizes the function x(1) + x(2) +x(3)/ x(1)^2 + x(2)^2+x(3)^2

If i =1, the mfile automatically minimizes the function x(1)/ x(1)^2

If i =5, the mfile automatically minimizes the function x(1) + x(2) +x(3)+x(4)+x(5)/ x(1)^2 + x(2)^2+x(3)^2+x(4)^2+x(5)^2

And so on. "i" can be any number.


It tried to write two function loops. One for the numerator (f) and one for the denominator (g). The last loop function (g) combines the two to the function from above.


f=@(x)0;
for i=1:2
     f=@(x) f(x)+x(i);
end


%----------------------------------------------------------

g=@(x)0;
for i=1:2
     g=@(x) ((g(x))+x(i)^2);
end

%---------------------------------------------------------

h=@(x) f(x)/g(x);





In the example I've chosen "i" is 2. So the function is:

x(1) + x(2) / x(1)^2 + x(2)^2

If I paste this code in the command window and type h([1,2]) the outcome is 0.6. Right

If is set i=3, paste the code in the command window and type h([1,3,8]) the outcome is 0.1622. Fine.

So that shows me, that the combination of two functions works and that mathlab has the right function in it's internal memory.

My problem is, that I would to assign the function h to fmincon.

[x,fval,exitflag]=fmincon(@h,x0,[],[],[],[],[],[])

So I could minimzie every size of the decribed function just by defining i.

But Matlab doesn't allow that.

Maybe someone can help me.


Thank you very much,


Dominik/Germany

Subject: minimizing a dynamic function

From: John D'Errico

Date: 15 Aug, 2010 15:35:04

Message: 2 of 17

"Dominik Ballreich" <dominik.ballreich@gmail.com> wrote in message <i48snb$fjt$1@fred.mathworks.com>...
> Hi,
>
>
> I wonder if it's possible to minimze a changing function.
>
> Example:
>
> The basic form of the function is : x(1) + ...+x(i)/ x(1)^2 +...+x(i)^2
> I would like the write a mfile, where the function is controlled by a constant i.
>
> If i =3, the mfile automatically minimizes the function x(1) + x(2) +x(3)/ x(1)^2 + x(2)^2+x(3)^2
>
> If i =1, the mfile automatically minimizes the function x(1)/ x(1)^2
>
> If i =5, the mfile automatically minimizes the function x(1) + x(2) +x(3)+x(4)+x(5)/ x(1)^2 + x(2)^2+x(3)^2+x(4)^2+x(5)^2
>
> And so on. "i" can be any number.
>
>
> It tried to write two function loops. One for the numerator (f) and one for the denominator (g). The last loop function (g) combines the two to the function from above.
>
>
> f=@(x)0;
> for i=1:2
> f=@(x) f(x)+x(i);
> end
>
>
> %----------------------------------------------------------
>
> g=@(x)0;
> for i=1:2
> g=@(x) ((g(x))+x(i)^2);
> end
>
> %---------------------------------------------------------
>
> h=@(x) f(x)/g(x);
>
>
>
>
>
> In the example I've chosen "i" is 2. So the function is:
>
> x(1) + x(2) / x(1)^2 + x(2)^2
>
> If I paste this code in the command window and type h([1,2]) the outcome is 0.6. Right
>
> If is set i=3, paste the code in the command window and type h([1,3,8]) the outcome is 0.1622. Fine.
>
> So that shows me, that the combination of two functions works and that mathlab has the right function in it's internal memory.
>
> My problem is, that I would to assign the function h to fmincon.
>
> [x,fval,exitflag]=fmincon(@h,x0,[],[],[],[],[],[])
>
> So I could minimzie every size of the decribed function just by defining i.
>
> But Matlab doesn't allow that.

Yes, it does. You simply don't know how to do it.

If i changes with iteration, or as a function of the
parameters, then no, it is not possible, as then the
function will be discontinuous, or at best with a
derivative singularity.

If i is fixed at the beginning of the optimization,
then there is no problem. Simply pass in the
parameter i. Learn to use function handles.

John

Subject: minimizing a dynamic function

From: Dominik Ballreich

Date: 15 Aug, 2010 15:52:04

Message: 3 of 17

Hi John,


that you very much for the answer. It's true. I really don't know how to use function handles.

I don't know where to pass in the parameter i. It's confusing to me.

Subject: minimizing a dynamic function

From: Walter Roberson

Date: 15 Aug, 2010 16:06:37

Message: 4 of 17

Dominik Ballreich wrote:

> The basic form of the function is : x(1) + ...+x(i)/ x(1)^2 +...+x(i)^2

That disagrees with what you show later. What you show later is

(x(1) + ... + x(i)) / (x(1)^2 +...+x(i)^2)

Subject: minimizing a dynamic function

From: Dominik Ballreich

Date: 15 Aug, 2010 16:08:03

Message: 5 of 17

This is the whole code:


function fullexample
[x,fval,exitflag]=fmincon(@x,x0,[],[],[],[],[],[])
%---------------------------------------------------------




%function f = zaehler(x);



f=@(x)0;
for i=1:3
     f=@(x) f(x)+x(i);
end


%----------------------------------------------------------

%function g = nenner(x);

g=@(x)0;
for i=1:3
     g=@(x) ((g(x))+x(i)^2);
end

%---------------------------------------------------------


function h =gesamtfunktion (x);

h=@(x) f(x)/g(x);



I want Matlab to construct the function by constructing it with the parameter "i".

AFTER that I want Matlab to optimizie the function "gesamtfunktion".


Doesn't work at all (-:

I have to confess that I'm really confused with the syntax at the moment.
I've also tried out many many possible solutions mit one of them has worked so far.


Tnak you very much,


Dominik

Subject: minimizing a dynamic function

From: Dominik Ballreich

Date: 15 Aug, 2010 16:15:06

Message: 6 of 17

Mistake, you ar right.
> >

The basic form of the function is : (x(1) + ...+x(i))/ (x(1)^2 +...+x(i)^2)
>
>

Subject: minimizing a dynamic function

From: Dominik Ballreich

Date: 15 Aug, 2010 16:22:04

Message: 7 of 17

"I've also tried out many many possible solutions mit one of them has worked so far."


Means

"I've also tried out many many possible solutions but non of them has worked so far"


(-:

Subject: minimizing a dynamic function

From: John D'Errico

Date: 15 Aug, 2010 16:23:04

Message: 8 of 17

"Dominik Ballreich" <dominik.ballreich@gmail.com> wrote in message <i492f4$lj5$1@fred.mathworks.com>...
> Hi John,
>
>
> that you very much for the answer. It's true. I really don't know how to use function handles.
>
> I don't know where to pass in the parameter i. It's confusing to me.

So learn. Maybe start here.

   doc function_handle

Or try something.

  fun = @(x,k) sin(x+k);

Can we minimize this function for k = 2?

[x,fval] = fminbnd(@(x) fun(x,2),0,10)
x =
          2.71238413719389

fval =
        -0.999999999988272

Or, try this.

  funlist = {@sin, @cos};
  i = 1;
  [x,fval ] = fminbnd(funlist{i},0,10)

x =
          4.71238236854635
fval =
        -0.999999999978142


  i = 2;
  [x,fval ] = fminbnd(funlist{i},0,10)
x =
          3.14159317188068
fval =
        -0.999999999999866

Of course, you could also trivially pass in i to a function
that would then perhaps use a switch/case to switch
between a choice of functions.

John

Subject: minimizing a dynamic function

From: Dominik Ballreich

Date: 15 Aug, 2010 16:47:04

Message: 9 of 17

Thank you, John.

Now I've tried this:

function fullexample
j=2;
x0 = (ones(j,1)*0.5)
lb = [0.1;0.1];
ub=[1;1];
[x,fval,exitflag]=fmincon(@(x) zaehler(x,j),x0,[],[],[],[],lb,ub)
%---------------------------------------------------------

function f = zaehler(j);

f=@(x,i)0;
for i=1:j
     f=@(x,i) f(x)+x(i);
end



Of course the minimum for j=2 is at [0.1,0.1] but that's not important since this function is only used for testing.




For j=3 the function is x(1)+x(2)+x(3) and so on.
For j=3 the minimum is at [0.1,0.1,0.1]

But I'm still not able to pass it to fmincon.

Your examples were helpful but I don't think that they exactly match my problem. My parameter "i" controlls the number of the variables x(i)

Thank you very much,


Dominik

Subject: minimizing a dynamic function

From: Roger Stafford

Date: 15 Aug, 2010 16:54:03

Message: 10 of 17

"Dominik Ballreich" <dominik.ballreich@gmail.com> wrote in message <i48snb$fjt$1@fred.mathworks.com>...
> Hi,
>
>
> I wonder if it's possible to minimze a changing function.
>
> Example:
>
> The basic form of the function is : x(1) + ...+x(i)/ x(1)^2 +...+x(i)^2
> I would like the write a mfile, where the function is controlled by a constant i.
>
> If i =3, the mfile automatically minimizes the function x(1) + x(2) +x(3)/ x(1)^2 + x(2)^2+x(3)^2
>
> If i =1, the mfile automatically minimizes the function x(1)/ x(1)^2
>
> If i =5, the mfile automatically minimizes the function x(1) + x(2) +x(3)+x(4)+x(5)/ x(1)^2 + x(2)^2+x(3)^2+x(4)^2+x(5)^2
>
> And so on. "i" can be any number.
>
>
> It tried to write two function loops. One for the numerator (f) and one for the denominator (g). The last loop function (g) combines the two to the function from above.
>
>
> f=@(x)0;
> for i=1:2
> f=@(x) f(x)+x(i);
> end
>
>
> %----------------------------------------------------------
>
> g=@(x)0;
> for i=1:2
> g=@(x) ((g(x))+x(i)^2);
> end
>
> %---------------------------------------------------------
>
> h=@(x) f(x)/g(x);
>
>
>
>
>
> In the example I've chosen "i" is 2. So the function is:
>
> x(1) + x(2) / x(1)^2 + x(2)^2
>
> If I paste this code in the command window and type h([1,2]) the outcome is 0.6. Right
>
> If is set i=3, paste the code in the command window and type h([1,3,8]) the outcome is 0.1622. Fine.
>
> So that shows me, that the combination of two functions works and that mathlab has the right function in it's internal memory.
>
> My problem is, that I would to assign the function h to fmincon.
>
> [x,fval,exitflag]=fmincon(@h,x0,[],[],[],[],[],[])
>
> So I could minimzie every size of the decribed function just by defining i.
>
> But Matlab doesn't allow that.
>
> Maybe someone can help me.
>
>
> Thank you very much,
>
>
> Dominik/Germany
- - - - - - - - - -
  Perhaps this is not relevant to your learning how to call on fmincon, but I would like to point out that without any constraints the minimum can easily be seen to be minus infinity, or in other words there is no finite minimum. Just let the x quantities all be very tiny and negative. The numerator will also be tiny and negative, but the denominator will be positive and very much tinier. There is no limit to how large and negative the fraction would be. If you constrain x values to be non-negative, then zero is the minimum as you can see by letting them be very large so that the denominator is even larger.

Roger Stafford

Subject: minimizing a dynamic function

From: Dominik Ballreich

Date: 15 Aug, 2010 16:58:03

Message: 11 of 17

Sorry...

My parameter "j" controlls the number of the variables x(i)

Subject: minimizing a dynamic function

From: Dominik Ballreich

Date: 15 Aug, 2010 17:03:03

Message: 12 of 17

Thank you very much for your answer.

I know that I have to define constraints. I didn't post them because they are not important to my "fmincon" problem (-:

Subject: minimizing a dynamic function

From: Steven_Lord

Date: 16 Aug, 2010 13:53:29

Message: 13 of 17



"Dominik Ballreich" <dominik.ballreich@gmail.com> wrote in message
news:i495m8$g5g$1@fred.mathworks.com...
> Thank you, John.
>
> Now I've tried this:
>
> function fullexample
> j=2;
> x0 = (ones(j,1)*0.5)
> lb = [0.1;0.1];
> ub=[1;1];
> [x,fval,exitflag]=fmincon(@(x) zaehler(x,j),x0,[],[],[],[],lb,ub)

j is always numel(x) so you don't actually need to pass it into your zaehler
function. If you need it, you can compute it inside zaehler.

> %---------------------------------------------------------
>
> function f = zaehler(j);
>
> f=@(x,i)0;
> for i=1:j
> f=@(x,i) f(x)+x(i);
> end

What does FMINCON expect the objective function you pass into it to return?
Does it expect that function to return a function handle, or does it expect
that function to return a numeric value, which is the value of the function
you want to optimize at the point given by the input? The HELP text
indicates what it expects, if you're not certain of the answer to this
question.

For this simple function, your zaehler function should be:

function f = zaehler(x)
f = sum(x);

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: minimizing a dynamic function

From: Dominik Ballreich

Date: 16 Aug, 2010 16:05:28

Message: 14 of 17

Thank you very much, Steven


That helped a lot!

Subject: minimizing a dynamic function

From: Dominik Ballreich

Date: 16 Aug, 2010 20:45:23

Message: 15 of 17




Just a little question:



The following code works ALMOST


j=2;
x0 = 0.5*ones(j,1); %starting vektor [0.5;0.5]
lb = zeros(j,1); %lower boundaries [0;0];
ub= ones(j,1); %upper boundaries [1;1];
Aeq= ones(1,j);
beq= 1;

f=@(x)0;
for i=1:j
     f=@(x) f(x)+x(i);
end


%----------------------------------------------------------

g=@(x)0;
for i=1:j
     g=@(x) (g(x)+x(i)^2);
end

%---------------------------------------------------------




h=@(x) f(x)/g(x);

[x,fval,exitflag]=fmincon(h,x0,[],[],Aeq,beq,lb,ub)

 



Because of j=2 Matlab should find the Minumum

of

(x(1)+x(2))/(x(1)^2+x(2)^2)

Constraints
0<=x(1)<=1 und 0<=x(2)<=1
x(1)+x(2)=1

Start=[0.5,0.5]




 

The outcome is:

x =

    0.5000
    0.5000


fval =

    2.0000


exitflag =

     1





This can't be the right solution.


Even x(1)=0.1 , x(2)=0.9

leads to a smaller value


What is th eproblem there?

Thank you very much,


Dominik

Subject: minimizing a dynamic function

From: Steven_Lord

Date: 17 Aug, 2010 01:50:32

Message: 16 of 17



"Dominik Ballreich" <dominik.ballreich@gmail.com> wrote in message
news:i4c813$clq$1@fred.mathworks.com...
>
>
>
> Just a little question:
>
>
>
> The following code works ALMOST
>
>
> j=2;
> x0 = 0.5*ones(j,1); %starting vektor [0.5;0.5]
> lb = zeros(j,1); %lower boundaries [0;0];
> ub= ones(j,1); %upper boundaries [1;1]; Aeq= ones(1,j); beq= 1;
> f=@(x)0;
> for i=1:j
> f=@(x) f(x)+x(i);

Don't do this. Instead write a function that accepts an input x and
performs the computations on that vector.

f = @sum;

> %----------------------------------------------------------
>
> g=@(x)0;
> for i=1:j
> g=@(x) (g(x)+x(i)^2);
> end

g = @(x) sum(x.^2);

> h=@(x) f(x)/g(x);

This is okay. Since both f and g return scalars, this will also return a
scalar.

*snip*

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: minimizing a dynamic function

From: Dominik Ballreich

Date: 17 Aug, 2010 02:16:04

Message: 17 of 17

Hi Steven,

that you for the answer. I know that my code doesn't look elegant (-:

But the matlab accepts the function in the way I programmed it. I testet it with many test vektors.

Furthermore: If I try to find the minimum of the function without Aeq and beq
(only 0<=x(1)<=1 and 0<=x(2)<=1) ,

Matlab computes the right solution.

j=2;
x0 = 0.5*ones(j,1); %starting vektor [0.5;0.5]
lb = zeros(j,1); %lower boundaries [0;0];
ub= ones(j,1); %upper boundaries [1;1];


f=@(x)0;
for i=1:j
     f=@(x) f(x)+x(i);
end


%----------------------------------------------------------

g=@(x)0;
for i=1:j
     g=@(x) (g(x)+x(i)^2);
end

%---------------------------------------------------------




h=@(x) f(x)/g(x);

[x,fval,exitflag]=fmincon(h,x0,[],[],[],[],lb,ub)



Solution:


x =

     1
     1


fval =

     1


exitflag =

     1







Maybe the linear constraints are the problem. I don't know, it's confusing to me.
I really need to work with constraints.

Thank you,


Dominik

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