l =
Symbolic matlabFunction() generates a non-working function for my problem
Show older comments
I want to convert the symbolic expression to a MATLAB function, but the result of matlabFunction() leads to a non-working function with an unspecified parameter t:
syms t real
l = [0;0;t]
syms x y z real
r = [x;y;z];
sep = r-l
dl = diff(l,t)
integrand = simplify(cross(dl,sep)./norm(sep).^3,"Steps",100)
Bsym = int(integrand,t,[0 Inf])
fcn = matlabFunction(Bsym,'File','fcn1');
I received:
function Bsym = fcn1(x,y,z)
%FCN1
% Bsym = FCN1(X,Y,Z)
% This function was generated by the Symbolic Math Toolbox version 25.2.
% 10-Mar-2026 12:09:20
t2 = abs(y);
t3 = x.^2;
t4 = y.^2;
t5 = z.^2;
t7 = -z;
t6 = t2.^2;
t8 = -t3;
t9 = -t4;
t10 = t+t7;
t11 = -t6;
t12 = t10.^2;
t13 = t3+t6;
t16 = t8+t9;
t14 = t5+t13;
t17 = t8+t11;
t18 = t12+t13;
t20 = sqrt(t16);
t19 = 1.0./sqrt(t14);
t23 = t20+z;
t27 = 1.0./sqrt(t18);
t21 = t19.*z;
t30 = t10.*t27.*x;
t31 = t10.*t27.*y;
if ~all(cellfun(@isscalar,{t23,t8,y,z}))
error(message('symbolic:sym:matlabFunction:ConditionsMustBeScalar'));
end
if (~(0.0 <= t23))
t0 = -(y.*(t21+1.0))./t13;
elseif ((0.0 <= t23) & (y ~= 0.0))
t0 = -y./t13-limit(t31,t == z+sqrt(t17),Left)./t13+limit(t31,t == z+sqrt(t17),Right)./t13+(t7.*t19.*y)./t13;
elseif ((0.0 <= z+sqrt(t8)) & (y == 0.0))
t0 = 0.0;
else
t0 = NaN;
end
if ~all(cellfun(@isscalar,{t23,t9,x,z}))
error(message('symbolic:sym:matlabFunction:ConditionsMustBeScalar'));
end
if (~(0.0 <= t23))
t1 = (x.*(t21+1.0))./t13;
elseif ((0.0 <= t23) & (x ~= 0.0))
t1 = x./t13+limit(t30,t == z+sqrt(t17),Left)./t13-limit(t30,t == z+sqrt(t17),Right)./t13+(t21.*x)./t13;
elseif ((0.0 <= z+sqrt(t9)) & (x == 0.0))
t1 = 0.0;
else
t1 = NaN;
end
Bsym = [t0;t1;0.0];
end
But if you try, you'll get an error.:
fcn1(1,1,1)
Unrecognized function or variable 't'.
t10 = t+t7;
^^^^^^^^^^^
Why did matlabFunction() skip the t parameter?
Accepted Answer
More Answers (0)
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

