Google Answers Logo
View Question
 
Q: translate from fortran to matlab program ( Answered 3 out of 5 stars,   2 Comments )
Question  
Subject: translate from fortran to matlab program
Category: Computers > Programming
Asked by: davidyalin-ga
List Price: $40.00
Posted: 21 Aug 2005 10:36 PDT
Expires: 20 Sep 2005 10:36 PDT
Question ID: 558379
translate the following fortran program (BFGS algorithm) to matlab program
http://www.mpri.lsu.edu/textbook/Table6-4.htm

Clarification of Question by davidyalin-ga on 21 Aug 2005 23:53 PDT
or sent me BFGS algorithm in matlab

Request for Question Clarification by studboy-ga on 22 Aug 2005 14:20 PDT
Hi

Please let me know if this will suffice (which I believe it does):

http://www2.imm.dtu.dk/~hbn/Software/ucminf.m

Request for Question Clarification by studboy-ga on 22 Aug 2005 14:20 PDT
If it does let me know and I will post a formal answer to close the
question.  Thanks!

Request for Question Clarification by studboy-ga on 23 Aug 2005 17:00 PDT
Hi David

Any chance to try it out?

Thanks
studgoy-ga

Clarification of Question by davidyalin-ga on 23 Aug 2005 23:17 PDT
Hi

i tried to run the program you gave me but it shows me error on line 120.

can you give me example of how to run this program (maybe i insert wrong
number to the function)?

Thanks Gilad

Request for Question Clarification by studboy-ga on 24 Aug 2005 17:07 PDT
Hi David

The program is meant to give an idea and not be run "as it"--
the concept should be there, and implemented accordingly as opposed to
copy-and-paste...  I hope you know what I mean :)

First of all, is this what you're looking for?

What version of Matlab are you using?
Matlab's generally pretty straightforward in their error messages.
What's the message?

Again, I can post a formal answer, but you must tell me whether I'm
*on the right track*

Request for Question Clarification by studboy-ga on 24 Aug 2005 17:09 PDT
Hi Gilad,

Consider the function 
   
  function [F,g,H] = rosenbrock(x,p) 
f1 = (x(2) - x(1)^2); f2 = 1 - x(1); 
F = 50*f1^2 + .5*f2^2 + 0; 
if nargout > 1 
   g = [-200*x(1)*f1-f2; 100*f1]; 
   if nargout > 2 
     H = [1-200*(f1 - 2*x(1)^2) -200*x(1) 
         -200*x(1) 100]; 
   end 
end 
   
With   p = 0   this is an implementation of the famous Rosenbrock
function. In the program
   
  x0 = [-1.2;1];   p = 0; 
[x1, ii1] = dampnewton(@rosenbrock,x0,[],p)
[x2, ii2] = ucminf(@rosenbrock,x0,[],[],p) 
   
we use the default values for opts and an empty D0 in ucminf. We get
the following results,
  
  x1 = 0.9999979771
0.9999959177     x2 = 1.000000319
1.000000633
  
 
  ii1 =
ii2 =   2.11e-12
5.25e-14   5.29e-06
1.42e-06   3.84e-04
9.32e-06   1.82e-06
34     34
38   1
1

Request for Question Clarification by studboy-ga on 24 Aug 2005 17:11 PDT
Typical calls are 
    [X, info] = ucminf(fun, x0) 
    [X, info] = ucminf(fun, x0, opts) 
    [X, info] = ucminf(fun, x0, opts, D0, p1,p2,...) 
    [X, info, perf] = ucminf(.....)
    [X, info, perf, D] = ucminf(.....)

Request for Question Clarification by studboy-ga on 24 Aug 2005 17:11 PDT
Again, let me know whether we're on the right track here.

Clarification of Question by davidyalin-ga on 24 Aug 2005 20:48 PDT
Hi

1) the program seem to be the one that i am looking for, but i want
program that will run and not just to get the idea.
2)for the second answer - do i have to run the function that you gave
me, before i run the BFGS program?

Thanks Gilad

Request for Question Clarification by studboy-ga on 25 Aug 2005 00:01 PDT
Hi Gilad

The purpoose of BFGS is to minimize an unconstrained nonlinear
multivariable function.

The "function" I posted is an example function, it can be "any"
function, ie, any function that you want to optimize.  The BFGS
algorith's job is to optimize the function, that's all, no more, no
less.  In order for it to do that, you need to provide two things to
it at the minimum:
1) A function (obviously)
2) An initial "guess" x0 (very much like Newton's method, if you've heard of that)

First of all, is this an assignment?  If it's an assignment then you
need to tell me the full details.  Furthermore, the "concept" must be
fully grasped before writing any program.

Secondly, I did try ucminf.m on Matlab 5 and 6 and it loads and works
without any problems.  Again, if this is an assignment, obviously I
need to know the exact version of Matlab you're using, as well as the
platform, etc.

So, in summary:

1) You've a function you want to optimize, f()
2) You run BFGS to get the optimized function, f'(), by doing:

   f'() = BFGS(f(), initial guess x0)

That's the main concept.  The program takes just two inputs:
a) function f() b) initial guess x0
and produces optimized f'()

Please let me know whether this is clear and how we may proceed.

Thanks
studboy-ga

Clarification of Question by davidyalin-ga on 25 Aug 2005 02:38 PDT
Hi

this is clear, but it still show me error.
the matlab i use is 6.5 and it show me the following error:

??? Error using ==> feval
Argument must contain a string or function_handle.

Error in ==> D:\My Documents\TauMaster\matlab\ucminf.m (check)
On line 120  ==>   [F g] = feval(fun,x,par);

Error in ==> D:\My Documents\TauMaster\matlab\ucminf.m
On line 46  ==>   [x n F g] = check(fun,par,x0,opts);

Request for Question Clarification by studboy-ga on 25 Aug 2005 17:57 PDT
Hi Gilad

Did you declare a function before calling the program?
You must declare the function you want to mimimize before calling the program.

For example, if you have a "rosenbrock" function, first declare it:

-----
function [F,g,H] = rosenbrock(x,p) 
f1 = (x(2) - x(1)^2); f2 = 1 - x(1); 
F = 50*f1^2 + .5*f2^2 + 0; 
if nargout > 1 
   g = [-200*x(1)*f1-f2; 100*f1]; 
   if nargout > 2 
     H = [1-200*(f1 - 2*x(1)^2) -200*x(1) 
         -200*x(1) 100]; 
   end 
end 
-----   

Then you call BFGS on rosenbrock with a guess x0:
   
  x0 = [-1.2;1];   p = 0; 

[x2, ii2] = ucminf(@rosenbrock,x0,[],[],p) 

The answers will be stored in [x2, ii2]

Can you tell me:

what's your function and how you declare it?

Clarification of Question by davidyalin-ga on 26 Aug 2005 23:02 PDT
eventually, i would like to minimize the Group Delay of digital filter
compare to desirable Group delay in few frequencies
{L=sum(GD_Filer[freq]-GD_desire[freq])^p}.
before i start to deal with this issue, i would like to verify that
the function is running.
i declare on the funcion you suggest. i defined: x=[3;7]; p=0;
i ran:  rosenbrock(x,p)
then i defined: x0=[-1.2,1];
and ran: ucminf(@rosenbrock,x0,[],[],p)
i get the following error:
??? Index exceeds matrix dimensions.

Error in ==> D:\Gilad\TAU\Thesis\matlab\fletcher-powell algorithm\rosenbrock.m
On line 2  ==> f1 = (x(2) - x(1)^2); f2 = 1 - x(1); 

Error in ==> D:\Gilad\TAU\Thesis\matlab\fletcher-powell algorithm\ucminf.m (check)
On line 120  ==>   [F g] = feval(fun,x,par);

Error in ==> D:\Gilad\TAU\Thesis\matlab\fletcher-powell algorithm\ucminf.m
On line 46  ==>   [x n F g] = check(fun,par,x0,opts);

Request for Question Clarification by studboy-ga on 29 Aug 2005 13:20 PDT
Hi Gilad

Did you declare x (the matrix) before used?

Before use, do:

x(2) = 0

BTW, you don't need to use the rosenbrock function--
that's just an example.  Why not go ahead and plug in your 
actual group delay function?  It's probably simpler...

Clarification of Question by davidyalin-ga on 29 Aug 2005 21:11 PDT
Hi

i would like to see that the program is running, before i start to
deal with the Group Delay function.
i declare: x=[3;0];
i declare: p=0;
i ran: rosenbrock(x,p)
i declare: x0=[-1.2,1];
i ran: ucminf(@rosenbrock,x0,[],[],p)

and i still got the same error

Request for Question Clarification by studboy-ga on 30 Aug 2005 17:01 PDT
Hi Gilad


1) help ucminf

2) ucminf(@rosenbrock,[],x0,[0,0,0,0])

Request for Question Clarification by studboy-ga on 31 Aug 2005 14:09 PDT
Hi Gilad

So I assume everything's cool?

Thanks
studboy-ga

Clarification of Question by davidyalin-ga on 31 Aug 2005 21:01 PDT
yes
thanks
Answer  
Subject: Re: translate from fortran to matlab program
Answered By: studboy-ga on 31 Aug 2005 21:36 PDT
Rated:3 out of 5 stars
 
Hi Gilad

Sorry I posted the incorrect calling arguments initially.  I'm glad
everything work out in the end.

The algorithm, formally, is as follows:

function  [X, info, perf, D] = ucminf(fun,par, x0, opts, D0)
%UCMINF  BFGS method for unconstrained nonlinear optimization:
% Find  xm = argmin{f(x)} , where  x  is an n-vector and the scalar
% function  F  with gradient  g  (with elements  g(i) = DF/Dx_i )
% must be given by a MATLAB function with with declaration
%           function  [F, g] = fun(x, par)
% par  holds parameters of the function.  It may be dummy.  
% 
% Call:  [X, info {, perf {, D}}] = ucminf(fun,par, x0, opts {,D0})
%
% Input parameters
% fun  :  String with the name of the function.
% par  :  Parameters of the function.  May be empty.
% x0   :  Starting guess for  x .
% opts :  Vector with 4 elements:
%         opts(1) :  Expected length of initial step
%         opts(2:4)  used in stopping criteria:
%             ||g||_inf <= opts(2)                     or 
%             ||dx||_2 <= opts(3)*(opts(3) + ||x||_2)  or
%             no. of function evaluations exceeds  opts(4) . 
%         Any illegal element in  opts  is replaced by its
%         default value,  [1  1e-4*||g(x0)||_inf  1e-8  100]
% D0   :  (optional)  If present, then approximate inverse Hessian
%         at  x0 .  Otherwise, D0 := I 
% Output parameters
% X    :  If  perf  is present, then array, holding the iterates
%         columnwise.  Otherwise, computed solution vector.
% info :  Performance information, vector with 6 elements:
%         info(1:3) = final values of [f(x)  ||g||_inf  ||dx||_2] 
%         info(4:5) = no. of iteration steps and evaluations of (F,g)
%         info(6) = 1 :  Stopped by small gradient
%                   2 :  Stopped by small x-step
%                   3 :  Stopped by  opts(4) .
%                   4 :  Stopped by zero step.
% perf :  (optional). If present, then array, holding 
%          perf(1:2,:) = values of  f(x) and ||g||_inf
%          perf(3:5,:) = Line search info:  values of  
%                        alpha, phi'(alpha), no. fct. evals. 
%          perf(6,:)   = trust region radius.
% D    :  (optional). If present, then array holding the 
%         approximate inverse Hessian at  X(:,end) .

%  Hans Bruun Nielsen,  IMM, DTU.  00.12.18 / 02.01.22

  % Check call 
  [x n F g] = check(fun,par,x0,opts);
  if  nargin > 4,  D = checkD(n,D0);  fst = 0;
  else,            D = eye(n);  fst = 1;    end
  %  Finish initialization
  k = 1;   kmax = opts(4);   neval = 1;   ng = norm(g,inf);
  Delta = opts(1);
  Trace = nargout > 2;
  if  Trace
        X = x(:)*ones(1,kmax+1);
        perf = [F; ng; zeros(3,1); Delta]*ones(1,kmax+1);
      end
  found = ng <= opts(2);      
  h = zeros(size(x));  nh = 0;
  ngs = ng * ones(1,3);
   
  while  ~found
    %  Previous values
    xp = x;   gp = g;   Fp = F;   nx = norm(x);
    ngs = [ngs(2:3) ng];
    h = D*(-g(:));   nh = norm(h);   red = 0; 
    if  nh <= opts(3)*(opts(3) + nx),  found = 2;  
    else
      if  fst | nh > Delta  % Scale to ||h|| = Delta
        h = (Delta / nh) * h;   nh = Delta;   
        fst = 0;  red = 1;
      end
      k = k+1;
      %  Line search
      [al  F  g  dval  slrat] = softline(fun,par,x,F,g, h);
      if  al < 1  % Reduce Delta
        Delta = .35 * Delta;
      elseif   red & (slrat > .7)  % Increase Delta
        Delta = 3*Delta;      
      end 
      %  Update  x, neval and ||g||
      x = x + al*h;   neval = neval + dval;  ng = norm(g,inf);
      if  Trace
             X(:,k) = x(:); 
             perf(:,k) = [F; ng; al; dot(h,g); dval; Delta]; end
      h = x - xp;   nh = norm(h);
      if  nh == 0,
        found = 4; 
      else
        y = g - gp;   yh = dot(y,h);
        if  yh > sqrt(eps) * nh * norm(y)
          %  Update  D
          v = D*y(:);   yv = dot(y,v);
          a = (1 + yv/yh)/yh;   w = (a/2)*h(:) - v/yh;
          D = D + w*h' + h*w';
        end  % update D
        %  Check stopping criteria
        thrx = opts(3)*(opts(3) + norm(x));
        if      ng <= opts(2),              found = 1;
        elseif  nh <= thrx,                 found = 2;
        elseif  neval >= kmax,              found = 3; 
%        elseif  neval > 20 & ng > 1.1*max(ngs), found = 5;
        else,   Delta = max(Delta, 2*thrx);  end
      end  
    end  % Nonzero h
  end  % iteration

  %  Set return values
  if  Trace
    X = X(:,1:k);   perf = perf(:,1:k);
  else,  X = x;  end
  info = [F  ng  nh  k-1  neval  found];

% ==========  auxiliary functions  =================================

function  [x,n, F,g, opts] = check(fun,par,x0,opts0)
% Check function call
  x = x0(:);   sx = size(x);   n = max(sx);   
  if  (min(sx) > 1)
      error('x0  should be a vector'), end
  [F g] = feval(fun,x,par);
  sf = size(F);   sg = size(g);
  if  any(sf-1) | ~isreal(F)
      error('F  should be a real valued scalar'), end
  if  (min(sg) ~= 1) | (max(sg) ~= n)
      error('g  should be a vector of the same length as  x'), end
  so = size(opts0);
  if  (min(so) ~= 1) | (max(so) < 4) | any(~isreal(opts0(1:4)))
      error('opts  should be a real valued vector of length 4'), end
  opts = opts0(1:4);   opts = opts(:).';
  i = find(opts <= 0);
  if  length(i)    % Set default values
    d = [1  1e-4*norm(g, inf)  1e-8  100];
    opts(i) = d(i);
  end   
% ----------  end of  check  ---------------------------------------

function  D = checkD(n,D0)
% Check given inverse Hessian
  D = D0;   sD = size(D);
  if  any(sD - n)
      error(sprintf('D  should be a square matrix of size %g',n)), end
  % Check symmetry
  dD = D - D';   ndD = norm(dD(:),inf);
  if  ndD > 10*eps*norm(D(:),inf)
      error('The given D0 is not symmetric'), end
  if  ndD,  D = (D + D')/2; end  % Symmetrize      
  [R p] = chol(D);
  if  p
      error('The given D0 is not positive definite'), end
    
function  [alpha,fn,gn,neval,slrat] = ...
              softline(fun,fpar, x,f,g, h)
% Soft line search:  Find  alpha = argmin_a{f(x+a*h)}
  % Default return values 
  alpha = 0;   fn = f;   gn = g;   neval = 0;  slrat = 1;
  n = length(x);  
  
  % Initial values
  dfi0 = dot(h,gn);  if  dfi0 >= 0,  return, end
  fi0 = f;    slope0 = .05*dfi0;   slopethr = .995*dfi0;
  dfia = dfi0;   stop = 0;   ok = 0;   neval = 0;   b = 1;
  
  while   ~stop
    [fib g] = feval(fun,x+b*h,fpar);  neval = neval + 1;
    dfib = dot(g,h); 
    if  b == 1, slrat = dfib/dfi0; end
    if  fib <= fi0 + slope0*b    % New lower bound
      if  dfib > abs(slopethr),  stop = 1;
      else
        alpha = b;   fn = fib;   gn = g;   dfia = dfib;  
        ok = 1;   slrat = dfib/dfi0;
        if  (neval < 5) & (b < 2) & (dfib < slopethr)
          % Augment right hand end
          b = 2*b;
        else,  stop = 1; end
      end
    else,  stop = 1; end   
  end
  
  stop = ok;  xfd = [alpha fn dfia; b fib dfib; b fib dfib];
  while   ~stop
    c = interpolate(xfd,n);
    [fic g] = feval(fun, x+c*h, fpar);   neval = neval+1;
    xfd(3,:) = [c  fic  dot(g,h)];
    if fic < fi0 + slope0*c    % New lower bound
      xfd(1,:) = xfd(3,:);   ok = 1;
      alpha = c;   fn = fic;   gn = g;  slrat = xfd(3,3)/dfi0;
    else,  xfd(2,:) = xfd(3,:);  ok = 0; end
    % Check stopping criteria
    ok = ok & abs(xfd(3,3)) <= abs(slopethr);
    stop = ok | neval >= 5 | diff(xfd(1:2,1)) <= 0;
  end  % while   
%------------  end of  softline  ------------------------------
  
function  alpha = interpolate(xfd,n);
% Minimizer of parabola given by
% xfd(1:2,1:3) = [a fi(a) fi'(a); b fi(b) dummy]

  a = xfd(1,1);   b = xfd(2,1);   d = b - a;   dfia = xfd(1,3);
  C = diff(xfd(1:2,2)) - d*dfia;
  if C >= 5*n*eps*b    % Minimizer exists
    A = a - .5*dfia*(d^2/C);
    d = 0.1*d;   alpha = min(max(a+d, A), b-d);
  else
    alpha = (a+b)/2;
  end
%------------  end of  interpolate  --------------------------
davidyalin-ga rated this answer:3 out of 5 stars

Comments  
Subject: Re: translate from fortran to matlab program
From: philnj-ga on 22 Aug 2005 11:59 PDT
 
Oh My.  I know FORTRAN and MATLAB, but this is not a simple
translation.  FORTRAN is a procedural language and MATLAB is organized
around arrays.  MATLAB is much more powerful because you can do vector
math very simply.  Good Luck.
Subject: Re: translate from fortran to matlab program
From: davidyalin-ga on 23 Aug 2005 23:10 PDT
 
Hi

i tried to run the program you gave me but it gave me error on line 120.

can you give me example how to use this program? maybe i insert wrong
number to the function

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy