MATLAB: Symbolic expression with matrix input and output

MATLABmatricessymbolic

I want to symbolically define a matrix weighted norm like this:
Obviously I do not want to hard code the dimensions of vector and matrix.
I tried to define a symbolic expression like this:
C_mat_g_term_vec = sym('C_g_T_%d%d', [1, 18]);
C_mat_g_term = diag(C_mat_g_term_vec)
g_term_vec = sym('g_term_%d%d', [18, 1]);
% norm_mat_expr = @(v_in, W_in)((eye(size(v_in, 1), size(v_in,1))*v_in)).'*eye(size(W_in, 1))*W_in*(eye(size(v_in), size(v_in,1))*v_in)
norm_mat_expr = @(v_in, W_in)(v_in).'*W_in*(v_in)
norm_mat = sym(norm_mat_expr)
norm_mat(g_term_vec, C_mat_g_term)
Giving the error message:
Error using sym/subsindex (line 825)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.
R_tilde = builtin('subsref',L_tilde,Idx);
So apparently Matlab Sim-Engine handles symbols as scalars.
Does someone has a practical solution for this? 🙂
The concrete answer is given in the comments below:
C_mat_g_term_vec = sym('C_g_T_%d_%d', [1, 18]);
syms C_mat_g_term
C_mat_g_term = diag(C_mat_g_term_vec);
g_term_vec = sym('g_term_%d_%d', [18, 1]);
norm_mat_expr = @(v_in, W_in)(v_in).'*W_in*(v_in)
norm_mat_expr(g_term_vec, C_mat_g_term)

Best Answer

The symbolic engine does treat all unresolved symbols as scalars. That is hardcoded into the way it works internally and would be difficult to change. Therefore at the MATLAB level any symbolic function you define must be a function with particular size of arguments.
It looks to me as if you should not sym() the function handle and should instead pass particular arrays to it.