Try out NAG Library functions

Explore NAG maths and stats routines with interactive demos
Function ID
E04NCF
Name
nagf_opt_lsq_lincon_solve_old
Description
Solves linear least squares and convex quadratic programming problems (non-sparse)
Keywords
nonlinear least squares | data fitting | active-set | QP, quadratic programming | convex optimization | least squares
This example minimizes the function 12b-Ax2, where
A=111111111121111200113111-1-1-311141111111131111111211000-1111101111111011111110111223101111022  and  b=1111111111  
subject to the bounds
0x120x22-x320x420x520x620x720x820x92  
and to the general constraints
2.0x1+x2+x3+x4+x5+x6+x7+x8+4x9-x1+2x2+3x3+4x4-2x5+x6+x7+x8+x92.01.0x1-x2+x3-x4+x5+x6+x7+x8+x94.0  
The initial point, which is infeasible, is
x0=1.0,0.5,0.3333,0.25,0.2,0.1667,0.1428,0.125,0.1111T,  
and Fx0=9.4746 (to five figures).
The example data reflects that shown in the "Example" section of the routine documentation. You can change this here to try alternative inputs. The formatting will need to be kept as it is here, otherwise the program is likely to fail to run correctly.

Please note that incompatible data will however cause the example output to display an error message. These error messages are fully explained in the Routine document
    Program e04ncfe

!     E04NCF Example Program Text

!     Mark 26 Release. NAG Copyright 2016.

!     .. Use Statements ..
      Use nag_library, Only: dgemv, e04ncf, e04nef, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter    :: one = 1.0_nag_wp
      Real (Kind=nag_wp), Parameter    :: zero = 0.0_nag_wp
      Integer, Parameter               :: inc1 = 1, nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: obj
      Integer                          :: i, ifail, iter, lda, ldc, liwork,    &
                                          lwork, m, n, nclin, sdc
      Logical                          :: verbose_output
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), b(:), bl(:), bu(:), c(:,:),  &
                                          clamda(:), cvec(:), work(:), x(:)
      Integer, Allocatable             :: istate(:), iwork(:), kx(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: max
!     .. Executable Statements ..
      Write (nout,*) 'E04NCF Example Program Results'

!     Skip heading in data file
      Read (nin,*)

      Read (nin,*) m, n, nclin
      liwork = n
      ldc = max(1,nclin)
      lda = max(1,m)

      If (nclin>0) Then
        sdc = n
      Else
        sdc = 1
      End If

!     This particular example problem is of type LS1, so we allocate
!     A(LDA,N), CVEC(1), B(M) and define LWORK as below

      If (nclin>0) Then
        lwork = 2*n**2 + 9*n + 6*nclin
      Else
        lwork = 9*n
      End If

      Allocate (istate(n+nclin),kx(n),iwork(liwork),c(ldc,sdc),bl(n+nclin),    &
        bu(n+nclin),cvec(1),x(n),a(lda,n),b(m),clamda(n+nclin),work(lwork))

      Read (nin,*)(a(i,1:n),i=1,m)
      Read (nin,*) b(1:m)
      Read (nin,*)(c(i,1:sdc),i=1,nclin)
      Read (nin,*) bl(1:(n+nclin))
      Read (nin,*) bu(1:(n+nclin))
      Read (nin,*) x(1:n)

!     Set this to .True. to cause e04nqf to produce intermediate
!     progress output
      verbose_output = .False.
      If (.Not. verbose_output) Then
!       Switch off intermediate output from e04ncf
        Call e04nef('Nolist')
        Call e04nef('Print level = 0')
      End If

!     Solve the problem

      ifail = -1
      Call e04ncf(m,n,nclin,ldc,lda,c,bl,bu,cvec,istate,kx,x,a,b,iter,obj,     &
        clamda,iwork,liwork,work,lwork,ifail)

      Select Case (ifail)
      Case (0:5,7:)
!       Print variable headers
        Write (nout,99999)

        Do i = 1, n
          Write (nout,99998) i, istate(i), x(i), clamda(i)
        End Do

        If (nclin>0) Then

!         C*x --> work
!         The NAG name equivalent of dgemv is f06paf
          Call dgemv('N',nclin,n,one,c,ldc,x,inc1,zero,work,inc1)

!         Print constraint headers
          Write (nout,99997)

          Do i = 1, nclin
            Write (nout,99996) i, istate(i+n), work(i), clamda(i+n)
          End Do

        End If

        Write (nout,99995) obj
      End Select

99999 Format (/,1X,'Varbl',3X,'Istate',4X,'Value',9X,'Lagr Mult')
99998 Format (1X,'V',2(1X,I3),4X,1P,E14.3,2X,1P,E12.3)
99997 Format (/,1X,'L Con',3X,'Istate',4X,'Value',9X,'Lagr Mult')
99996 Format (1X,'L',2(1X,I3),4X,1P,E14.3,2X,1P,E12.3)
99995 Format (/,1X,'Final objective value = ',1P,E15.3)
    End Program e04ncfe
The NAG Library
The world’s largest collection of robust, documented, tested and maintained numerical algorithms.
Learn more here or contact us for purchasing information