usually, symbolic functions return vectors for vector inputs:
syms('x');
f=x*2;
subs(f,[1 2 3])
outputs: [2 4 6]
but doing
f=sym('0');
subs(f,[1 2 3]);
outputs: 0
and not: [0 0 0]
so basically, my questions is how do I make f behave as a “normal” symbolic function.
I can do something ugly like f=x-x to create a function that always returns zero, but is there a prettier way?
Actually, there is no way.
sym('0')creates a symbolic constant (0, in this case).subs()replaces all variables with each value from the given vector. However, you have no variables, sosubs()just returns the given symbolic constant.It gets better.
sym()internally does some simplification, sosym('0*x')orsym('x-x')both becomesym('0')and you get the exact same behavior. Similarly,sym('x/x')turns intosym('1')and you just get the scalar 1 back fromsubs()even if you pass it a vector.The only way you’re going to get around this is by writing a helper function that detects if the
size()of the output fromsubs()is less than the vector, and turns it into the correct size of a vector if needed.