Polska wersja

Scientific Computing 2015/16

Tue lecture 1015am in room 5870, lab 1215pm room 2041(lab)

Konsultacje:

Patrz: Plan. (room 5010 - IV floor). - during classes only, otherwise please contact me by e-mail

Exam

Project 80% - oral questions 20%. Please, contact me by e-mail to make an appointment. During the exam one should show the code in action and answer a few questions concerning the material presented in the lectures.
In MID-September I will be available on Thu Sept 15 around 12pm.
Next lab

Octave scripts or source C files

Projects

References

  1. P. Krzyzanowski, Obliczenia inzynierskie i naukowe. Skuteczne, szybkie, efektowne, Wydawnictwo Naukowe PWN, 2011
  2. P. Krzyzanowski, Obliczenia naukowe. Html lecture notes (in Polish). Pdf version is alos available there. University of Warsaw. 2010.

Lab

  1. Lab 1 - linux basics: cd, ls, rm, cp, pwd, mv, tar, mkdir, rmdir, chmod, head, tail, more, cat, man. Basics of octave - octave as a scientific calculator, semicolon : how to create a matrix, saving/loading from/to files basic matrix/vector operations - opt. solving linear equations and LLSP etc Pr 1 Create matrices 3x4 A and 3x5 B - then the matrix 3x8 C with 3 first columns are A and next B. Create D 3x3 as a main minor of C oi.e. from C(1,1) to C(3,3). Change the order of columns of D. Compute sin on elements of D. Write D to a file (binary or ascii) - change D(1,1) to -100 and load this new matrix to octave as DD. Compute 1st norm of DD. Pr 2 Compute a discrete max norm of (sin(x))^2 on [0,1] (on 10000 points)
  2. Lab 2- scripts: example matbasic.m write on command line of octave'a: matbasic.
    1: using vander() and polyval() - solve linear regression problem for givem x_k,y_k find a,b such that \sum_k |ax_k+b -y_k|^2 is minimal. Compare with polyfit().
    2:find Lagrange interpolating polynomial on equidistant points on [-5,5] for f(x)=1(1+x*x) - compute discrete max norm of the error on [-5,5] and at interpolation nodes Test for 5,10,20 i 30 nodes.
    3- do same as in prob 2 but for Chebyshev points (linearly scaled roots of T_n(x)=cos (n*arc cos(x))).
    lab2.m - a few solutions
  3. Lab 3 and 4 Solve nonlinear equations (fsolve and fzeros) for simple equations x^2-2=0 etc exp(x)+x=0 . Global variables can be useful.... Prob 1: Solve x*(1+0.5sin(x))=0; (x-1)^2=0; Prob 2: using fsolve plot the graph of implicit function: e.g. y(x) for y^2+x^2=1 y(0)=1 or y(0)=-1; another ex. compute y(x) for f(x,y)=x^3+y^3-y=0 near y(0)=1. Prob 3: plot the graph of the inverse of sin(x) on [0,pi] or x^ on [0,2] (compare with arcsin(x) or \sqrt(x)) etc or whe ndo not know the direct formula for the inverse f.: (x+1)*exp(x^2) on [1,3] or x+cos(x). Prob 4: Repeat in 2d : on the mesh 10x10 on [0,1]x[0,1] compute the values of the inverse function of f([x;y])= [10x+y*y; 10y+x*y]
    Serious project we want to plot the inverse of a given function - see formulas in Chapter 3 in lect. notes.
    nonlin16.m - some simple examples of solving nonlinear equations (fsolve and fzero)
  4. Lab 5 and 6 : Numercal classics, sparse matrices, loops, integration etc : LU QR decompositions, LLSP, how to compute the inverse of a matrix, condition number, norms, SVD, null space, R(A) - how to get orhonormal basis of a system of vectors etc Sparse matrices global and static (persistent a=0) variables, .
  5. Because of a computer science high school competition - last lab was cancelled - we can do 1-2 problems from the previous lab. Solving initial value problems for ODEe : main solver : lsode. - scalar and multivariate cases
  6. Earthquake model, ODEs cont. Solving ODEs cont.
  7. Simple control problem for IVP or BVP ode16-1.m - some solutions for odes
    earthq16.m - some solutions earthquake model
    cp1d.m - a script with the solution of 1d control problem -u''=f u(0)=s u'(2)=0 we want to find s0 such that u(x;s0) minimizes : \int_1^2 |h(x)-u(x;s)|^2 dx 1/ test for f=0 (then u(x;s)=s) an h=0.5 the naturally s0=0.5
    How to organize scripts i.e. path,addpath etc.
  8. Control problem - parabolic PDE in 1D u_t=u_{xx} (0,T] x (0,L) - space 1D -rgt bnd : homogeneous Neumann u'(t,L)=0; left bnd cond : Dirichlet - u(t,0)=s(t) - control in pracitise let s be a lineatr spline (lpiecewise linear between times; or a polynomial of set degree e.quadratic. u(0,x)=u0(x) given.
  9. C language and using fortran libs i.e. e.g. BLAS in C/C++
    1. Prob Write a code in C/C++ which sums the harmonic series:\sum 1/n^p; p>1 using different loops: for(){ }, while(){ }, do{ } while(), option - p should be given interactively from the keyboard by an user
    2. Prob Binary and ascii files in C. Read help to (internet or in unix: man fopen )fopen(), fclose(), fwrite(), fread(), fprintf(), fscanf(), fgetchar() feof()etc Write a code that will write a vector (double array)to a file - binary or ascii (text) file then another code that will read it from the file. Test for: [1 2 3 4 -9.45].
      vecmatsave.c - a simplec C code writing vectors/matrices to text files
      Homework: write a C code that reads matrices from a text file written in ascii octave format to a double indexed float array in C. Then show the matrix on the computer display.
    3. Prob Alokacja pamięci. allocate and then free dynamically vectors float,double etc. Write a a C function generating a vector sin(x) for a given vector x (e.g .equidistant points on [a,b]) - parameters : apointer to x (or ends of interval - easier version)) and pointer to returned vector with sin(x) - C function should allocate memory and write to that memory sin(x), optionally export this vector to binary file (fwrite()) and then read it back to memory to another memory address
    4. Prob Precompiler macros like functions - #define #if etc Modify the code from the previous function in such a way that depending on the precompiler options it will work either for doubles or floats (single precision).
    5. Prob write a macro IJ(i,j) to a matrix MXN which is held in memory columnwise as a single vector of length M*N, and a functions that using that macro can show this matrix on the display, adds two matrices (in that format), multiplies two matrices, scales a matrix, creates matrix of given dimension (allocates memory), destroys matrix (frees memory) etc
    6. Prob create a new type which is a structure with two fields: 1st integer (dimension of a vector), and 2nd real pointer (data)- which represents a vector - the na functions creating vector (allocating memory to data pointer), destroying, scaling a vector, adding two vectors etc.
    7. Prob use a fortran BLAS function e.g. dnrm2.f- computes a 2nd norm of a vector - or - dscal.f - scales a vector by a scalar - in C code. Then write your header file : (in C code: #include "mojblas.h") with the proper headers of this functions (in order to use them in your C code)
      In general we have to compile fortran libs/functions using lib: gfortran.g and BLAS using blas.g e.g. gcc ourccode.c -lgfortran -lblas or if we want to compile using fortran e.g .function dscal.f then we can write: gcc ourccode.c dscal.f -lgfortran (-lm - in case of dnrm2.f - this funcion needs sqrt() from lmath lib)
      tblas.c - a simple C code using BLAS dnrm2() function - and dynamically allocating memory (using malloc(); free() functions)
  10. Using fortran libs cont.: matrix operations in BLAS, solving linear systems and overdetermined linear systems (LLSP) in LAPACK in C/C++
    1. Prob 1 Write a macro: IJ(i,j,M) returning the index of the element (i,j) in a matrix MXN kept in a single index array columnwise M in C. Using BLAS DGEMV(): dgemv.f compute Ax for A=[1 2;-1 1] and x= [0;1], [1;0], [1;1].
    2. Prob 2: Solve system Ax=f for A=[ 2 -2; 1 1] and f=[0; 2] (rozw [ 1;1]) using LAPACK function DGESV(): dgesv.f
    3. Prob 3: Solve system Bx=f for B tridiagonal NxN (discrete Laplacian 1D) : 2 on the main diagonal and -1 using LAPACK function DPTSV(): dptsv.f , check if we get the right solution ( e1=[1;0;0;...;0]) using a right BLAS function.
    4. Prob 4: Find LAPACk function solving the tridiagonal system and use for the system Ax=f with the tridiagonal matrix A with 7 on the main diagonal, -2 on superdiagonal, and -1 on subdiagonal for f=[7;-1;0;0;..;0] for N=100.
    5. Prob 5: Using a LAPACK function DGELS(): dgels.f for solving a regular LLSP Ax=f. Test on A=[1,1;1,2;1,3] and f=[2;3;4] (i.e. x=[1;1]) Solve the system MX2 Aa=y equivalent to linear regression i.e. x,y given vectors, we want to get a=arg\min_a \sum_k|a(1)+a(2)x(k)-y(k)|^2. Write a C/C++ function solving linear regression by DGELS(). Test for x=[1;...;10] and y=x+1 and different M=2,3,5,0,120. INPUT: integer M (dim of x,y), pointers to double x,y, OUTPUT: pointer to double a (solution of LLSP), pointer to integer info (code of results, zero if OK) (a=[1;1]).

  11. A few octave scripts, m-files, source C files, LAPACK functions

    Link to Octave
    octave-forge - octave extensions

    Octave scripts, m-files

    matbasic.m - a script with basic octave commands we can edit it : gedit matbasic.m
    lab2.m - a few solutions of lab2
    nonlin16.m - some simple examples of solving nonlinear equations (fsolve and fzero)
    on16sparse.m -some solutions for sparse 3diag matrices
    sesja4.m - a few simple examples on solving nonlinear equations and integration in octave
    ode16-1.m -some solutions for odes (simple odes in octave 1d or 2d, blow-up real and false, stiff solved by explicit or implict methods in lsode)
    sesja5.m - solving ODEs - 2 simple examples

    Wyklad - Środowko programisty gdzie można znależć materiały o języku C jak i Makefile'ach (in Polish)
    Project (in Polish). There is one project, i.e. optimal control of a heated room, described in details (there 2 versions: stationary and unstationary with many possible space discretizations method - I would describe 2 in details during lectures). I have other projects - ask for details).
    In the optimal control project I simplified necessary tests (it is enough to test the linear case - cf. describtion for details)

    Return to my homepage


    Last update : August 30, 2016