Products & Services Solutions Academia Support User Community Company

Learn more about Symbolic Math Toolbox   

matlabFunction - Convert symbolic expression to function handle or file

Syntax

g = matlabFunction(f)
g = matlabFunction(f1, f2, ...)
g = matlabFunction(f,param1,value1,...)

Description

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

g = matlabFunction(f1, f2, ...) converts a list of the symbolic expressions f1, f2, ... to a MATLAB function with multiple outputs. The function handle is g.

g = matlabFunction(f,param1,value1,...) converts the symbolic expression f to a MATLAB function with the handle g. The command accepts the following options for parameter/value pairs:

Symbolic Math Toolbox with a Maple engine does not support matlabFunction. For details, see Differences in Functionality When Using MuPAD and Maple Engines.

Examples

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

The following example generates a file:

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 directory, 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)

%    This function was generated
%    by the Symbolic Math Toolbox version 5.2.
%    07-Nov-2008 14:38:59

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

You can change the order of the input variables:

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

The created new_function accepts variables in the required order:

function r = new_function(y,z,x)
%NEW_FUNCTION
%    R = NEW_FUNCTION(Y,Z,X)

%    This function was generated
%    by the Symbolic Math Toolbox version 5.2.
%    31-Oct-2008 18:13:19

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

You can specify that the input arguments are vectors:

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

The resulting function operates on vectors:

function r = new_function(t,in2)
%NEW_FUNCTION
%    R = NEW_FUNCTION(T,IN2)

%    This function was generated
%    by the Symbolic Math Toolbox version 5.2.
%    03-Nov-2008 18:19:37

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

You can name 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', 'new_function',...
'outputs', {'name1','name2'});

The generated function returns name1 and name2:

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

%    This function was generated
%    by the Symbolic Math Toolbox version 5.2.
%    31-Oct-2008 18:20:48

t9 = x.^2;
t10 = y.^2;
t11 = z.^2;
name1 = t10 + t11 + t9;
if nargout > 1
    name2 = t9 - t11 - t10;
end

Also, you can convert MuPAD expressions:

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)

%    This function was generated
%    by the Symbolic Math Toolbox version 5.2.
%    31-Oct-2008 17:41:12

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

See Also

ccode, fortran, subs, sym2poly, emlBlock

Generating Code from Symbolic Expressions

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS