Download presentation
Presentation is loading. Please wait.
Published byJessie Snow Modified over 9 years ago
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 Mirshahi (304) Mx Max Kigobe (304)
2
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
3
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
4
Few Reminders Matrix Operations Element-by-element multiplications Matrix product Two FOR LOOPS: row index column index
5
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? matrix_test.m
6
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
7
Exercise: vectorization Approximate the exponential function with the first 10 terms in its series expansion: Assume: x=1; k=[1:10] Sum Factorial Vectorization./ vectorization_test.m
8
Session #3: Script & Functions Script: Write a script to solve the following system of linear equations -Find a solution of the equation for various values of the parameter r -Find the determinant of matrix A in each case solvex.m
9
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
10
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, timeR, angmon, force rx,ry, [0:100]r,h,f 2, 3.5, 0.001r,h,f rx,ryradius, h Input variablesOutput variables function_test2.m
11
Session #3: Script & Functions Function: Turn your script into a a function -Find a solution of the equation for various values of the parameter r -Find the determinant of matrix A in each case solvexf.m
12
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)
13
Applications Solving a set of linear algebraic equations (1) Write equations in matrix form (2) Solve the matrix equation in Matlab (3) Check the solution (4) Write a function solvexf2 {output x}
14
Curve Fitting Built-in polynomial functions Desired coefficients polyfit: a=polyfit(x,y,n) polyval
15
Linear fit M: 5.0010.0020.0050.00100.00 Compute spring force: F=m/1000*g [N] Deformation: 15.533.0753.39140.24310.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
16
Least squares curve fitting Polynomial Non-Polynomial
17
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 nonlinfit.m
18
Least squares curve fitting
19
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
20
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;
21
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
22
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
23
ODE Example:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.