MATLAB Programming
Module-3
Inline Functions
Working with anonymous functions Symbolic Computations
Importing and exporting data Working with files and directories
Creating and Using Inline Functions
• The syntax for creating an inline-function is particularly
simple:
F = inline(’function formula’)
• Thus, a function such as F(x)=x2sin(x) can be coded as
F = inline(’x^2 * sin(x)’)
Anonymous Function
• An anonymous function is a function of one or
more variables that you create on the
command line for subsequent evaluation
• Such a function is especially useful if you need
to evaluate the function several times (with
different input) during a single MATLAB
session and you do not care to code it in a
function file and save for later use
• f(x)= x3 - 32 x2 + (x - 22)x + 100
• evaluate this function at different values of x,
plot it over certain range of x, or find the zeros
of f (x)
• For such computations, you can define f (x) as
an anonymous function and evaluate it at any
value(s) of x or use this function as an input to
other functions that need such input.
What you are going to learn
• How to define an anonymous function of a
single variable.
• How to evaluate an anonymous function with
scalar or array arguments.
• How to define an anonymous function of
several variables.
• How to use anonymous functions as input to
other functions.
Method
• The key to anonymous functions is the syntax of its creation:
• fn_name = @(list of input variables) function_expression
• Here, the key element is the symbol @that assigns a function
handle to the defined function.
• A function handle is a name given to a function by which you
can call it wherever you need it.
• In the anonymous function definition line, fn_name is the
name of the function or the handle of the function
• The syntax @(list of input variables) is what tells MATLAB
that you are defining an anonymous function here.
• Anonymous functions are defined on the command line.
They live in the MATLAB workspace and are alive as long as
the MATLAB session lasts.
• You can define an anonymous function with any number of
input variables.
• You must use a vectorized expression (using array
operators) for the function if you intend to use an array as
an input variable.
• You can use anonymous functions as input to other
functions where appropriate
• Evaluate the function at x =0, i.e. , find f(0)
• Evaluate the function at x = 1. Note that f is
singular at x = 1.
• You can use f in an array also
values= [f(0) f(1) f(2) f(3)]
• Using an array as the input to f causes an
error. This is because the expression for f is
not vectorized
• x=[0 1 2 10];
f(x) ---------------------- Error?????
• Redefine f by vectorizing the expression (use
array operators). Now ans = use it with an
array argument
• f(x)
• You can also use f as input to other functions
where appropriate.
• x=linspace(-10,10);
• plot(x,f(x))
• Define a function of two variables
f(μ,x) = μ x - x3 and evaluate over a range of x for different values
of μ
f=@(mu,x) mu*x-x.^3
x=linspace(-5,5)‘
f_many=[f(-5,x) f(0,x) f(5,x) f(45,x)];
plot(x,f_many)
grid
xlabel('x')
ylabel('f(\mu,x)=\mu x-x^3')
Symbolic Computations
• To learn how to do simple symbolic algebra in MATLAB.
Such calculations are done with symbolic variables
without resorting to their numerical values.
• How to create symbolic variables and use them in
defining functions.
• How to manipulate expressions using symbolic math
functions expand, simplify, subs, factor, pretty, etc.
• How to differentiate and integrate functions symbolically.
• How to solve simultaneous linear and nonlinear
equations.
• The most important step in carrying out
symbolic computation is to declare the
independent variables to be symbolic before you
do anything with them.
• Suppose you want to use x and y as symbolic
variables. Then, you can declare them to be
symbolic using any of the commands that follow
• x = sym ( ' x ' ) ; y = sym ( 'y' )
• syms x y
• The command sym is the formal command for
constructing objects of symbol class.
• You can define one symbolic variable at a time
using this command
• You can use additional qualifiers in the definition
for the symbolic object (e.g., for restricting a
variable to be only real or positive) .
• The command syms is a shortcut to declare
several variables of the same class in one shot
• There are three basic skills to learn in symbolic
computations
(i) How to define expressions and do simple
algebra with them (multiply, divide, expand,
factorize, sirnplify, etc.).
(ii) How to do simple calculus (differentiate,
integrate, etc.).
(iii) How to solve equations-algebraic and
differential equatiouns
Importing and exporting data
• To learn how to read data from common data
files into MATLAB workspace and how to save
data into a MATLAB readable file
What you are going to learn
• How to save data (computed variables) into a Mat-file, the
MATLAB's native binary format.
• How to read all variables or some selected variables from
data stored in a Mat-file.
• How to write input data as text in a script file (an M-file) and
run the script file to load the data in MATLAB workspace.
• How to read mixed data-text and numbers-from an Excel
spreadsheet and how to decipher the data read
automatically into a cell array
• We are going to work with three different kinds of
files for reading data into MATLAB's workspace
• Mat-file: This is MATLAB's native binary format file
for saving data. Two commands save and load make
it particularly easy to save data into and load data
from these files.
• In this lesson, we will create such a file and save
some data into it in named variables. We will then
use this file to load desired data back into the
MATLAB workspace
Working with files and directories.
• To learn how to navigate through MATLAB directory
system and work with files in a particular location.
• What you are going to learn
• How to find your bearings in the jungle of directories.
• How to see the contents of a directory.
• How to create a new directory and change the current
directory.
• How to copy a file from one directory to another.
• How to see the list of only MATLAB-related files.