How to solve geometric (nonlinear) least-square problem?




On Tue, 26 May 2009, Clemens Koller wrote:

 
< ... up to here, everything seems clear to me.
< > (%i7) mse : mse[1,1];
< But what is this assignment supposed to do?

I use scalarmatrixp:false, so that a 1x1 matrix remains such and is not
converted to a scalar. This line does this manually, by retrieving the
relevant entry. You can see this in %o6-%o7.

< 
< > Now, substitute for the points on the vertices of the 4-gons. After
< > which
< 
< That's what I did, I think...
< 
< > load(lbfgs)$
< > lbfgs(mse,'[w,tx,ty],[0,0,0],1e-4,[1,3]);

I have copied your example, and here is what I get (I have changed
scalarmatrixp at the outset to avoid confusion). Please send the output
from your attempt if you still have problems.

(%i1) scalarmatrixp;
(%o1) false
(%i2) scalarmatrixp:true;
(%o2) true
(%i3) A: matrix([cos(w),-sin(w),tx],[sin(w),cos(w),ty],[0,0,1]);
(%o3) matrix([cos(w),-sin(w),tx],[sin(w),cos(w),ty],[0,0,1])
(%i4) P:
[matrix([99.4656],[99.9805],[1]),matrix([101.173],[-99.3658],[1]),matrix([-97.1562],[-99.197],[1]),matrix([-97.9357],[100.024],[1])];
(%o4) [matrix([99.4656],[99.9805],[1]),matrix([101.173],[-99.3658],[1]),
       matrix([-97.1562],[-99.197],[1]),matrix([-97.9357],[100.024],[1])]
(%i5) Q:
[matrix([100],[100],[1]),matrix([100],[-100],[1]),matrix([-100],[-100],[1]),matrix([-100],[100],[1])];
(%o5) [matrix([100],[100],[1]),matrix([100],[-100],[1]),
       matrix([-100],[-100],[1]),matrix([-100],[100],[1])]
(%i6) R : makelist(A.P[i]-Q[i],i,1,4);
(%o6) [matrix([-99.9805*sin(w)+99.4656*cos(w)+tx-100],
              [99.4656*sin(w)+99.9805*cos(w)+ty-100],[0.0]),
       matrix([99.3658*sin(w)+101.173*cos(w)+tx-100],
              [101.173*sin(w)-99.3658*cos(w)+ty+100],[0.0]),
       matrix([99.197*sin(w)-97.1562*cos(w)+tx+100],
              [-97.1562*sin(w)-99.197*cos(w)+ty+100],[0.0]),
       matrix([-100.024*sin(w)-97.9357*cos(w)+tx+100],
              [-97.9357*sin(w)+100.024*cos(w)+ty-100],[0.0])]
(%i7) mse : sum( transpose(R[i]).R[i],i,1,4);
(%o7) (101.173*sin(w)-99.3658*cos(w)+ty+100)^2
       +(99.4656*sin(w)+99.9805*cos(w)+ty-100)^2
       +(99.3658*sin(w)+101.173*cos(w)+tx-100)^2
       +(99.197*sin(w)-97.1562*cos(w)+tx+100)^2
       +(-97.1562*sin(w)-99.197*cos(w)+ty+100)^2
       +(-97.9357*sin(w)+100.024*cos(w)+ty-100)^2
       +(-99.9805*sin(w)+99.4656*cos(w)+tx-100)^2
       +(-100.024*sin(w)-97.9357*cos(w)+tx+100)^2
(%i8) load(lbfgs);
(%o8)
"/knoppix-home/work/maxima/maxima-5.18.1-clisp/share/lbfgs/lbfgs.mac"
(%i9) lbfgs(mse,[w,tx,ty],[0,0,0],1e-4,[1,1,3]);
*************************************************
  N=    3   NUMBER OF CORRECTIONS=25
       INITIAL VALUES
 F=  1.505802018000004D+01   GNORM=  4.550643733793317D+02
 VECTOR X=
   0.000000000000000D+00   0.000000000000000D+00   0.000000000000000D+00
 GRADIENT VECTOR G=
   4.549200000000043D+02   1.109340000000000D+01   2.883400000000023D+00
*************************************************

   I  NFN     FUNC                    GNORM                   STEPLENGTH

   1    3     1.440897443730085D+01   3.336997629485338D+01
5.861192757949555D-06
   2    4     1.440499841585286D+01   1.146448653587576D+01
1.000000000000000D+00
   3    5     1.440400802048269D+01   1.229168107012059D+01
1.000000000000000D+00
   4    6     1.439099600937044D+01   4.178223298338200D+01
1.000000000000000D+00
   5    7     1.436703191450251D+01   8.072895312748928D+01
1.000000000000000D+00
   6    8     1.429466167192572D+01   1.548701097701052D+02
1.000000000000000D+00
   7    9     1.411867424819032D+01   2.651150173732902D+02
1.000000000000000D+00
   8   10     1.367108607478545D+01   4.332531548416378D+02
1.000000000000000D+00
   9   11     1.264892396628666D+01   6.476696849321535D+02
1.000000000000000D+00
  10   12     1.069043078816495D+01   8.192271557606652D+02
1.000000000000000D+00
  11   13     8.184018892433315D+00   7.324475166470045D+02
1.000000000000000D+00
  12   14     6.594417492913759D+00   3.518233855105166D+02
1.000000000000000D+00
  13   15     6.223081283160614D+00   6.455447008776902D+01
1.000000000000000D+00
  14   16     6.196116002861831D+00   6.218342457068777D-01
1.000000000000000D+00
  15   17     6.195561166173844D+00   6.358372568501851D-01
1.000000000000000D+00
  16   18     6.195558138274431D+00   3.659409639802011D-02
1.000000000000000D+00
  17   19     6.195558134019704D+00   5.426025776826688D-04
1.000000000000000D+00
  18   20     6.195558134018182D+00   7.498033028912820D-07
1.000000000000000D+00

 THE MINIMIZATION TERMINATED WITHOUT DETECTING ERRORS.
 IFLAG = 0
(%o9) [w = -.002863653630305682,tx = -1.38770143906069,
        ty = -.3564525711588058]

Leo

-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.