MATLAB: How to store symbolic variables and symbolic functions in a array

arrayfunction

How can I store symbolic variables and symbolic functions in a array?

Best Answer

You cannot mix symbolic expressions and symbolic functions in a single array unless it is a composite data structure such as a cell array or structure array.
You also cannot store multiple symbolic functions in a single array unless it is a composite data structure such as a cell array.
If you attempt to store multiple symbolic functions in a single non-composite array, and they do not all use the same set of variables as arguments, then MATLAB will issue an error. If they do all use the same set of variables as arguments, then instead of creating an array of functions, MATLAB will create a single function that returns an array.
MATLAB uses the same syntax of round brackets for indexing and function calls. If you had an array of functions, should A(5) be extracting the 5th function from the array or should it be calling a function passing in 5? A(5)(7) to hypothetically extract the 5th function and invoke it with parameter 7 is not valid syntax in MATLAB.
MATLAB handles this by saying that () of a symbolic function is always invoking, never indexing. Therefore to be able to separate the functions you must use a cell array as A{5}(7) is valid.
Related Question