/* nag_lone_fit (e02gac) Example Program. * * Copyright 2001 Numerical Algorithms Group. * * Mark 7, 2001. */ #include #include #include #include #include int main(void) { /* Scalars */ double resid, t, tol; Integer exit_status, i, iter, m, rank, n, nplus2, pda; NagError fail; Nag_OrderType order; /* Arrays */ double *a = 0, *b = 0, *x = 0; #ifdef NAG_COLUMN_MAJOR #define A(I,J) a[(J-1)*pda + I - 1] order = Nag_ColMajor; #else #define A(I,J) a[(I-1)*pda + J - 1] order = Nag_RowMajor; #endif INIT_FAIL(fail); exit_status = 0; Vprintf("nag_lone_fit (e02gac) Example Program Results\n"); /* Skip heading in data file */ Vscanf("%*[^\n] "); n = 3; nplus2 = n + 2; Vscanf("%ld%*[^\n] ", &m); if (m > 0) { /* Allocate memory */ if ( !(a = NAG_ALLOC((m + 2) * nplus2, double)) || !(b = NAG_ALLOC(m, double)) || !(x = NAG_ALLOC(nplus2, double)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } if (order == Nag_ColMajor) pda = m + 2; else pda = nplus2; for (i = 1; i <= m; ++i) { Vscanf("%lf%lf%*[^\n] ", &t, &b[i-1]); A(i, 1) = exp(t); A(i, 2) = exp(-t); A(i, 3) = 1.0; } tol = 0.0; /* nag_lone_fit (e02gac). * L_1-approximation by general linear function */ nag_lone_fit(order, m, a, b, nplus2, tol, x, &resid, &rank, &iter, &fail); if (fail.code == NE_INT || fail.code == NE_INT_2) { Vprintf("Error from nag_lone_fit (e02gac).\n%s\n", fail.message); exit_status = 1; goto END; } else { Vprintf("\n"); Vprintf("resid = %10.2e Rank = %5ld Iterations =" " %5ld\n", resid, rank, iter); Vprintf("\n"); Vprintf("Solution\n"); for (i = 1; i <= n; ++i) Vprintf("%10.4f", x[i-1]); Vprintf("\n"); } } END: if (a) NAG_FREE(a); if (b) NAG_FREE(b); if (x) NAG_FREE(x); return exit_status; }