matlabsymbolic-math

How to generate function with trivial dependency


Given

syms x y z ;
w = 3 * z
wn = matlabFunction(w)

Matlab actually produces a function of only z, so that wn(1,1,1) gets "too many arguments".

How can I get it to produce a function that depends on all of x, y and z?


Note - I need to do this so that I can feed this function into another process that takes functions of x, y, z that might or might not strictly depend on each. I also want the code to mimic the algebra. I am not looking for someone to say - well, show me the whole code and I will rearrange it. I am quite able to rewrite the code by manually making sure that the right arguments are provided. Or having a configuration structure. Or using strings to force the issue. And so on. What I am looking for is a method to get Matlab to simply give me a function with the right argument list instead of being too smart for its own good.


Solution

  • In matlabFunction you can use the optional 'Vars' input to specify which input variables the produced anonymous function should have:

    wn = matlabFunction(w, 'Vars', {x y z});