What are Functions?

Download Report

Transcript What are Functions?

Today's Agenda
●
1
Introduce Matlab User Functions
G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi
What are Functions?
●
2
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
G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi
Matlab's Built-In Functions:
●
Elementary
Functions
●
Data analysis
functions
●
Array Operations
●
Graphing Functions
●
Special Functions
●
●
Matrix Operations
Polynomial
Functions
●
Signal Processing
Functions
●
3
Numerical Methods
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)
4
number or variable
(input value)
MATLAB function
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
5
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
6
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
●
7
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;
8
●
output_variables = Asqr
●
function_name = square.m
●
input_variables = side
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])
9
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;
10
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)
11
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
●
12
x=[1,2,3,4,5,6,7,8,9,10]
G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi
Stepwise Refinement
13
●
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
G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi
Program Modules
14
●
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
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.
●
●
15
Break tasks to modules
List Input and Output parameters for each
module
G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi
Modules
●
16
Main Program
●
flow_stats
●
daily_ave
●
weekly_ave
●
plot_data
G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi
main
17
●
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
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:
●
18
Calculate mean, median, standard deviation hourly
flow for the entire year
G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi
daily_ave
19
●
Input:
hour vector, hourly flow vector
●
Output:
vector
day vector, average daily flow
●
Tasks:
●
Determine number of days in data set
●
Day vector = day of year
●
Daily ave = mean (flow hours (a:b) )
G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi
weekly_ave
20
●
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) )
G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi
plot_data
21
●
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
G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi
Testing Your Code
22
●
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
G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi
Team Homework ...
Due: 13A
●
23
Glen Canyon Dam Analysis (see handout)
G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi