MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: FindMinimum

pratip chakraborty wrote:

> I am new in Mathematica and have a problem with "FindMinimum". I want to
> minimize a multi variable numerical function it looks like:
> MinFun[{a1,a2,...,ak}]
> I also have a code for the gradient computation so I want to  use the
> gradient option of FindMinimum and supply my gradient function which looks
> like:
> GradFun[{a1,a2,...,ak}]
> Now my problem is in the fact that FindMinimum accepts the cost function and
> the supplied gradient if and only if they are of the form:
> MinFun[a1,a2,...,ak]
> GradFun[a1,a2,...,ak]
> But I can not every time write a cost function like:
> MinFun[a1_?NumericQ,a2_?NumericQ,...,ak_?NumericQ]
> GradFun[a1_?NumericQ,a2_?NumericQ,...,ak_?NumericQ]
> because my k can vary among the set of positive integers so I defined my
> cost function with modified test for the argument as
> MinFun[a_?(VectorQ[#,NumericQ]&)]
> GradFun[a_?(VectorQ[#,NumericQ]&)]
> But I dont know how to define a function
> f[a1_?NumericQ,...,a2000_?NumericQ]:=Block[
> {..},
> ....
> ]
> other than writing so many arguments manualy.
> I tried a wraper like
> g[{a_}]:=f[a]
> But an error occurs saying the shapes dont match.

You must have some other error(s) in your code since FindMinimum handles 
very well functions that have vectors as arguments. For instance,

     ClearAll[MinFun, GradFun]

     MinFun[a_?(VectorQ[#, NumericQ] &)] :=
       Sin[a[[1]] a[[2]]]

     GradFun[a_?(VectorQ[#, NumericQ] &)] :=
       {a[[2]] Cos[a[[1]] a[[2]]],
        a[[1]] Cos[a[[1]] a[[2]]]}

     FindMinimum[MinFun[{x, y}], {x, y},
       Gradient -> GradFun[{x, y}], Method -> "Newton"]

     (* For comparison. *)
     FindMinimum[Sin[x y], {x, y},
       Gradient -> {y Cos[x y], x Cos[x y]}, Method -> "Newton"]

     {-1., {x -> -1.60711, y -> 0.977402}}

     {-1., {x -> -1.60711, y -> 0.977402}}

-- Jean-Marc

  • Prev by Date: Re: How to use package without manually evaluating?
  • Next by Date: Re: Re: What does FullForm[ ] actually do?
  • Previous by thread: Re: FindMinimum
  • Next by thread: How to do a negative pattern?