hydroinformatics: session4 dr ivan stoianov [email protected] room 328b dr andrew ireson...

23
Hydroinformatics: Session4 Dr Ivan Stoianov [email protected] Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback Mirshahi (304) Mx Max Kigobe (304)

Upload: jessie-snow

Post on 16-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Hydroinformatics: Session4Dr Ivan Stoianov

[email protected]

Room 328B

Dr Andrew Ireson (Room 304)

Mr Juan Rodriguez-Sanchez (411A)

Mr Baback Mirshahi (304)

Mx Max Kigobe (304)

Page 2: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Hydroinformatics: Matlab

Session 1: Introduction to Hydroinformatics & Basic Matlab Use

Session 2: Working with Arrays and Graphics. Curve Fitting & Interpolation

Session 3: Script Files & Functions

Session 4: Script & Functions &

Data Analysis (Stats), ODEs & Nonlinear Algebraic Equations

Session 5: Matlab Symbolic Mathematics

Page 3: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Few Reminders

Array Operations

-Eliminates the need for FOR LOOP

Exercise:

Compute the product of data in two columns, x and y, where each column has n entries

n=5

X – linearly spaced vector of 5 points between 10 to 20;

Y – linearly spaced vector of 5 points between 20 to 40;

- Compute the product of X * Y using a FOR LOOP

- Any other way using a single line of code?

array_test.m

Page 4: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Few Reminders

Matrix Operations

ij ij ijC A B Element-by-element multiplications

ij ik kjC A B Matrix product

Two FOR LOOPS:• row index•column index

Page 5: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Few Reminders

Matrix Operations

Exercise:

Multiply two matrices – create a script called matrix_test

n=5

x(5,5)

x1 – linearly spaced vector of 5 points between 1 to 5;

x2=x1+2

x3=x1+3

x4=x1+4

x5=x1+5

x=[x1; x2; x3; x4; x5];

Y(5,5)

y1 – linearly spaced vector of 5 points between 5 to 9;

y2, y3, y4, y5 – same procedures as X

- Compute the product of X(5,5) and Y(5,5) using a FOR LOOP computation (DOUBLE LOOP)

- Any other way using a single line of code?

1(1) 1(2) 1(3) 1(4) 1(5)2(1)3(1)4(1)5(1) 5(5)

x x x x xxxxx x

{ }ij ij ijxy x y

matrix_test.m

Page 6: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Please note

Array dot product!

Matrix No worries re DOT product if A & B are compatible

VECTORIZATION

Take full advantage of array operations by vectorizing your code or computation

Page 7: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Exercise: vectorization

Approximate the exponential function with the first 10 terms in its series expansion:

110

exp( )( 1)!

k

i k

xx

k

Assume: x=1; k=[1:10]SumFactorialVectorization ./

vectorization_test.m

Page 8: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Session #3: Script & Functions

Script: Write a script to solve the following system of linear equations

1

2

3

5 2 23 6 2 1 32 1 3 5

xr rr x

r r x

Ax b-Find a solution of the equation for various values of the parameter r-Find the determinant of matrix A in each case

solvex.m

Page 9: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Function files

- Same as a script file, except that the variables in a function file are all local

- Starts with a function definition line + a well defined list of inputs and outputs

function [output variables]=function_name (input variables);

- function_name can be the same as file name;

- function must be typed in lower case (NOT Function)

- Comment: function_test.m

function_test.m

Page 10: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Function files: Executing a function

% function definition line

function [rho,H,F]=motion (x,y,t);

??? How do we execute the function (function call execution statements)

5 mins to write as many as possible in a script file

rt, yt, time R, angmon, force

rx,ry, [0:100] r,h,f

2, 3.5, 0.001 r,h,f

rx,ry radius, h

Input variables Output variables

function_test2.m

Page 11: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Session #3: Script & Functions

Function: Turn your script into a a function

1

2

3

5 2 23 6 2 1 32 1 3 5

xr rr x

r r x

Ax b-Find a solution of the equation for various values of the parameter r-Find the determinant of matrix A in each case

solvexf.m

Page 12: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Follow-up from Session #4

- Solving a set of linear algebraic equations

- Curve fitting

- Least squares fitting (an optimization problem)

- Data Analysis and stats

- Ordinary Differential Equation (ODEs)

Page 13: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Applications

Solving a set of linear algebraic equations

5 3 2 108 4 3 202 4 9 9

x y zy z xx y z

(1) Write equations in matrix form(2) Solve the matrix equation in Matlab(3) Check the solution(4) Write a function solvexf2 {output x}

Page 14: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Curve Fitting

Built-in polynomial functions Desired coefficients

( , )

? ,

i i

i i

x yy mx bm c y mx c

1 2

1 2 1 0...k ki k i k i i iy a x a x a x a x a

polyfit: a=polyfit(x,y,n)polyval

Page 15: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Linear fit

M:

5.00 10.00 20.00 50.00 100.00

Compute spring force: F=m/1000*g [N]

Deformation:

15.5 33.07 53.39 140.24 310.03

(1) Find the coefficients

a=polyfit (x,y,1)

(2) Evaluate y at finer xjs using the fitted polynomial

y_fitted=polyval (a,x_fine)

(3) Plot and see

plot(x,y,’o’,x_fine,y_fitted);

linefit_test.m

Page 16: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Least squares curve fitting

by ax

bxy ae

Polynomial

Non-Polynomial

ln( ) ln( )y a bx ~ ~

0 1 1 0; ln( ), , ln( )y a a x y y a b a a

Page 17: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Steps:

(1) Prepare new data by taking the log of the original data

ybar=log(y)

Leave x as it is

(2) Do a linear fit: use polyfit to find the coefficients a0 and a1

(3) Plot the curve: from the curve fit coefficients, calculate the values of the original constants

t: 0; 0.5; 1.0; 5.0; 10.0; 20.0

P 760, 625, 528, 85, 14, 0.16

/0

0

~

1 0

~

1

0 0

( )

ln( ) ln( )

ln( )1/ln( )

tP t P et

P P

P a t a

P Paa P

nonlinfit.m

Page 18: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Least squares curve fitting

0 1cos( ) sin( )y a t a t t

11 1

22 20

3 3 31

cos( ) sin( )

cos( ) cos( )

cos( ) cos( )

cos( ) cos( )n n n

xt t t

xt ta

t t xa

t t x

Page 19: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Interpolation

Interp1

YI = INTERP1(X,Y,XI)

interp2; interp3; spline

Exercise:

X: 0, 0.785, 1.570, 2.356, 3.141, 3.927, 4.712, 5.497 6.283

Y: 0, 0.707, 1.000, 0.707, 0.000, -0.707, -1.000, -0.707, 0

exinterp.m

Page 20: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Data Analysis and Statistics

X=[ 1 2 3 4 5]

A=[6, 5, -2; 7, 4, -1; 8, 3, 0; 9, 2, 1; 10, 2,2]

Find:

mean; median; std; max; min; sum; cumsum; prod; sort; sortrows; trapz;

Page 21: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

Ordinary Differential Equations

ode23 & ode45: most popular

Implementation of second/third order and fouth/fifth order Runge-Kutta methods

Basic rules:

- Write the differential equations as a set of first order ODEs

- - write a function to compute the state derivative (the vector of derivatives)

- - use the built-in ODE solvers to solve the equations

1 2

1

1 1 2 3

2

1 2 3

( , ); [ ... ]

( , , ... ,

( , , ... ,

Tn

n

n nn

dxf x t where x x x x

dt

dx

dt f x x x x tdx

dt

f x x x x tdx

dt

Page 22: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

ODEs

[time, solution] = ode23(‘your function’ , tspan, x0)

YourFunction user written function with title line: xdot=YourFunction(t,x). This function contains the ODEs you want to solve

Page 23: Hydroinformatics: Session4 Dr Ivan Stoianov Ivan.stoianov@imperial.ac.uk Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback

ODE

Example:

; (0) 0dx

x t initial conditions xdt