J =
Hi ludolfusexe,
It looks like the original code was attempting to use j as a index into an array, but symbolic variables are never allowed to be used as indices.
Here's the working code (I made m smaller to more easily see the final result)
syms x y p_jx p_jy phi_j
%m = 10;
m = 4;
p_j = sym('p', [m 2]);
phi = sym('phi', [m,1]);
J_j = sqrt((x-p_jx)^2+(y-p_jy)^2)*phi_j;
J = 0;
for j=1:m
tmp = subs(J_j,p_jx, p_j(j,1));
tmp = subs(tmp,p_jy, p_j(j,2));
tmp = subs(tmp,phi_j, phi(j) );
J = J + tmp;
end
J
Symbolic arrays support elementwise and other basic operations, which also support scalar expansion, just like standard numerical arrays. Hence, we can construct J as
J1 = sum(phi.*sqrt((x - p_j(:,1)).^2 + (y - p_j(:,2)).^2))
Check
isAlways(J1 == J)