matlabFunction

Convert symbolic expression to function handle or file

Syntax

g = matlabFunction(f)
g = matlabFunction(f1,...,fN)
g = matlabFunction(f,Name,Value)
g = matlabFunction(f1,...,fN,Name,Value)

Description

g = matlabFunction(f) converts the symbolic expression or function f to a MATLAB® function with the handle g.

g = matlabFunction(f1,...,fN) converts a vector of the symbolic expressions or functions f1,...,fN to a MATLAB function with multiple outputs. The function handle is g.

g = matlabFunction(f,Name,Value) converts the symbolic expression or function f to a MATLAB function using additional options specified by one or more Name,Value pair arguments.

g = matlabFunction(f1,...,fN,Name,Value) converts a vector of the symbolic expressions or functions f1,...,fN to a MATLAB function with multiple outputs using additional options specified by one or more Name,Value pair arguments.

Input Arguments

f

Symbolic expression or function.

f1,...,fN

Vector of symbolic expressions or functions.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

'file'

Generate a file with optimized code. The generated file can accept double or matrix arguments and evaluate the symbolic expression applied to the arguments. Optimized means intermediate variables are automatically generated to simplify or speed up the code. MATLAB generates intermediate variables as a lowercase letter t followed by an automatically generated number, for example t32. The value of this parameter must be a string representing the path to the file.

Default: If the value string is empty, matlabFunction generates an anonymous function. If the string does not end in .m, the function appends .m.

'outputs'

Specify the names of output variables. The value must be a cell array of strings.

Default: The names of output variables coincide with the names you use calling matlabFunction. If you call matlabFunction using an expression instead of individual variables, the default names of output variables consist of the word out followed by a number, for example, out3.

'vars'

Specify the order of the input variables or symbolic vectors in the resulting function handle or the file. The value of this parameter must be either a cell array of strings or symbolic arrays, or a vector of symbolic variables. The number of value entries must equal or exceed the number of free variables in f.

Default: When converting symbolic expressions, the order is alphabetical. When converting symbolic functions, the input arguments appear in front of other variables. Other variables are sorted alphabetically.

Output Arguments

g

MATLAB function handle.

Examples

Convert this symbolic expression to a MATLAB function with the handle ht:

syms x y
r = sqrt(x^2 + y^2);
ht = matlabFunction(sin(r)/r)
ht = 
   @(x,y)sin(sqrt(x.^2+y.^2)).*1.0./sqrt(x.^2+y.^2)

Create this symbolic function:

syms x y
f(x, y) = x^3 + y^3;

Convert f to a MATLAB function:

ht = matlabFunction(f)
ht = 
    @(x,y)x.^3+y.^3

Convert this expression to a MATLAB function generating the file myfile that contains the function:

syms x y z
r = x^2 + y^2 + z^2;
f = matlabFunction(log(r)+r^(-1/2),'file','myfile');

If the file myfile.m already exists in the current folder, matlabFunction replaces the existing function with the converted symbolic expression. You can open and edit the resulting file:

function out1 = myfile(x,y,z)
%MYFILE
%    OUT1 = MYFILE(X,Y,Z)

t2 = x.^2;
t3 = y.^2;
t4 = z.^2;
t5 = t2 + t3 + t4;
out1 = log(t5) + 1.0./sqrt(t5);

Convert this expression to a MATLAB function using an empty string to represent a path to the file. An empty string causes matlabFunction to generate an anonymous function:

syms x y z
r = x^2 + y^2 + z^2;
f = matlabFunction(log(r)+r^(-1/2),'file','')
f = 
    @(x,y,z)log(x.^2+y.^2+z.^2)+1.0./sqrt(x.^2+y.^2+z.^2)

When converting this expression to a MATLAB function, specify the order of the input variables:

syms x y z
r = x^2 + y^2 + z^2;
matlabFunction(r, 'file', 'my_function',...
'vars', [y z x]);

The created my_function accepts variables in the required order:

function r = my_function(y,z,x)
%MY_FUNCTION
%    R = MY_FUNCTION(Y,Z,X)

r = x.^2 + y.^2 + z.^2;

When converting this expression to a MATLAB function, specify its second input argument as a vector:

syms x y z t
r = (x^2 + y^2 + z^2)*exp(-t);
matlabFunction(r, 'file', 'my_function',...
'vars', {t, [x y z]});

The resulting function operates on vectors:

function r = my_function(t,in2)
%MY_FUNCTION
%    R = MY_FUNCTION(T,IN2)

x = in2(:,1);
y = in2(:,2);
z = in2(:,3);
r = exp(-t).*(x.^2+y.^2+z.^2);

When converting this expression to a MATLAB function, specify the names of the output variables:

syms x y z
r = x^2 + y^2 + z^2;
q = x^2 - y^2 - z^2;
f = matlabFunction(r, q, 'file', 'my_function',...
'outputs', {'name1','name2'});

The generated function returns name1 and name2:

function [name1,name2] = my_function(x,y,z)
%MY_FUNCTION
%    [NAME1,NAME2] = MY_FUNCTION(X,Y,Z)

t2 = x.^2;
t3 = y.^2;
t4 = z.^2;
name1 = t2+t3+t4;
if nargout > 1
    name2 = t2-t3-t4;
end

Convert this MuPAD® expression to a MATLAB function:

syms x y
f = evalin(symengine, 'arcsin(x) + arccos(y)');
matlabFunction(f, 'file', 'my_function');

The generated file contains the same expressions written in the MATLAB language:

function f = my_function(x,y)
%MY_FUNCTION
%    F = MY_FUNCTION(X,Y)

f = asin(x) + acos(y);

More About

expand all

Tips

  • To convert a MuPAD expression or function to a MATLAB function, use f = evalin(symengine,'MuPAD_Expression') or f = feval(symengine,'MuPAD_Function',x1,...,xn). matlabFunction cannot correctly convert some MuPAD expressions to MATLAB functions. These expressions do not trigger an error message. When converting a MuPAD expression or function that is not on the MATLAB vs. MuPAD Expressions list, always check the conversion results. To verify the results, execute the resulting function.

  • When you use the file argument, use rehash to make the generated function available immediately. rehash updates the MATLAB list of known files for directories on the search path.

Was this topic helpful?