Using subs() in a function when symbolic variables defined in another function

5 次查看(过去 30 天)
Hello Everyone! I'm having some problems using the subs() function and I need your help.
I defined a function to calculate a jacobian matrix using symbolic variables. For example:
function myJacobian = getJacobian()
% Define function variables
syms x y z
% Define myFunction
myFunction = x^2 + y^2 + z^2;
% Calculate myJacobian
myJacobian = jacobian(myFunction, [x,y,z]);
end
Now, in "main", I am calling this function and obtaining the jacobian. Then, I am substituting the symbolic variables (x,y,z) with some numbers in another function
% Get the jacobian matrix F
F = getJacobian;
% Get my result
value_F = someRandomFunc(F, someOtherInputs);
where "someRandomFunc" is given as
function value_F = someRandomFunc(F)
currentX = getX(someRandomInputs1);
currentY = getY(someRandomInputs2);
currentZ = getZ(someRandomInputs3);
value_F = subs(F, {x,y,z}, {currentX,currentY,currentZ});
end
However, I am getting the following error
"Unrecognized function or variable 'x'."
I know that I am getting this error because these symbolic variables are not defined in the workspace of "someRandomFunc". So my question is that it is possible to transfer these symbolic variables defined in "getJacobian()" function to "someRandomFunc" so that I can use subs() with complacency. I tried to simplify my functions so that you can understand the problem better. My "somRandomFunc" function is in a for loop. At every iteration I am obtaining different x,y,z values and calculating the jacobian for this specific iteration accordingly. I do not want to define symbolic variables in "someRandomFunc" since it slows down the program a lot. Thanks for the help in advance.
Kind regards.

采纳的回答

Paul
Paul 2022-9-25
Making F into a symfun seems to work ....
F = getJacobian
F(x, y, z) = 
valueF = someRandomFunc(F)
valueF = 
function myJacobian = getJacobian()
% Define function variables
syms x y z
% Define myFunction
myFunction = x^2 + y^2 + z^2;
% Calculate myJacobian
myJacobian(x,y,z) = jacobian(myFunction, [x,y,z]);
end
function value_F = someRandomFunc(F)
currentX = 1;
currentY = 2;
currentZ = 3;
value_F = F(currentX,currentY,currentZ);
end
  4 个评论
Paul
Paul 2022-9-25
I thought you wanted to stay in the sym realm. If you only want a numerical calculation of the Jacobian, then consider returning it as an anonymous function. Note that F is vectorized.
F = getJacobian
F = function_handle with value:
@(x,y,z)[x.*2.0,y.*2.0,z.*2.0]
valueF = someRandomFunc(F)
valueF = 1×3
2 4 6
function myJacobian = getJacobian()
% Define function variables
syms x y z
% Define myFunction
myFunction = x^2 + y^2 + z^2;
% Calculate myJacobian
myJacobian = matlabFunction(jacobian(myFunction, [x,y,z]),'Vars',{'x' 'y' 'z'});
end
function value_F = someRandomFunc(F)
currentX = 1;
currentY = 2;
currentZ = 3;
value_F = F(currentX,currentY,currentZ);
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Formula Manipulation and Simplification 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by