bug in matlabFunction when passing unspecified symbolic function?

2 ビュー (過去 30 日間)
dominik
dominik 2014 年 5 月 21 日
回答済み: dominik 2014 年 5 月 23 日
Hi
the function matlabFunction seems to do a mistake with undefined functions.
It seems to work all right if there is only one reference to the unknown function, but not if there is more than one. This is most easily seen by testing this code: only the first call of matlabFunction gives me the right output.
clear all
syms x y z f(x,y,z)
E=x + y + f(x,y,z)
D1= diff(E,x)
D2= [diff(E,x) diff(E,y) diff(E,z)]
matlabFunction(D1,'file','NlconFunc','vars',{x,y,z});
matlabFunction(D2,'file','NlconFunc','vars',{x,y,z});
For the first call of matlabfuction the generated code is as i would expect it
function D1 = NlconFunc(x,y,z)
%NLCONFUNC
% D1 = NLCONFUNC(X,Y,Z)
% This function was generated by the Symbolic Math Toolbox version 6.0.
% 21-May-2014 18:46:17
D1 = diff(f(x,y,z),x)+1.0;
For the last call of matlabfunction the references to the derivative of f are missing. the generated code is:
function D3 = NlconFunc(x,y,z)
%NLCONFUNC
% D3 = NLCONFUNC(X,Y,Z)
% This function was generated by the Symbolic Math Toolbox version 6.0.
% 21-May-2014 17:03:56
t2 = f(x,y,z);
D3 = [1.0,1.0,0.0];
Is this a bug (i tried 2013b 64bit and 2014a 32bit) or what did I do wrong?

回答 (1 件)

dominik
dominik 2014 年 5 月 23 日
I still think it's a bug but as a workaround, one can define auxiliary variables
syms Dfx Dfy Dfz
and substitute them for the expressions
diff(f(x,y,z),x)
diff(f(x,y,z),y)
diff(f(x,y,z),z)
using
subs(D2, [diff(f(x,y,z),x) diff(f(x,y,z),y) diff(f(x,y,z),z)], [Dfx Dfy Dfz])

カテゴリ

Help Center および File ExchangeNumbers and Precision についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by