Hydroinformatics: Session4 Dr Ivan Stoianov [email protected] Room 328B Dr Andrew Ireson...
-
Upload
jessie-snow -
Category
Documents
-
view
219 -
download
0
Transcript of Hydroinformatics: Session4 Dr Ivan Stoianov [email protected] Room 328B Dr Andrew Ireson...
Hydroinformatics: Session4Dr Ivan Stoianov
Room 328B
Dr Andrew Ireson (Room 304)
Mr Juan Rodriguez-Sanchez (411A)
Mr Baback Mirshahi (304)
Mx Max Kigobe (304)
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
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
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
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
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
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
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
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
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
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
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)
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}
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
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
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
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
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
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
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;
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
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
ODE
Example:
; (0) 0dx
x t initial conditions xdt