Symbolic matlabFunction() generates a non-working function for my problem

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'.
Error in fcn1 (line 16)
t10 = t+t7;
^^^^^^^^^^^
Why did matlabFunction() skip the t parameter?

 Risposta accettata

syms t real
l = [0;0;t]
l = 
syms x y z real
r = [x;y;z];
sep = r-l
sep = 
dl = diff(l,t)
dl = 
integrand = simplify(cross(dl,sep)./norm(sep).^3,"Steps",100)
integrand = 
Bsym = int(integrand,t,[0 Inf])
Bsym = 
symvar(Bsym)
ans = 
In Bsym, t appears only as a variable in a limit() call.
matlabFunction() cannot deal with limit properly, as limit() is inherently a symbolic call that cannot be translated to pure numeric functions.

3 Commenti

Your equations have the possibility that x and y might be 0, in which case the denominator becomes ((t-z)^2)^(3/2) . But if z is positive then because t runs from 0 to inf, at some point t-z will be 0, leading to an all-zero denominator, which you will have trouble integrating over.
Note by the way, that with x and y both real, that involves the square root of a quantity that is at most 0 (if x and y are both 0) and is otherwise negative. So involves mostly complex quantities. The limit() calls generated involve conditionals on which is true if and only if x and y are both 0, and otherwise is a comparison to a complex quantity which is fallse. matlabFunction() really doesn't do that well on limit() calls.
"matlabFunction() cannot deal with limit properly,"
Why can't matlabFunction just punt with a message that "limit" can't be in the input expression?
Maybe you can directly use the antiderivatives:
syms x y z t
int(-y/((t-z)^2+y^2+x^2)^(3/2),t)
ans = 
int(x/((t-z)^2+y^2+x^2)^(3/2),t)
ans = 

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2025b

Richiesto:

il 10 Mar 2026 alle 5:16

Modificato:

il 10 Mar 2026 alle 14:28

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by