Documentation Center

  • Trial Software
  • Product Updates

Generate MATLAB Functions

You can use matlabFunction to generate a MATLAB® function handle that calculates numerical values as if you were substituting numbers for variables in a symbolic expression. Also, matlabFunction can create a file that accepts numeric arguments and evaluates the symbolic expression applied to the arguments. The generated file is available for use in any MATLAB calculation, whether or not the computer running the file has a license for Symbolic Math Toolbox™ functions.

If you work in the MuPAD® Notebook app, see Create MATLAB Functions from MuPAD Expressions.

Generating a Function Handle

matlabFunction can generate a function handle from any symbolic expression. For example:

syms x y
r = sqrt(x^2 + y^2);
ht = matlabFunction(tanh(r))
ht = 
    @(x,y)tanh(sqrt(x.^2+y.^2))

You can use this function handle to calculate numerically:

ht(.5,.5)
ans =
    0.6089

You can pass the usual MATLAB double-precision numbers or matrices to the function handle. For example:

cc = [.5,3]; 
dd = [-.5,.5];
ht(cc, dd)
ans =
    0.6089    0.9954

Control the Order of Variables

matlabFunction generates input variables in alphabetical order from a symbolic expression. That is why the function handle in Generating a Function Handle has x before y:

ht = @(x,y)tanh((x.^2 + y.^2).^(1./2))

You can specify the order of input variables in the function handle using the vars option. You specify the order by passing a cell array of strings or symbolic arrays, or a vector of symbolic variables. For example:

syms x y z
r = sqrt(x^2 + 3*y^2 + 5*z^2);
ht1 = matlabFunction(tanh(r), 'vars', [y x z])
ht1 = 
    @(y,x,z)tanh(sqrt(x.^2+y.^2.*3.0+z.^2.*5.0))
ht2 = matlabFunction(tanh(r), 'vars', {'x', 'y', 'z'})
ht2 = 
    @(x,y,z)tanh(sqrt(x.^2+y.^2.*3.0+z.^2.*5.0))
ht3 = matlabFunction(tanh(r), 'vars', {'x', [y z]})
ht3 = 
    @(x,in2)tanh(sqrt(x.^2+in2(:,1).^2.*3.0+in2(:,2).^2.*5.0))

Generate a File

You can generate a file from a symbolic expression, in addition to a function handle. Specify the file name using the file option. Pass a string containing the file name or the path to the file. If you do not specify the path to the file, matlabFunction creates this file in the current folder.

This example generates a file that calculates the value of the symbolic matrix F for double-precision inputs t, x, and y:

syms x y t
z = (x^3 - tan(y))/(x^3 + tan(y));
w = z/(1 + t^2);
F = [w,(1 + t^2)*x/y; (1 + t^2)*x/y,3*z - 1];
matlabFunction(F,'file','testMatrix.m')

The file testMatrix.m contains the following code:

function F = testMatrix(t,x,y)
%TESTMATRIX
%    F = TESTMATRIX(T,X,Y)

t2 = x.^2;
t3 = tan(y);
t4 = t2.*x;
t5 = t.^2;
t6 = t5 + 1;
t7 = 1./y;
t8 = t6.*t7.*x;
t9 = t3 + t4;
t10 = 1./t9;
F = [-(t10.*(t3 - t4))./t6,t8; t8,- t10.*(3.*t3 - 3.*t2.*x) - 1];

matlabFunction generates many intermediate variables. This is called optimized code. MATLAB generates intermediate variables as a lowercase letter t followed by an automatically generated number, for example t32. Intermediate variables can make the resulting code more efficient by reusing intermediate expressions (such as t4, t6, t8, t9, and t10 in the calculation of F). Using intermediate variables can make the code easier to read by keeping expressions short.

If you don't want the default alphabetical order of input variables, use the vars option to control the order. Continuing the example,

matlabFunction(F,'file','testMatrix.m','vars',[x y t])

generates a file equivalent to the previous one, with a different order of inputs:

function F = testMatrix(x,y,t)
...

Name Output Variables

By default, the names of the output variables coincide with the names you use calling matlabFunction. For example, if you call matlabFunction with the variable F

syms x y t
z = (x^3 - tan(y))/(x^3 + tan(y));
w = z/(1 + t^2);
F = [w, (1 + t^2)*x/y; (1 + t^2)*x/y,3*z - 1];
matlabFunction(F,'file','testMatrix.m','vars',[x y t])

the generated name of an output variable is also F:

function F = testMatrix(x,y,t)
...

If you call matlabFunction using an expression instead of individual variables

syms x y t
z = (x^3 - tan(y))/(x^3 + tan(y));
w = z/(1 + t^2);
F = [w,(1 + t^2)*x/y; (1 + t^2)*x/y,3*z - 1];
matlabFunction(w + z + F,'file','testMatrix.m',...
'vars',[x y t])

the default names of output variables consist of the word out followed by the number, for example:

function out1 = testMatrix(x,y,t)
...

To customize the names of output variables, use the output option:

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

The generated function returns name1 and name2 as results:

function [name1,name2] = new_function(x,y,z)
...

Convert MuPAD Expressions

You can convert a MuPAD expression or function to a MATLAB function:

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

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

function f = new_function(x,y)
%NEW_FUNCTION
%    F = NEW_FUNCTION(X,Y)

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

    Tip   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 results of conversion. To verify the results, execute the resulting function.

Was this topic helpful?