| Contents | Index |
g = matlabFunction(f)
g = matlabFunction(f1,...,fN)
g = matlabFunction(f,Name,Value)
g = matlabFunction(f1,...,fN,Name,Value)
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.
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.
f |
Symbolic expression or function. |
f1,...,fN |
Vector of symbolic expressions or functions. |
Specify optional comma-separated pairs of Name,Value arguments, where 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.
g |
MATLAB function handle. |
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.^3Convert 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);
ccode | evalin | feval | fortran | matlabFunctionBlock | simscapeEquation | subs | sym2poly

See how symbolic computations can help you find analytical solutions to math and engineering problems.
Get free kit| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |