1 / 15

C++ AND FORTRAN PROGRAMMING

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

kaoru
Download Presentation

C++ AND FORTRAN PROGRAMMING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ AND FORTRAN PROGRAMMING Applications to Engineering Problems

  2. WHY PROGRAMMING/CODING? • Data processing and analyzing • No free software available • Specific needs • For the win…

  3. WHY FORTRAN/C++? • Commonly used programming languages • Quite easy to learn and use • Huge reference resources (books, Internet, forum, experts)

  4. GENERAL PROCEDURE • Edit source code (Notepad, IDE) • Build executable (Compilers, Linkers) • Test and Debug: • Correct -> Done!!! (Phew..) • Incorrect-> Return to Step 1.

  5. DEMO • Least square fitting

  6. 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"; }

  7. 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,…)

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. ANY QUESTION?

More Related