Wrong function from matlabFunction
显示 更早的评论
I would like to convert the symbolic function f to a Matlab function in a file. With the 'Optimize' flag false I get the right result, but for true I get a strange matlab function file.
syms x y z
%
f = - (64*((8*sin(8*x*y*z) - 8*x*y^2*sin(8*x*y*z) + 64*x*y*z*cos(8*x*y*z))/...
(256*x^2 + y^2 + 16*z^2)^(1/2) - (16*z*(y*cos(8*x*y*z) + 8*z*sin(8*x*y*z) + 16*x*cos(8*x*z)))/...
(256*x^2 + y^2 + 16*z^2)^(3/2)))/(256*x^2 + y^2 + 16*z^2) - ((y^2 + 16*z^2)*((16*sin(8*y)*cos(8*x*z) + 64*y*z^2*cos(8*x*y*z)...
- 8*y^2*z*sin(8*x*y*z) - 128*x*z*sin(8*y)*sin(8*x*z))/(256*x^2 + y^2 + 16*z^2)^(1/2) - (256*x*(y*cos(8*x*y*z) + 8*z*sin(8*x*y*z)...
+ 16*x*sin(8*y)*cos(8*x*z)))/(256*x^2 + y^2 + 16*z^2)^(3/2))) + (16*x*y*( - (y*(16*x*sin(8*y)))))...
- ((64*x*z*((16*sin(8*y)*cos(8*x*z) + 64*y*z^2*cos(8*x*y*z) - 8*y^2*z*sin(8*x*y*z) - 128*x*z*sin(8*y)*sin(8*x*z))/...
(256*x^2 + y^2 + 16*z^2)^(1/2) - (256*x*(y*cos(8*x*y*z) + 8*z*sin(8*x*y*z) + 16*x*sin(8*y)*cos(8*x*z)))/...
(256*x^2 + y^2 + 16*z^2)^(3/2)))/(256*x^2 + y^2 + 16*z^2) - (((8*sin(8*x*y*z) - 128*x^2*sin(8*y)*sin(8*x*z)...
- 8*x*y^2*sin(8*x*y*z) + 64*x*y*z*cos(8*x*y*z))/(256*x^2 + y^2 + 16*z^2)^(1/2) - (16*(cos(8*x*y*z) + 8*z*sin(8*x*y*z)...
+ 16*x*sin(8*y)*cos(8*x*z))))*(256*x^2 + y^2)) + (4*y*z*((cos(8*x*y*z)))));
%
double(subs(f,[x,y,z],[1,1,1]))
%
matlabFunction(f,'File','test_fun.m','Vars',[x y z],'Optimize',false);
test_fun(1,1,1)
%
matlabFunction(f,'File','test_fun.m','Vars',[x y z],'Optimize',true);
test_fun(1,1,1)
For me the function evaluations are:
ans =
-2.466656681506189e+04
ans =
-2.466656681506189e+04
ans =
-0.310224932180499
1 个评论
Matt J
2020-1-29
but for true I get a strange result.
which is...?
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!