Creating Functions

  • The user does not see the calculations performed, but just accepts the answer while working with builtin functions.
  • The function exp(x)
    • is named exp
    • takes the user input inside the parentheses (in this case x), and
    • calculates the result
  • User-defined functions work the same way.
  • User-defined functions are created in M-files
  • Each must start with a function defintion line that contains:
    • The word function
    • A variable that defines the function output
    • A function name
    • A variable used for the input argument
  • You can create a function using the ”New Function” option on the top left.

Let's see an example:

In [ ]:
function output = poly(x)
 % This function calculates the value of a third-order polynomial
output=3*x.^3+5*x.^2-2*x+1
end
  • The function name is poly
  • The input argument is x
  • The output variable is named output
  • The comments on the line immediately following the very first line are returned when the help function is queried from the command window.

Calling functions

  • function name(input): Calculates the result of a user-defined function, using the same syntax rules that apply to the built-in functions.

Note: A user-defined function can be called if it is in the current folder!

In [ ]:
x=3;
y=poly(x);

y =

        121

Functions with Multiple Inputs

In [ ]:
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
end
  • The function name is g
  • The input arguments are x and y
  • The output variable is named output
In [ ]:
function [dist, vel, accel] = motion(t)
% This function calculates the distance velocity, and acceleration of a particular car for ...
given value of t assuming all 3 parameters are initially 0.
accel = 0.5.*t
vel = t.^2/4
dist = t.^3/12
end
  • The function name is motion
  • The input argument is t
  • The output variables are accel, vel, and dist

Local and Global Variables

In [ ]:
function result = distance(t)
% This function calculates the distance a falling object travels due to gravity
g = 9.8 % meter per second squared
result = 1/2*g*t.^2
end
  • t, g and result are local variables
In [ ]:
global G; G=9.8;
In [ ]:
function result = distance(t)
% This function calculates the distance a falling object travels due to gravity
global G
result = 1/2*G*t.^2
end
  • G is a global variable

Determining the Number of Input and Output Arguments

  • nargin('function'): Determines the number of input arguments in either a user-defined function or a built-in function

    • nargin('exp')
      ans = 1

    • nargin('g')
      ans = 2

  • nargout('function'): Determines the number of outputs from the function

    • nargin('sin')
      ans = 1

    • nargin('motion')
      ans = 3

Anonymous Functions and Function Handles

  • An anonymous function is a function that is not stored in a program file, but is associated with a variable whose data type is function_handle
  • ln = @(x)log(x)
    • The @ symbol alerts MATLAB that ln is a function
    • Immediately following the @ symbol, the input to the function is listed in parantheses
    • Finally, the function is defined
In [1]:
a = 1.3;
b = 0.2;
c = 30;
parabola = @(x) a*x.^2+b*x+c;
parabola(2)
ans =

   35.6000


Anonymous Functions with Multiple Inputs and Outputs

  • Anonymous functions with multiple inputs:
In [2]:
c = @(a,b) sin(a*b);
c(1,3)
ans =

    0.1411


  • Anonymous functions with multiple outputs:
In [3]:
c = 10;
mygrid = @(x,y) ndgrid((-x:x/c:x),(-y:y/c:y));
[x,y] = mygrid(pi,2*pi)
x =

  Columns 1 through 13

   -3.1416   -3.1416   -3.1416   -3.1416   -3.1416   -3.1416   -3.1416   -3.1416   -3.1416   -3.1416   -3.1416   -3.1416   -3.1416
   -2.8274   -2.8274   -2.8274   -2.8274   -2.8274   -2.8274   -2.8274   -2.8274   -2.8274   -2.8274   -2.8274   -2.8274   -2.8274
   -2.5133   -2.5133   -2.5133   -2.5133   -2.5133   -2.5133   -2.5133   -2.5133   -2.5133   -2.5133   -2.5133   -2.5133   -2.5133
   -2.1991   -2.1991   -2.1991   -2.1991   -2.1991   -2.1991   -2.1991   -2.1991   -2.1991   -2.1991   -2.1991   -2.1991   -2.1991
   -1.8850   -1.8850   -1.8850   -1.8850   -1.8850   -1.8850   -1.8850   -1.8850   -1.8850   -1.8850   -1.8850   -1.8850   -1.8850
   -1.5708   -1.5708   -1.5708   -1.5708   -1.5708   -1.5708   -1.5708   -1.5708   -1.5708   -1.5708   -1.5708   -1.5708   -1.5708
   -1.2566   -1.2566   -1.2566   -1.2566   -1.2566   -1.2566   -1.2566   -1.2566   -1.2566   -1.2566   -1.2566   -1.2566   -1.2566
   -0.9425   -0.9425   -0.9425   -0.9425   -0.9425   -0.9425   -0.9425   -0.9425   -0.9425   -0.9425   -0.9425   -0.9425   -0.9425
   -0.6283   -0.6283   -0.6283   -0.6283   -0.6283   -0.6283   -0.6283   -0.6283   -0.6283   -0.6283   -0.6283   -0.6283   -0.6283
   -0.3142   -0.3142   -0.3142   -0.3142   -0.3142   -0.3142   -0.3142   -0.3142   -0.3142   -0.3142   -0.3142   -0.3142   -0.3142
         0         0         0         0         0         0         0         0         0         0         0         0         0
    0.3142    0.3142    0.3142    0.3142    0.3142    0.3142    0.3142    0.3142    0.3142    0.3142    0.3142    0.3142    0.3142
    0.6283    0.6283    0.6283    0.6283    0.6283    0.6283    0.6283    0.6283    0.6283    0.6283    0.6283    0.6283    0.6283
    0.9425    0.9425    0.9425    0.9425    0.9425    0.9425    0.9425    0.9425    0.9425    0.9425    0.9425    0.9425    0.9425
    1.2566    1.2566    1.2566    1.2566    1.2566    1.2566    1.2566    1.2566    1.2566    1.2566    1.2566    1.2566    1.2566
    1.5708    1.5708    1.5708    1.5708    1.5708    1.5708    1.5708    1.5708    1.5708    1.5708    1.5708    1.5708    1.5708
    1.8850    1.8850    1.8850    1.8850    1.8850    1.8850    1.8850    1.8850    1.8850    1.8850    1.8850    1.8850    1.8850
    2.1991    2.1991    2.1991    2.1991    2.1991    2.1991    2.1991    2.1991    2.1991    2.1991    2.1991    2.1991    2.1991
    2.5133    2.5133    2.5133    2.5133    2.5133    2.5133    2.5133    2.5133    2.5133    2.5133    2.5133    2.5133    2.5133
    2.8274    2.8274    2.8274    2.8274    2.8274    2.8274    2.8274    2.8274    2.8274    2.8274    2.8274    2.8274    2.8274
    3.1416    3.1416    3.1416    3.1416    3.1416    3.1416    3.1416    3.1416    3.1416    3.1416    3.1416    3.1416    3.1416

  Columns 14 through 21

   -3.1416   -3.1416   -3.1416   -3.1416   -3.1416   -3.1416   -3.1416   -3.1416
   -2.8274   -2.8274   -2.8274   -2.8274   -2.8274   -2.8274   -2.8274   -2.8274
   -2.5133   -2.5133   -2.5133   -2.5133   -2.5133   -2.5133   -2.5133   -2.5133
   -2.1991   -2.1991   -2.1991   -2.1991   -2.1991   -2.1991   -2.1991   -2.1991
   -1.8850   -1.8850   -1.8850   -1.8850   -1.8850   -1.8850   -1.8850   -1.8850
   -1.5708   -1.5708   -1.5708   -1.5708   -1.5708   -1.5708   -1.5708   -1.5708
   -1.2566   -1.2566   -1.2566   -1.2566   -1.2566   -1.2566   -1.2566   -1.2566
   -0.9425   -0.9425   -0.9425   -0.9425   -0.9425   -0.9425   -0.9425   -0.9425
   -0.6283   -0.6283   -0.6283   -0.6283   -0.6283   -0.6283   -0.6283   -0.6283
   -0.3142   -0.3142   -0.3142   -0.3142   -0.3142   -0.3142   -0.3142   -0.3142
         0         0         0         0         0         0         0         0
    0.3142    0.3142    0.3142    0.3142    0.3142    0.3142    0.3142    0.3142
    0.6283    0.6283    0.6283    0.6283    0.6283    0.6283    0.6283    0.6283
    0.9425    0.9425    0.9425    0.9425    0.9425    0.9425    0.9425    0.9425
    1.2566    1.2566    1.2566    1.2566    1.2566    1.2566    1.2566    1.2566
    1.5708    1.5708    1.5708    1.5708    1.5708    1.5708    1.5708    1.5708
    1.8850    1.8850    1.8850    1.8850    1.8850    1.8850    1.8850    1.8850
    2.1991    2.1991    2.1991    2.1991    2.1991    2.1991    2.1991    2.1991
    2.5133    2.5133    2.5133    2.5133    2.5133    2.5133    2.5133    2.5133
    2.8274    2.8274    2.8274    2.8274    2.8274    2.8274    2.8274    2.8274
    3.1416    3.1416    3.1416    3.1416    3.1416    3.1416    3.1416    3.1416


y =

  Columns 1 through 13

   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566
   -6.2832   -5.6549   -5.0265   -4.3982   -3.7699   -3.1416   -2.5133   -1.8850   -1.2566   -0.6283         0    0.6283    1.2566

  Columns 14 through 21

    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832
    1.8850    2.5133    3.1416    3.7699    4.3982    5.0265    5.6549    6.2832


Two outputs, x and y, are obtained as a result of this function.

Multiple Anonymous Functions

$$ g(c) = \int\limits_{0}^{1} (x^2+cx+1)dx $$
In [4]:
g = @(c) (integral(@(x)(x.^2 + c*x + 1),0,1));
g(2)
ans =

    2.3333


Function Functions

  • They are functions that require other functions as input.
  • One example of a MATLAB built-in function function is the function plot: fplot

    This function requires two inputs: a function or a function handle, and a range over which to plot.

In [5]:
poly5 = @(x) - 5*x.^5 + 400*x.^4 + 3*x.^3 + 20*x.^2 - x + 5;
fplot(poly5, [-30,90])

  • Another example of a MATLAB built-in function function is fzero. It tries to find a point x where $function(x) = 0$. This function requires a non-linear function, and an initial point.
In [6]:
fzero(poly5, 75)
ans =

   80.0081


Subfunctions

  • More complicated functions can be created by grouping functions together in a single file as subfunctions. These subfunctions can be called only from the primary function, so they have limited utility. Subfunctions can be used to modularize your code and to make the primary function easier to read.
In [ ]:
function [addition result, subtraction result] = subfunction demo(x, y)
% This function both adds and subtracts the elements stored in two arrays
addition result = add(x,y);
subtraction result = subtract(x,y);

function result = add(x,y) % subfunction plus
result = x + y;
end

function output = subtract(x,y) %subfunction minus
output = x - y;
end

end

Output Statements: disp

  • disp: Displays the result of an expression or a string
  • Example 1
In [7]:
disp('Hello')
Hello

  • Example 2
In [8]:
disp(exp(2))
    7.3891


  • Example 3
In [9]:
x=12;
y=-3;
disp(x+y)
     9


Output Statements: fprintf

  • fprintf: Prints formatted output to the screen
  • Example 1
In [10]:
cows = 5;
fprintf('There are %f cows in the pasture \n', cows)
There are 5.000000 cows in the pasture 

  • Example 2
In [11]:
fprintf('There are %d cows in the pasture \n', cows)
There are 5 cows in the pasture 

  • Example 3
In [12]:
fprintf('The value is %d, for sure! \n', cows)
The value is 5, for sure! 

  • In the first example below, % character denotes the start of a format specifier requesting a field of width 10 with 3 digits after the decimal point and \n denotes a new line.
  • For a negative number, a minus sign occupies one position of the field width.
In [13]:
fprintf('%10.3f\n', pi)
     3.142

In [14]:
fprintf('%10.4e\n', pi)
3.1416e+00

In [15]:
fprintf('%-5.0f\n%5.0f', 9, 103)
9    
  103
  • MATLAB will repeat the string in the fprintf command until it uses all the values in the matrix.
In [16]:
x=1:5;
fprintf('%10.2f\n', x);
      1.00
      2.00
      3.00
      4.00
      5.00

  • If the variable is a two-dimensional matrix, MATLAB uses the values one column at a time, going down the first column, then the second, and so on. Here’s a more complicated example:
In [17]:
feet = 1:3;
inches = feet.*12;
table = [feet;inches]
table =

     1     2     3
    12    24    36


In [18]:
fprintf('%7.0f %15.3f \n', table);
      1          12.000 
      2          24.000 
      3          36.000 

Type Field Format

Code Format
%f Fixed-point notation
%e Exponential notation
%d Decimal notation - does not include trailing zeros if the value displayed is an integer. If the number includes a fractional component, it is displayed using exponential notation.
%g Whichever is shorter, %f or %e
%c Character information (displays one character at a time)
%s String of characters (displays the entire string)

Output Statements: sprintf

  • The sprintf function is similar to fprintf, but instead of just sending the result of the formatted string to the command window, sprintf assigns it a name and sends it to the command window.
In [19]:
fprintf('Some example output is %10.2f \n', pi*1000)
Some example output is    3141.59 

In [20]:
sprintf('Some example output is %10.2f \n', pi*1000)
ans =

    'Some example output is    3141.59 
     '


Graphical Input

  • MATLAB offers a technique for entering ordered pairs of x and y values graphically.

    The ginput command allows the user to select points from a figure window and converts the points into the appropriate x and y coordinates.

  • [x,y] = ginput(n): Requests the user to select n points from the figure windows.
  • [x,y] = ginput: Accepts points until the return key is pressed.

User-defined Input

  • input('text-string'): Allows the user to input values of a matrix from the keyboard while the program is running.
  • It displays a text string in the command window and then waits for the user to provide the requested input.

    x = input('Enter a value')

  • If a character or string input is desired, 's' must be added after the prompt:

    y = input('Enter your name', 's')

Import/Export Data

  • If you select a data file from the current folder and double-click on the file name, the Import Wizard launches.
  • The Import Wizard determines what kind of data is in the file and suggests ways to represent the data in MATLAB.

    • xlsread('filename.xls'): Imports data from Excel

    • xlswrite('filename.xls', M): Exports data to Excel