You are on page 1of 23

CFD I

Computational Fluid Dynamics I

CFD I

Computational Fluid Dynamics I


Matlab 1: Introduction and operations
Acacia Naves Garca-Rendueles

Hochschule Magdeburg-Stendal
Fachbereich Wasser und Kreislaufwirtschaft

Universidad de A Corua
Escuela Tcnica Superior de Ingenieros de Caminos, Canales y Puertos

CFD I
Computational Fluid Dynamics I
0.

1.

Introduction to CFD. Revision of concepts (6h)

4.

End user programmes (20h)

1.

Open channel flow. A revision

2.

Saint-Venant equations.

3.

Introduction to CFD

Introduction to Matlab (8h)

4.

Mathematical preliminaries

CW3 (2h)

Governing equations (6h)

1. MATLAB

CW4 (6h)

1. Navier-Stokes
2. Potential, stream function, stokes flow

2. EPANET (6h + CW)

3. Shallow Water equations

3. HEC-RAS (6h + CW)

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. SMS//RMA2

CFD I
Computational Fluid Dynamics I

What is MATLAB?
Mathematical tool:

Diagram of main
features and
capabilities of MATLAB

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

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

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

CFD I
Computational Fluid Dynamics I

CFD I
Computational Fluid Dynamics I

Command window Introduce commands


>>
>> 234+485
ans = 719

PROMPT means MATLAB is ready for new instrucctions


Use as calculator: operation + enter
ans is a variable where last result is saved

CFD I
Computational Fluid Dynamics I

Commands and operators used:

+ - * / ^

>> 234*485
ans =
113490

use as calculator : multiplication

>> 5^7
ans =
78125

use as calculator : exponentiation

ans changes its value

Several operations PRIORITY RULES: - First inside parenthesis and after outside
1
7
0.25 3(1 3)
212 +

>> (2^12+1/7)/(0.25-3*(1-3^0.5))
ans = 1.6745e+03

- 1st exponentiation,
2nd multiplication & division
3rd addition & substration

CFD I
Computational Fluid Dynamics I

MATLAB recognizes the letters i and j as the imaginary number


1
(i.e.: 2+5i or 2+5*i) Operations with complex numbers
>> (3-2i)*(4+5i)
ans =
22.0000 + 7.0000i

>> abs( 22 + 7i)


ans =
23.0868

>> angle( 22 + 7i)


ans =
0.3081

MATLAB knows trigonometric functions:


sin, cos, tan, asin, cot, sec, sinh, asinh, (arguments in radians)

F1

CFD I
Computational Fluid Dynamics I

>> tan(pi/3)
ans =
1.7321

Save as a new variable


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

Use of semicolon ( ; )

>> x=tan(pi/3);
>>
Variable value
>> x
x=
1.7321

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

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

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.

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

>> ones(4,3)
1
1
1
1

1
1
1
1

1
1
1
1

>> zeros (2,3)


0
0

0
0

0
0

0
0
0
1
0

>> rand (5)

>> logspace

Uniformly distributed pseudorandom


numbers (0,1)

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

0
0
0
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

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

8 10

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

CFD I
Computational Fluid Dynamics I

Operations

Matrix addition:
>> A+E
ans =
0.5469
2.9575
3.9649
5.1576

-0.0294
1.9572
0.4854
0.8003

Matrices definition:

3.1419
7.4218
6.9157
10.7922

2.9595
2.6557
3.0357
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

>> A
A=
0
2
3
5

-1 3 2
1 7 2
0 6 3
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.9575 0.9572
0.9649 0.4854
0.1576 0.8003

0.1419
0.4218
0.9157
0.7922

0.9595
0.6557
0.0357
0.8491

CFD I
Computational Fluid Dynamics I

Matrix multiplication: *

Matrices definition:

Ordinary matrix multiplication


Pay attention to matrix dimensions

>> 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.9575 0.9572
0.9649 0.4854
0.1576 0.8003

0.1419
0.4218
0.9157
0.7922

Operations

>> D*E
ans =
3.0309 2.4401 2.6092 1.3704
1.7530 4.4868 4.8768 4.2814
0.9595
0.6557
0.0357
0.8491

>> 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-1b


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

CFD I
Computational Fluid Dynamics I

Transposition of a matrix: DT
Matrix definition:
D=
2 -1 3 0
0 0 1 5
Notice than for a complex number
array D is the conjugate
inverse matrix

>> D
ans =
2
-1
3
0

0
0
1
5

Other matrix operations


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

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

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
>> C = D .^ 4

B = A*A*A*A
C (i,j) = A (i,j) ^4

>> B = F./D

B (i,j) = F (i,j) / D (i,j)

>> 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)

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

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

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

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)

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

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 (:,j)

a range of rows/columns defined


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

>> A(2,4)
ans = 2

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

0 10

>> A([1 4],[2 4])


ans =
-1 2
0 6

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

CFD I
Computational Fluid Dynamics I

To add/delete rows or columns of a matrix


[E ; u]
[E uT]

row addition
column addition

Row/column deletion:
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 1 7
3 0 6
5 0 10
3 4 1

2
2
3
6
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

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
>> 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;

0
0
0
0
0
0

0
0
0
0
0
0

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])

You might also like