23
CFD I Computational Fluid Dynamics I CFD I Universidad de A Coruña Escuela Técnica Superior de Ingenieros de Caminos, Canales y Puertos Computational Fluid Dynamics I Matlab 1: Introduction and operations Acacia Naves García-Rendueles Hochschule Magdeburg-Stendal Fachbereich Wasser und Kreislaufwirtschaft

Matlab 1 Introduction and Operation

  • Upload
    -

  • View
    24

  • Download
    0

Embed Size (px)

DESCRIPTION

Introduction and Operation

Citation preview

Page 1: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

CFD I

Universidad de A Coruña Escuela Técnica Superior de Ingenieros de Caminos, Canales y Puertos

Computational Fluid Dynamics I Matlab 1: Introduction and operations

Acacia Naves García-Rendueles

Hochschule Magdeburg-Stendal Fachbereich Wasser und Kreislaufwirtschaft

Page 2: Matlab 1 Introduction and Operation

0. Introduction to CFD. Revision of concepts (6h)

1. Open channel flow. A revision

2. Saint-Venant equations.

3. Introduction to CFD

4. Mathematical preliminaries

1. Governing equations (6h)

1. Navier-Stokes

2. Potential, stream function, stokes flow

3. Shallow Water equations

4. Convection-diffusion eq

2. Finite elements and fluids hydrodynamics (24 h)

1. Finite elements and fluids

2. Variational and weighted residuals methods

3. Discretization

4. Potential flow

5. Stokes flow

6. Stable velocity-pressure pairs

7. Unsteady convective flow

8. Penalty methods

9. Shallow water equations

10. Stabilizing techniques

11. Flow in porous media

12. Conservative transport

13. Non-isothermal transport of reactives

3. Introduction to Finite Volumes (8h)

4. End user programmes (20h)

1. MATLAB

Introduction to Matlab (8h)

CW3 (2h)

CW4 (6h)

2. EPANET (6h + CW)

3. HEC-RAS (6h + CW)

4. SMS//RMA2

CFD I Computational Fluid Dynamics I

Page 3: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

What is MATLAB?

Mathematical tool:

MATrix LABoratory

Software package for high

performance numerical computation

and visualization.

Keys of MATLAB success:

•Easy use: shallow learning curve

•High availability of built-in functions

•Large number of users

Diagram of main

features and

capabilities of MATLAB

Page 4: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

• Launch MATLAB & do some

simple calculations: add,

multiply, exponentiate

numbers, use trigonometric

functions…

• Create and operate with

arrays: matrices and vectors

• Plot graphs

• Write and execute scripts

and function files

Page 5: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

Will MATLAB run in my computer?

Most likely “YES”

Windox, Unix, Linx, Mac OS X...

Where do I get MATLAB?

It is a product of MathWorks

MATLAB WINDOWS

Matlab desktop:

- Command window

-Current directory pane

- Details pane

- Workspace pane (variables)

- Command Hystory pane

Figure window

Editor window

Page 6: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

Page 7: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

Command window Introduce commands >> PROMPT means MATLAB is ready for new instrucctions >> 234+485 Use as calculator: operation + enter ans = 719 ans is a variable where last result is saved

Page 8: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

Commands and operators used: + - * / ^ >> 234*485 use as calculator : multiplication ans = 113490 ans changes its value >> 5^7 use as calculator : exponentiation ans = 78125 Several operations PRIORITY RULES: - First inside parenthesis and after outside - 1st exponentiation, 2nd multiplication & division 3rd addition & substration >> (2^12+1/7)/(0.25-3*(1-3^0.5)) ans = 1.6745e+03

212 +17

0.25− 3(1 − 3)

Page 9: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

MATLAB recognizes the letters i and j as the imaginary number (i.e.: 2+5i or 2+5*i) Operations with complex numbers >> (3-2i)*(4+5i) ans = 22.0000 + 7.0000i MATLAB knows trigonometric functions: sin, cos, tan, asin, cot, sec, sinh, asinh,… (arguments in radians)

−1

>> abs( 22 + 7i) ans = 23.0868

>> angle( 22 + 7i) ans = 0.3081

F1

Page 10: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

>> tan(pi/3) ans = 1.7321

>> x=tan(pi/3) x = 1.7321

>> x=tan(pi/3); >>

Save as a new variable Use of semicolon ( ; )

Variable value >> x x = 1.7321

Page 11: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

>> x x = 1.7321 >> format long >> x x = 1.732050807568877 >> format rat >> x x = 1351/780 >> format short e >> x x = 1.7321e+00 >> format short >> x x = 1.7321

Screen output formats Format ≠ machine precision (accuracy)

Delete Command Window >> clc

Page 12: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

Creating and working with arrays of numbers

Goal: Learn how to create arrays and how to perform arithmetic and

trigonometric operations with them.

Array rows & columns 1 row or 1 column = vector

m rows and n columns = matrix (m x n)

scalar variables “1x1 arrays”

Dimensioning an array is automatic in MATLAB.

No dimension statements are required.

Creating an array:

• Between braquets [ ]

• Rows are separated by ;

• Columns are separated by , or spaces

Page 13: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

Creating an array:

• Between square braquets [ ]

• Rows are separated by ;

• Columns are separated by , or spaces

>> a=[2 3 0 1] a = 2 3 0 1 >> b=[2;3;0;1] b = 2 3 0 1

>> A=[0 -1 3 2;2 1 7 2;3 0 6 3; 5 0 10 6] A = 0 -1 3 2 2 1 7 2 3 0 6 3 5 0 10 6

See values: a ≠ A lower or upper case

Name of variables: several letters and numbers. The first should be a letter.

Page 14: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

Elementary matrices:

>> eye (5)

1 0 0 0 0

0 1 0 0 0

0 0 1 0 0

0 0 0 1 0

0 0 0 0 1

>> ones(4,3)

1 1 1

1 1 1

1 1 1

1 1 1

>> zeros (2,3)

0 0 0

0 0 0

>> rand (5) Uniformly distributed pseudorandom numbers (0,1) 0.9058 0.6324 0.1270 0.0975 0.9134 0.2785

>> linspace (0,10,11) Linspace(X1, X2,N) generates a row vector of linearly equally N spaced points between X1 and X2. 0 1 2 3 4 5 6 7 8 9 10

>> x = 0:2:10 x= X1: E : X2 generates a row vector of points between X1 and X2 which are spaced E

0 2 4 6 8 10

>> logspace Logspace(X1, X2, N) generates generates a row vector of N logarithmically equally spaced points between decades 10^X1 and 10^X2.

>> meshgrid(a,b) or meshgrid (a,b,c) Rectangular grid in 2-D and 3D space Replicates vectors a, b &c to produce a full grid.

Page 15: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

Operations Matrices definition: >> A A = 0 -1 3 2 2 1 7 2 3 0 6 3 5 0 10 6 >> D=[2 -1 3 0;0 0 1 5] D = 2 -1 3 0 0 0 1 5 >> E=rand(4,4) E = 0.5469 0.9706 0.1419 0.9595 0.9575 0.9572 0.4218 0.6557 0.9649 0.4854 0.9157 0.0357 0.1576 0.8003 0.7922 0.8491

Matrix addition: +

>> A+E ans = 0.5469 -0.0294 3.1419 2.9595 2.9575 1.9572 7.4218 2.6557 3.9649 0.4854 6.9157 3.0357 5.1576 0.8003 10.7922 6.8491

>> A+D Error using + Matrix dimensions must agree.

Multiplication by a scalar >> 4*D ans = 8 -4 12 0 0 0 4 20

Page 16: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

Operations Matrices definition:

>> D=[2 -1 3 0;0 0 1 5] D = 2 -1 3 0 0 0 1 5

>> E=rand(4,4) E = 0.5469 0.9706 0.1419 0.9595 0.9575 0.9572 0.4218 0.6557 0.9649 0.4854 0.9157 0.0357 0.1576 0.8003 0.7922 0.8491

Matrix multiplication: * Ordinary matrix multiplication Pay attention to matrix dimensions

>> D*E ans = 3.0309 2.4401 2.6092 1.3704 1.7530 4.4868 4.8768 4.2814 >> E*D Error using * Inner matrix dimensions must agree.

Inverse of a matrix and “rigth” division ( \ ): Linear equations system Ax=b

1st solution: x=A-1·b >> x = inv(A)*b Where inv(A) calculates the inverse A matrix

2nd solution: Gauss Reduction Method >> x = A\b Less sensitive to numerical errors

Page 17: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

Transposition of a matrix: DT

Matrix definition: D = 2 -1 3 0 0 0 1 5

>> D‘ ans = 2 0 -1 0 3 1 0 5

Matriz “multiplication term by term”: .* Each element of the resultant matrix is derived by multiplying the elements on same position in both matrices. >> B = A.*E B (i,j) = A (i,j) * E (i,j) Matrix dimensions must agree. Matrix definition: >> F=10*rand(2,4) F = 9.3399 7.5774 3.9223 1.7119 6.7874 7.4313 6.5548 7.0605

>> D.*F ans = 18.6799 -7.5774 11.7668 0 0 0 6.5548 35.3023

Other matrix operations

You will find MATLAB is set up to do almost any matrix computation: inverse, determinant, rank,…

Notice than for a complex number array D’ is the conjugate inverse matrix

Page 18: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

Other operations “term by term”:

They are indicated by a previous to the operation symbol dot >> B = D ^ 4 B = A*A*A*A >> C = D .^ 4 C (i,j) = A (i,j) ^4 >> B = F./D B (i,j) = F (i,j) / D (i,j) Matrix definition:

D = 2 -1 3 0 0 0 1 5 F = 9.3399 7.5774 3.9223 1.7119 6.7874 7.4313 6.5548 7.0605

>> F./D ans = 4.6700 -7.5774 1.3074 Inf Inf Inf 6.5548 1.4121 Warning: Divide by zero Not an error but Inf (infinity)

Page 19: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

Trigonometric and math functions (exponential, logarithmic, …)

They can be applied to array arguments term by term No previous dot is needed >> sin(D) ans = 0.9093 -0.8415 0.1411 0 0 0 0.8415 -0.9589

What are term-by-term operations useful for? They are very useful to make the same calculations for a set of numerical values (ex: to evaluate a function in several points)

>> exp(D) ans = 7.3891 0.3679 20.0855 1.0000 1.0000 1.0000 2.7183 148.4132

Page 20: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

EXERCISE Test than

Create a vector n containing the following terms Create a new vector s in which is evaluated for each term of n

>> n = [1 10 100 1000 10000 1E05] >> s = (1+1./n).^n s = 2.0000 2.5937 2.7048 2.7169 2.7181 2.7183 >> e=exp(1) e = 2.7183

Page 21: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

Select a term or a submatrix

A (i,j) gives the term of A at ith row and jth column A(i, j:k) a range of rows or columns is indicated using “ : “ between the its limits A (i,:) a range of rows/columns defined A (:,j) by using “ : “ without limits selects ith row or jth column A(i, [j k]) more than one row/column index inside square brackets selects no adjacent rows/columns

Matrix definition >> A A = 0 -1 3 2 2 1 7 2 3 0 6 3 5 0 10 6

>> A(2,4) ans = 2

>> A(2,1:3) ans = 2 1 7 >> A(2:4,3) ans = 7 6 10

>> A(4,:) ans = 5 0 10 6

>> A([1 4],[2 4]) ans = -1 2 0 6

Page 22: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

To add/delete rows or columns of a matrix

[E ; u] row addition Row/column deletion: [E uT] column addition equal to “empty matrix”: [ ] (artefact) Pay attention to matrix dimensions

Matrix definition >> A A = 0 -1 3 2 2 1 7 2 3 0 6 3 5 0 10 6 >> u=[3 4 1 5];

>> G=[A;u] G = 0 -1 3 2 2 1 7 2 3 0 6 3 5 0 10 6 3 4 1 5 >> G=[A u'] G = 0 -1 3 2 3 2 1 7 2 4 3 0 6 3 1 5 0 10 6 5

>> G=A; >> G(3,:)=[] G = 0 -1 3 2 2 1 7 2 5 0 10 6 >> G(:,[1 3])=[] G = -1 2 1 2 0 3 0 6

Page 23: Matlab 1 Introduction and Operation

CFD I Computational Fluid Dynamics I

EXERCISE : Build G by operating with A, B and C

>> G=zeros(6,6) G = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >> A=[2 6;3 9]; >> B=[1 2;3 4]; >> C=[-5 5;5 3]; >> G(1:2,1:2)=A; >> G(3:4,3:4)=B; >> G(5:6,5:6)=C;

A - Delete the last row and column B - Select the first 4x4 submatrix C - Replace G(5,5) by 4 D - Select the {1,3,6}x {2,5} submatriz

A >> G(1:5,1:5)

or

>>H=G >>H(:,6) = [] >>H(6,:) = []

B >> G(1:4,1:4) C >>H=G >> H(5,5)=4

D >> G([1 3 6],[2 5])