Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

mvasamp2.cpp

Go to the documentation of this file.
00001 #include "cammva.h"
00002 #include <fstream.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 //
00006 //######################################################################
00007 //
00008 // CAMmva Test Program #2
00009 //
00010 // This program demonstrates file input/output by constructing a matrix and
00011 // a vector, writing it to a file, and then reading it back in. The program
00012 // then solves the linear system represented by the matrix and the vector.
00013 //
00014 // Chris Andersion (C) UCLA 1997
00015 //######################################################################
00016 //
00017 int main(){
00018 
00019   CAMdoubleMatrix A(3,3);            // declare a matrix
00020   CAMdoubleMatrix M(3,3);
00021   CAMdoubleVector b(3);              // declare vectors
00022   CAMdoubleVector rhs(3);
00023   CAMdoubleVector x(3);
00024   CAMdoubleVector residual(3);
00025 //
00026 //   Open a file and write out a matrix and a right hand side
00027 //
00028   ofstream OutFile;                  // declare an output file stream
00029   OutFile.open("Test.dat",ios::out);   //
00030 
00031   long i,j;                         // variables for loop index
00032     
00033     
00034   for(i=1; i<= 3; i++)              // nested do-loop for initializing
00035   for(j=1; j<= 3; j++)              // the matrix M.
00036   {
00037     M(i,j)=1.0/(double(i) + double(j));
00038   }
00039     
00040   for(i=1; i<=3; i++)               // initialize rhs
00041   {
00042     rhs(i)=double(i);
00043   }
00044   OutFile << M;
00045   OutFile << rhs;
00046   OutFile.close();
00047 //
00048 // Open file and read back in the matrix and solve the system
00049 //
00050   ifstream InFile;                   // declare an input file stream    
00051     
00052                                      // connect the file stream with                                     
00053   InFile.open("Test.dat",ios::in);   // the external file tinput
00054   if (!InFile ) 
00055   {cerr <<  " Error in Opening input File "; exit(1); }
00056     
00057   InFile >> A;                       // read in the matrix A
00058   InFile >> b;                       // read in the vector b
00059 
00060   x=A/b;                            // solve the system A*x=b for x
00061   residual=A*x - b;                 // compute the residual
00062     
00063                                     // set scientific notation output 
00064                                     // format
00065 
00066   cout.setf(ios::scientific, ios::floatfield); 
00067 
00068   cout << " The matrix A " << endl;
00069   cout << A;
00070   cout << " The vector b " << endl;
00071   cout << b;
00072   cout << " The solution of A*x=b " << endl;
00073   cout << x;
00074   cout << " The residual " << endl;
00075   cout << residual;
00076 
00077   cout << " Program End : Hit Any Key to Terminate " << endl;
00078   getchar();
00079   return 0;
00080 }
00081 
00082   

Generated on Wed Sep 5 12:54:23 2007 for DSACSS Operational Code by  doxygen 1.3.9.1