230 likes | 296 Views
Today's Agenda. Introduce Matlab User Functions. What are Functions?. Functions in MATLAB are similar to: functions in C subroutines in FORTRAN and BASIC procedures in Pascal Building blocks of larger programs Allows complex programs to be structured and organized
E N D
Today's Agenda • Introduce Matlab User Functions
What are Functions? • Functions in MATLAB are similar to: • functions in C • subroutines in FORTRAN and BASIC • procedures in Pascal • Building blocks of larger programs • Allows complex programs to be structured and organized • Defined by MATLAB or created by user • Accept input values and return output values
Data analysis functions Graphing Functions Polynomial Functions Signal Processing Functions Matlab's Built-In Functions: • Elementary Functions • Array Operations • Special Functions • Matrix Operations • Numerical Methods
Using Elementary Functions • Math Format y = f(x) • MATLAB Format output_variable = function_name (input_variable) • Example squareroot_of_number = sqrt (16) number or variable (input value) MATLAB function Any name you choose (output value)
Matlab runs two types of programs: • SCRIPT files --- stand alone programs like you've been writing • FUNCTION files --- sub-programs intended to do a specific task and return the results to the program that called it
Matlab user-defined FUNCTIONS: • First line designates it as a function • lists input values and returns output values • Second line(s) are “prologue” to be printed in response to a help command • Following lines complete the calculations NOTE: All the variables are local variables, which means their values are only available within the function
Generic Function Example function[output_variables] = function_name (input_variables); % this is an example of a function % input_variable and ouput_variable are both vectors output_variables =input_variables .^ 2 • If only one output value the [ ] are optional • First line tells MATLAB "this is a function" • Prologue describes what the function does • help function_name prints Prologue to screen • Calculations come after the Prologue • Must SAVE function in EDITOR with .m extension
Area of a Square Function function [Asqr] = square(side) % This function calculates the area of a square. % Input variable (length of side) can be a scalar or vector % Output variable will be a scalar or vector depending on input [Asqr]=side.^2; • output_variables = Asqr • function_name = square.m • input_variables = side
Calling a Function In Command Window or Parent program type: [output_variable]=function_name(input_variable) Examples: >> side=2 >> [Asqr]=square(side) OR >> Area=square(2) OR >> Asqr=square([1,2,4])
Area of a Square and Circle Function function [Asqr,Acir] = square_circle(side,radius) % This function calculates the area of a square and circle. % Input variables can be a scalar or vector % Input variables are length of square and radius of circle % Output variable will be a scalar or vector depending on input % Output variables are area of square and area of circle [Asqr]=side.^2; [Acir]=pi*radius.^2;
Calling this Function • Variable names do NOT need to be the same as in the function • Variables must be in same location and order Example: >> length=2 >> r=2 >> [Area1,Area2] = square_circle(33,65) >> [surf_a_sq,surf_a_ci]=square_circle(length,r)
As a Team... • Write a "statistics' function that calculates mean, min, max, stdev for a set of data • Name this function stat_pack.m • Test this function in your command window using a simple data set • x=[1,2,3,4,5,6,7,8,9,10]
Stepwise Refinement • Design Strategy used to break a large task into smaller tasks • Continue to divide tasks until the tasks are relatively simple and have an obvious solution • For example: plot a sine wave from 0 to 2*pi • Generate a vector x with elements between 0 and 2pi • Evaluate yi=sin(xi) • Plot yi versus xi
Program Modules • Programs generally have a Main program which calls individual modules (user functions) • Modules should be dedicated to 1 task • Each module can be individually developed and tested • Modules may be reused for other applications
As a Team • Read the Glen Canyon Dam problem (LM p. 44) • Plan your solution to the Glen Canyon Dam Monitoring Analysis. • Break tasks to modules • List Input and Output parameters for each module
Modules • Main Program • flow_stats • daily_ave • weekly_ave • plot_data
main • Main routine • Problem documentation • Tasks: • Load the data into MATLAB matrix • Copy the data into a vector of times and a vector of flow rates • Call other functions to calculate statistics and output results
flow_stats • Input: hourly flow vector • Output: mean hourly flow, median hourly flow, standard deviation of hourly flow • Tasks: • Calculate mean, median, standard deviation hourly flow for the entire year
daily_ave • Input: hour vector, hourly flow vector • Output: day vector, average daily flow vector • Tasks: • Determine number of days in data set • Day vector = day of year • Daily ave = mean (flow hours (a:b) )
weekly_ave • Input: hour vector, hourly flow vector • Output: week vector, weekly flow vector • Tasks: • Determine number of weeks in data set • Week vector = day of year, middle of the week • Weekly ave = mean (hourly flow (a:b) )
plot_data • Input: hour, hourly flow, day vector, daily flow, week vector, weekly flow • Output: plot of flow rates and histogram • Tasks: • Plot hourly data versus time • Plot histogram of hourly flow, with 20 bins • Plot daily data versus time as a line and weekly data versus time as circle
Testing Your Code • Test each module with a simple version of the problem, whose answers can be checked by hand calculations • Display intermediate calculations by removing semicolons at the end of statements or adding or removing print statements • Use MATLAB debugger
Team Homework ...Due: 13A • Glen Canyon Dam Analysis (see handout)