170 likes | 373 Views
C++ AND FORTRAN PROGRAMMING. Applications to Engineering Problems. WHY PROGRAMMING/CODING?. Data processing and analyzing No free software available Specific needs For the win…. WHY FORTRAN/C++?. Commonly used programming languages Quite easy to learn and use
E N D
C++ AND FORTRAN PROGRAMMING Applications to Engineering Problems
WHY PROGRAMMING/CODING? • Data processing and analyzing • No free software available • Specific needs • For the win…
WHY FORTRAN/C++? • Commonly used programming languages • Quite easy to learn and use • Huge reference resources (books, Internet, forum, experts)
GENERAL PROCEDURE • Edit source code (Notepad, IDE) • Build executable (Compilers, Linkers) • Test and Debug: • Correct -> Done!!! (Phew..) • Incorrect-> Return to Step 1.
DEMO • Least square fitting
C++ code Fortran code #include <iostream.h> #include <stdio.h> void main() { double** data; double Sxy, Sx, Sy, Sx2; inti, N; //Import datafile into a matrix of 2 rows cout << "Reading data file..\n"; FILE* fp = fopen("sample-data.txt", "r"); fscanf(fp, "%d", &N); data = new double* [2]; for (i=0; i<2; i++) data[i] = new double [N]; for (i=0; i<N; i++) fscanf(fp, "%lf\t%lf", &data[0][i], &data[1][i]); program LINEAR_SQUAREFIT implicit none real(8), dimension(:,:), allocatable:: data real(8):: a, b, Sx, Sy, Sxy, Sx2 integer:: I, N ! Import datafile into a matrix of 2 rows print *,’Reading data file’ open (1, file = 'sample-data.txt') read (1, *) N allocate (data(2, int(N))) do i = 1,N read (1,*) data(1,i), data(2,i) enddo ! Calculate coefficients of linear line, a and b Sx = 0.0 Sy = 0.0 Sxy = 0.0 Sx2 = 0.0 do i = 1,N Sx = Sx + data(1,i) Sy = Sy + data(2,i) Sxy = Sxy + data(1,i)*dataxy(2,i) Sx2 = Sx2 + data(1,i)**2 enddo a = (N * Sxy – Sx * Sy) / (N * Sx2 - Sx**2) b = (-Sx * Sxy + Sx2 * Sy) / (N * Sx2 - Sx**2) print *, ‘Fitting coeff. for linear fitting (y = ax + b):\’; print *, a, b end program LINEAR_SQUAREFIT // Calculate coefficients of linear line, a and b Sxy = 0.0; Sx = 0.0; Sy = 0.0; Sx2 = 0.0; for (i=0; i<N; i++) { Sxy = Sxy + data[0][i] * data[1][i]; Sx = Sx + data[0][i]; Sy = Sy + data[1][i]; Sx2 = Sx2 + data[0][i] * data[0][i]; } double a, b; a = (N * Sxy - Sx * Sy) / (N * Sx2 - Sx * Sx); b = (-Sx * Sxy + Sx2 * Sy) / (N * Sx2 - Sx * Sx); cout << "Fitting coeff. for linear fitting (y = ax + b):\n"; cout << "a = " << a << " b = " << b << "\n"; }
C++ code Fortran code program LINEAR_SQUAREFIT implicit none real(8), dimension(:,:), allocatable:: data real(8):: a, b, Sx, Sy, Sxy, Sx2 integer:: I, N ! Import datafile into a matrix of 2 rows print *,’Reading data file’ open (1, file = 'sample-data.txt') read (1, *) N allocate (dataxy(2,int(N))) do i = 1,N read (1,*) data(1,i), data(2,i) enddo #include <iostream.h> #include <stdio.h> void main() { double** data; inti, N; // Import datafile into a matrix of 2 rows cout << "Reading data file..\n"; FILE* fp = fopen("sample-data.txt", "r"); fscanf(fp, "%d", &N); data = new double* [2]; for (i=0; i<2; i++) data[i] = new double [N]; for (i=0; i<N; i++) fscanf(fp, "%lf\t%lf", &data[0][i], &data[1][i]); Declare used variables (type, decision,…)
C++ code Fortran code program LINEAR_SQUAREFIT implicit none real(8), dimension(:,:), allocatable:: data real(8):: a, b, Sx, Sy, Sxy, Sx2 integer:: I, N ! Import datafile into a matrix of 2 rows print *,’Reading data file’ open (1, file = 'sample-data.txt') read (1, *) N allocate (data(2,int(N))) do i = 1,N read (1,*) data(1,i), data(2,i) enddo #include <iostream.h> #include <stdio.h> void main() { inti, N; double** data; FILE* fp; // Import datafile into a matrix of 2 rows cout << "Reading data file..\n"; fp = fopen("sample-data.txt", "r"); fscanf(fp, "%d", &N); data = new double* [2]; for (i=0; i<2; i++) data[i] = new double [N]; for (i=0; i<N; i++) fscanf(fp, "%lf\t%lf", &data[0][i], &data[1][i]); Open data file named sample-data.txt
C++ code Fortran code program LINEAR_SQUAREFIT implicit none real(8), dimension(:,:), allocatable:: data real(8):: a, b, N, Sx, Sy, Sxy, Sx2 integer:: i ! Import datafile into a matrix of 2 rows print *,’Reading data file’ open (1, file = 'sample-data.txt') read (1, *) N allocate (data(2,int(N))) do i = 1,N read (1,*) data(1,i), data(2,i) enddo #include <iostream.h> #include <stdio.h> void main() { inti, N; double** data; FILE* fp; // Import datafile into a matrix of 2 rows cout << "Reading data file..\n"; fp = fopen("sample-data.txt", "r"); fscanf(fp, "%d", &N); data = new double* [2]; for (i=0; i<2; i++) data[i] = new double [N]; for (i=0; i<N; i++) fscanf(fp, "%lf\t%lf", &data[0][i], &data[1][i]); Read the first line for total number of columns
C++ code Fortran code program LINEAR_SQUAREFIT implicit none real(8), dimension(:,:), allocatable:: data real(8):: a, b, Sx,Sy, Sxy, Sx2 integer:: I, N ! Import datafile into a matrix of 2 rows print *,’Reading data file’ open (1, file = 'sample-data.txt') read (1, *) N allocate (data(2,int(N))) do i = 1,N read (1,*) data(1,i), data(2,i) enddo #include <iostream.h> #include <stdio.h> void main() { inti, N; double** data; // Import datafile into a matrix of 2 rows cout << "Reading data file..\n"; FILE* fp = fopen("sample-data.txt", "r"); fscanf(fp, "%d", &N); data = new double* [2]; for (i=0; i<2; i++) data[i] = new double [N]; for (i=0; i<N; i++) fscanf(fp, "%lf\t%lf", &data[0][i], &data[1][i]); Read the rest of data and import to a 2-by-n matrix
C++ code Fortran code // Calculate coefficients of linear line, a and b .. double Sxy, Sx, Sy, Sx2; Sxy = 0.0; Sx = 0.0; Sy = 0.0; Sx2 = 0.0; for (i=0; i<N; i++) { Sxy = Sxy + data[0][i] * data[1][i]; Sx = Sx + data[0][i]; Sy = Sy + data[1][i]; Sx2 = Sx2 + data[0][i] * data[0][i]; } double a, b; a = (N * Sxy - Sx * Sy) / (N * Sx2 - Sx * Sx); b = (- Sx * Sxy+Sx2 * Sy) / (N * Sx2 - Sx * Sx); cout << "Fitting coeff. for linear fitting (y = ax + b):\n"; cout << "a = " << a << " b = " << b << "\n"; } ! Calculate coefficients of linear line, a and b Sx = 0 Sy = 0 Sxy = 0 Sx2 = 0 do i = 1,N Sx = Sx + data(1,i) Sy = Sy + data(2,i) Sxy = Sxy + data(1,i)*data(2,i) Sx2 = Sx2 + data(1,i)**2 enddo a = (N * Sxy – Sx * Sy) / (N * Sx2 - Sx**2) b = (-Sx * Sxy + Sx2 * Sy) / (N * Sx2 - Sx**2) print *, "Fitting coeff. for linear fitting (y = ax + b):" print *, a, b end program LINEAR_SQUAREFIT Calculate some required summations
C++ code Fortran code // Calculate coefficients of linear line, a and b double Sxy, Sx, Sy, Sx2; Sxy = 0.0; Sx = 0.0; Sy = 0.0; Sx2 = 0.0; for (i=0; i<N; i++) { Sxy = Sxy + data[0][i] * data[1][i]; Sx = Sx + data[0][i]; Sy = Sy + data[1][i]; Sx2 = Sx2 + data[0][i] * data[0][i]; } double a, b; a = (N * Sxy - Sx * Sy) / (N * Sx2 - Sx * Sx); b = (-Sx * Sxy + Sx2 * Sy) / (N * Sx2 - Sx * Sx); cout << "Fitting coeff. for linear fitting (y = ax + b):\n"; cout << "a = " << a << " b = " << b << "\n"; } ! Calculate coefficients of linear line, a and b Sx = 0 Sy = 0 Sxy = 0 Sx2 = 0 do i = 1,N Sx = Sx + data(1,i) Sy = Sy + data(2,i) Sxy = Sxy + data(1,i)*data(2,i) Sx2 = Sx2 + data(1,i)**2 enddo a = (N * Sxy – Sx * Sy) / (N * Sx2 - Sx**2) b = (-Sx * Sxy + Sx2 * Sy) / (N * Sx2 - Sx**2) print *, "Fitting coeff. for linear fitting (y = ax + b):" print *, a, b end program LINEAR_SQUAREFIT Compute a and b
C++ code Fortran code // Calculate coefficients of linear line, a and b double Sxy, Sx, Sy, Sx2; Sxy = 0.0; Sx = 0.0; Sy = 0.0; Sx2 = 0.0; for (i=0; i<N; i++) { Sxy = Sxy + data[0][i] * data[1][i]; Sx = Sx + data[0][i]; Sy = Sy + data[1][i]; Sx2 = Sx2 + data[0][i] * data[0][i]; } double a, b; a = (N * Sxy - Sx * Sy) / (N * Sx2 - Sx * Sx); b = (Sx2 * Sy - Sx * Sxy) / (N * Sx2 - Sx * Sx); cout << "Fitting coeff. for linear fit (y = ax + b):\n"; cout << "a = " << a << " b = " << b << "\n"; } ! Calculate coefficients of linear line, a and b Sx = 0 Sy = 0 Sxy = 0 Sx2 = 0 do i = 1,N Sx = Sx + data(1,i) Sy = Sy + data(2,i) Sxy = Sxy + data(1,i)*data(2,i) Sx2 = Sx2 + data(1,i)**2 enddo a = (N * Sxy – Sx * Sy) / (N * Sx2 - Sx**2) b = (-Sx * Sxy + Sx2 * Sy) / (N * Sx2 - Sx**2) print *, ‘Fitting coeff. for linear fit (y = ax + b):’ print *, ‘a = ’, a print *, ’b = ’, b end program LINEAR_SQUAREFIT Display results on the monitor