Download presentation
Presentation is loading. Please wait.
1
1 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi Today's Agenda ● Introduce Matlab User Functions
2
2 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
3 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi Matlab's Built-In Functions: ● Elementary Functions ● Array Operations ● Special Functions ● Matrix Operations ● Numerical Methods ● Data analysis functions ● Graphing Functions ● Polynomial Functions ● Signal Processing Functions
4
4 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi Using Elementary Functions ● Math Format y = f(x) ● MATLAB Format output_variable = function_name (input_variable) ● Example squareroot_of_number = sqrt (16) Any name you choose (output value) MATLAB function number or variable (input value)
5
5 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi Matlab runs two types of programs: 1)SCRIPT files --- stand alone programs like you've been writing 2)FUNCTION files --- sub-programs intended to do a specific task and return the results to the program that called it
6
6 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
7 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
8 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
9 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
10 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
11 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
12 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
13 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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 y i =sin(x i ) ● Plot y i versus x i
14
14 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
15 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
16 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi Modules ● Main Program ● flow_stats ● daily_ave ● weekly_ave ● plot_data
17
17 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
18 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
19 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
20 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
21 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
22 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi 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
23 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi Team Homework... Due: 13A ● Glen Canyon Dam Analysis (see handout)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.