/* Programming template for Final Part 2 Question 3 Refer to Exercise 4.8 from the text and also the Wikipedia entry on the Thomas Algorithm. Suppose A is a n x n tridiagonal matrix with the vector v along the diagonal, w on the upper diagonal and u on the lower diagonal. Thus, [ v[0] w[0] ] [ u[1] v[1] w[1] ] [ u[2] v[2] w[2] ] A = [ . . . ] [ . . . ] [ . . . ] [ u[n-2] v[n-2] w[n-2] ] [ u[n-1] v[n-1] ] Assume |u[i]| + |w[i]| < |v[i]| for every i so that Gaussian elimination can be performed without any pivoting. Write a subroutine called tdthomas to solve Ax = b in O(n) time. Feel free to use the matrix library developed in class or any other code that you find useful as part of your program. */ #include #include #include #include #include #include static double tic_time; void tic() { struct timeval tp; gettimeofday(&tp,0); tic_time=tp.tv_sec+tp.tv_usec*1.0e-6; } double toc() { struct timeval tp; gettimeofday(&tp,0); return (tp.tv_sec+tp.tv_usec*1.0e-6)-tic_time; } // Multiply the tridiagonal system Ax to find y assume n>3. void tdmult(int n,double u[n],double v[n],double w[n-1], double x[n],double y[n]){ y[0]=v[0]*x[0]+w[0]*x[1]; for(int i=1;i=0;i--) x[i]=b[i]-w[i]*x[i+1]; } #define K 10 int main(){ printf("Final Part 2 Question 3.\n"); setrlimit(RLIMIT_STACK, &(const struct rlimit) {RLIM_INFINITY,RLIM_INFINITY}); int n=512; double T[K]; // Use different values of n to make sure O(n) time complexity. for(int k=0;k