Documentation

matlabFunction

Convert symbolic expression to function handle or file

Syntax

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

Description

example

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

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

example

g = matlabFunction(___,Name,Value) converts the symbolic expression, function, or a vector of the symbolic expressions or functions to a MATLAB function using additional options specified by one or more Name,Value pair arguments. You can specify Name,Value after the input arguments used in the previous syntaxes.

Examples

Convert Symbolic Expression to Function Handle

Convert a symbolic expression to an anonymous MATLAB function.

Create a symbolic expression.

syms x y
r = sqrt(x^2 + y^2);

Convert it to a MATLAB function with the handle ht.

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

Convert Symbolic Functions to Function Handle

Convert a symbolic function to an anonymous MATLAB function.

Create a symbolic function.

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

Convert f to a MATLAB function with the handle ht.

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

Write Generated MATLAB Function to File

Convert a symbolic expression to a MATLAB function and write it to a file.

Create a symbolic expression.

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

Convert r to a MATLAB function writing this function to the 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.

f = matlabFunction(log(r)+r^(-1/2),'File','myfile');
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);

If a path to the file is an empty string, then matlabFunction does not create a file. It generates an anonymous function instead.

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)

Disable Code Optimization

When you convert a symbolic expression to a MATLAB function and write the resulting function to a file, matlabFunction optimizes the code by default. This approach can help simplify and speed up further computations that use the file. Nevertheless, generating the optimized code from some symbolic expressions and functions can be very time-consuming. Use Optimize to disable code optimization.

Create a symbolic expression.

syms x
r = x^2*(x^2 + 1);

Convert r to a MATLAB function and write the function to the file myfile. By default, matlabFunction creates a file containing the optimized code.

f =  matlabFunction(r,'File','myfile');
function r = myfile(x)
%MYFILE
%    R = MYFILE(X)

t2 = x.^2;
r = t2.*(t2+1.0);

Disable the code optimization by setting the value of Optimize to false.

f =  matlabFunction(r,'File','myfile','Optimize',false);
function r = myfile(x)
%MYFILE
%    R = MYFILE(X)

r = x.^2.*(x.^2+1.0);

Specify Input Arguments for Generated Function

When converting an expression to a MATLAB function, you can specify the order of the input arguments of the resulting function. You also can specify that some input arguments are vectors instead of single variables.

Create a symbolic expression.

syms x y z
r = x + y/2 + z/3;

Convert r to a MATLAB function writing this function to the file myfile that accepts variables in the specified order.

matlabFunction(r, 'File', 'myfile','Vars', [y z x]);
function r = myfile(y,z,x)
%MYFILE
%    R = MYFILE(Y,Z,X)

r = x+y.*(1.0./2.0)+z.*(1.0./3.0);

Convert an expression r to a MATLAB function whose second input argument is a vector.

syms x y z t
r = (x + y/2 + z/3)*exp(-t);
matlabFunction(r, 'File', 'myfile','Vars', {t, [x y z]});
function r = myfile(t,in2)
%MYFILE
%    R = MYFILE(T,IN2)

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

Specify Output Variables

When converting a symbolic expression to a MATLAB function, you can specify the names of the output variables.

Create symbolic expressions r and q.

syms x y z
r = x^2 + y^2 + z^2;
q = x^2 - y^2 - z^2;

Convert r and q to a MATLAB function and write the resulting function to a file myfile that returns a vector of two elements, name1 and name2.

f = matlabFunction(r, q, 'File', 'myfile',...
'Outputs', {'name1','name2'});
function [name1,name2] = myfile(x,y,z)
%MYFILE
%    [NAME1,NAME2] = MYFILE(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 MuPAD Expression to MATLAB Function

You can convert MuPAD® expressions to MATLAB functions.

Use evalin to evaluate the MuPAD expression arcsin(x) + arccos(y) in the MATLAB Command Window.

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

Now, use matlabFunction to convert the resulting expression to a MATLAB function. The file myfile contains the expression written in the MATLAB language.

matlabFunction(f, 'File', 'myfile');
function f = myfile(x,y)
%MYFILE
%    F = MYFILE(X,Y)

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

Related Examples

Input Arguments

collapse all

f — Symbolic input to be converted to MATLAB functionsymbolic expression | symbolic function

Symbolic input to be converted to a MATLAB function, specified as a symbolic expression or function.

f1,...,fN — Symbolic input to be converted to MATLAB function with N outputssymbolic expressions | symbolic functions

Symbolic input to be converted to MATLAB function with N outputs, specified as 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.

Example: matlabFunction(f,'File','myfile','Optimize',false)

'File' — Path to file containing generated MATLAB functionstring

Path to the file containing the generated MATLAB function, specified as a string. The generated function accepts arguments of type double, and can be used without Symbolic Math Toolbox™. If the value string is empty, matlabFunction generates an anonymous function. If the string does not end in .m, the function appends .m.

By default, matlabFunction with the File argument generates a file containing optimized code. 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. To disable code optimization, use the Optimize argument.

'Optimize' — Flag that lets you prevent optimization for code written into function filetrue (default) | false

Flag that lets you prevent optimization for code written into a function file, specified as false or true.

By default, matlabFunction with the File argument generates a file containing optimized code. 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.

matlabFunction without the File argument (or with a file path specified by an empty string) creates a function handle. In this case, the code is not optimized. If you try to enforce code optimization by setting Optimize to true, then matlabFunction throws an error.

'Outputs' — Names of output variablescell array of strings

Names of output variables, specified as a cell array of strings.

If you do not specify the output variable names, then they coincide with the names you use when 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' — Order of input variables or vectors in generated MATLAB functioncell array of strings or symbolic arrays | vector of symbolic variables

Order of input variables or vectors in generated MATLAB function, specified as 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.

By default, when you convert symbolic expressions, the order is alphabetical. When you convert symbolic functions, the input arguments appear in front of other variables, and all other variables are sorted alphabetically.

Output Arguments

collapse all

g — Function handle that can serve as input argument to numerical functionsMATLAB function handle

Function handle that can serve as an input argument to numerical functions, returned as a MATLAB function handle.

More About

collapse 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 Differences Between MATLAB and MuPAD Syntax 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.

Introduced in R2008b

Was this topic helpful?