1 / 23

Today's Agenda

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

lovey
Download Presentation

Today's Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today's Agenda • Introduce Matlab User Functions

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  16. Modules • Main Program • flow_stats • daily_ave • weekly_ave • plot_data

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

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

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

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

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

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

  23. Team Homework ...Due: 13A • Glen Canyon Dam Analysis (see handout)

More Related