/* This is the template file for HW4 problem 4. * * To compile, use UNIX command * cc -g -Wall -o hw4_4 hw4_4.c -llapack -lm * This will generate an executable hw4_4. The -g option is optional and * is needed only if you need to debug the program in a debugger such as ddd. % The -Wall option would enable compiler's warning messages. * * Run the program * hw4_4 * would create two MATLAB/Octave files "results_hw4_4a.m" and "results_hw4_4b.m". * * To produce the plot, start octave by issuing UNIX command * octave * Then within octave, issue commands * results_hw4_4a * results_hw4_4b * exit * This will produce an EPS file results_hw4_4[ab].eps. Alternatively, * you can also run Octave in batch mode by issuing the UNIX command * octave -q -f --eval "results_hw4_4a; results_hw4_4ab; exit" */ #include #include #include #include #define N 100 #define MFNAME1 "results_hw4_4a.m" #define MFNAME2 "results_hw4_4b.m" #define EPSFNAME1 "results_hw4_4a.eps" #define EPSFNAME2 "results_hw4_4b.eps" #define TITLE1 "Computer Problem 7.6(a)-(c) -- Interpolate to Square Root" #define TITLE2 "Computer Problem 7.6(d) -- Interpolate to Square Root" /* Function prototypes */ void polyfit8(const double *t, const double *y, double cs[9]); void polyval8(const double p[9], int n, const double* ts, double* yp); void spline(const double *t, const double *y, double pp[8][4]); void ppval(double pp[8][4], const double* t_input, int n, const double* ts, double* ys); void write_plot_script_1(const char *mfname, const char *epsfname, const char *title, const double *t, const double *y, const double *ts, const double *yr, const double *yp, const double * ys, int n); void write_plot_script_2(const char *mfname, const char *epsfname, const char *title, const double *t, const double *y, const double *ts, const double *yr, const double *yp, const double * ys, int n); int solve9x9(double A[9][9], double x[9]); int solve32x32(double A[32][32], double x[32]); int main(int argc, char **argv) { double t_input[] = {0, 1, 4, 9, 16, 25, 36, 49, 64}; double y_input[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; /* cs is an array containing the coefficients of the polynomial in ascending power for monomial basis functions: p(t) = cs(0) + cs(1)*t + cs(2)*t^2 + ... + cs(8)*t^8 */ double cs[9]; /* PP is an 8x4 matrix containing the eight piecewise polynomial coefficients in ascending powers, f(t) = pp[i][0] + pp[i][1]*t + pp[i][2]*t^2 + pp[i][3]*t^3, i is in [0, 7] */ double pp[8][4]; double ts[N]; /* x-value data points */ double yr[N]; /* the y-values by the build-in sqrt function */ double yp[N]; /* the y-values by the interpolation polynomial of degree eight */ double ys[N]; /* the y-values by the interpolation cubic spline function */ int i; double h; /* Computer Problem 7.6(a)-(c) -- Interpolate to Square Root */ /* Compute the corresponding values given by the built-in sqrt function over the domain [0, 64] */ for (i=0, h=64.0/(N-1); i0) { /* Require C^1 continuity at left vertex */ s = t[j]; mat[4*(j-1)+1][4*j+2] = 1; mat[4*j+1][4*j+2] = -1; mat[4*(j-1)+2][4*j+2] = 2*s; mat[4*j+2][4*j+2] = -2*s; mat[4*(j-1)+3][4*j+2] = 3*s*s; mat[4*j+3][4*j+2] = -3*s*s; b[4*j+2] = 0; /* Require C^2 continuity at left vertex */ mat[4*(j-1)+2][4*j+3] = 2; mat[4*j+2][4*j+3] = -2; mat[4*(j-1)+3][4*j+3] = 6*s; mat[4*j+3][4*j+3] = -6*s; b[4*j+3] = 0; } else { /* Require second derivative to be 0 at two ends. */ mat[2][2] = 2; mat[3][2] = 0; b[2] = 0; mat[4*7+2][3] = 2; mat[4*7+3][3] = 6*t[8]; b[3] = 0; } } /* Solve linear system. */ memcpy( &pp[0][0], b, sizeof(double)*32); solve32x32( mat, &pp[0][0]); } /************************************************************* * FUNCTION: ppval * * Evaluate cubic spline at given values *************************************************************/ void ppval(double pp[8][4], const double *t_input, int n, const double* ts, double* ys) { int i,j,k=0; /* FIXME: Implement this function to evaluate cubic spline. */ assert(0); for (i=0; i= 0. NRHS (input) INTEGER The number of right hand sides, i.e., the number of columns of the matrix B. NRHS >= 0. A (input/output) REAL array, dimension (LDA,N) On entry, the N-by-N coefficient matrix A. On exit, the factors L and U from the factorization A = P*L*U; the unit diagonal elements of L are not stored. LDA (input) INTEGER The leading dimension of the array A. LDA >= max(1,N). IPIV (output) INTEGER array, dimension (N) The pivot indices that define the permutation matrix P; row i of the matrix was interchanged with row IPIV(i). B (input/output) REAL array, dimension (LDB,NRHS) On entry, the N-by-NRHS matrix of right hand side matrix B. On exit, if INFO = 0, the N-by-NRHS solution matrix X. LDB (input) INTEGER The leading dimension of the array B. LDB >= max(1,N). INFO (output) INTEGER = 0: successful exit < 0: if INFO = -i, the i-th argument had an illegal value > 0: if INFO = i, U(i,i) is exactly zero. The factorization has been completed, but the factor U is exactly singular, so the solution could not be computed. ===================================================================== */ /* Solve 9x9 linear system by calling LAPACK routine dgesv_ */ int solve9x9(double A[9][9], double b[9]) { int info,n,nrhs,lda,ipiv[9],ldb; /* Invoke dgesv_ */ n = lda = ldb = 9; nrhs = 1; dgesv_(&n, &nrhs, &A[0][0], &lda, ipiv, b, &ldb, &info); if (info) printf("Error: dgesv returned info %d\n", info); return info; } /* Solve 32x32 linear system by calling LAPACK routine dgesv_ */ int solve32x32(double A[32][32], double b[32]) { int info,n,nrhs,lda,ipiv[32],ldb; /* Invoke dgesv_ */ n = lda = ldb = 32; nrhs = 1; dgesv_(&n, &nrhs, &A[0][0], &lda, ipiv, b, &ldb, &info); if (info) printf("Error: dgesv returned info %d\n", info); return info; }