This chapter presents the following content: Basic definitions, example of a database, typical DBMS functionality, main characteristics of the database approach, database users, workers behind the scene,...
AMBIENT/
Chủ đề:
Nội dung Text: Lecture note Data visualization - Chapter 26
- Lecture 26
- Recap
Saving Plots
Summary of Chapter 5
Introduction of Chapter 6
- Function Mfiles
Userdefined functions are stored as Mfiles and can be
accessed by MATLAB if they are in the current folder or
on MATLAB’s search path
- Syntax of Function Mfile
Both builtin MATLAB ® functions and userdefined
MATLAB functions have the same structure
Each consists of a name, userprovided input, and
calculated output.
For example: the function
cos(x)
is named cos
takes the user input inside the parentheses (in this case, x )
calculates a result
- Continued….
Userdefined functions are created in Mfi les. Each must
start with a functiondefinition line that contains:
The word function
A variable that defines the function output
A function name
A variable used for the input argument
For example:
function output = my_function(x)
is the first line of the userdefined function called
my_function
- Continued….
Here’s an example of a very simple MATLAB function
that calculates the value of a particular polynomial:
function output = poly(x)
%This function calculates the value of a thirdorder
%polynomial
output = 3*x.^3 + 5*x.^2 2*x +1;
The function name is poly , the input argument is x , and
the output variable is named output
Before this function can be used, it must be saved into the
current folder
- Continued….
Once the Mfile has been saved, the function is available
for use from the command window, from a script Mfi le,
or from another function
You cannot execute a function Mfile directly from the
Mfile itself. This makes sense, since the input parameters
have not been defined until you call the function from the
command window or a script Mfile
Consider the poly function just created. If, in the
command window, we type
poly(4)
then MATLAB responds with
ans =265
- Comments
As with any computer program, code should be
commented liberally so that it is easy to follow
However, in a MATLAB function, the comments on the
line immediately following the very first line serve a
special role
These lines are returned when the help function is queried
from the command window
Consider, for example, the following function:
function results = f(x)
%This function converts seconds to minutes
- Functions with Multiple Inputs
and Outputs
- Continued….
Similarly, a userdefined function could be written to
multiply two vectors together:
function output = g(x,y)
% This function multiplies x and y together
% x and y must be the same size matrices
a = x .*y;
output = a;
When x and y are defined in the command window and
the function g is called, a vector of output values is
returned:
x = 1:5;
- Continued….
Such functions can also be created that return more than
one output variable
Many of the predefined MATLAB functions return more
than one result
For example: max returns both the maximum value in a
matrix and the element number at which the maximum
occurs. To achieve the same result in a userdefined
function, make the output a matrix of answers instead of a
single variable, as in
function [dist, vel, accel] = motion(t)
% This function calculates the distance, velocity, and
- Continued….
Remember, all variables in MATLAB are matrices, so it’s
important in the preceding example to use the .* operator,
which specifies elementbyelement multiplication
For example: using a vector of time values from 0 to 30
in the motion function
time = 0:10:30;
[distance, velocity, acceleration] = motion(time)
returns three vectors of answers:
distance =0 83.33 666.67 2250.00
velocity =0 25.00 100.00 225.00
acceleration =0 5.00 10.00 15.00