MATLAB: Help needed on using “solve” for equation including element from array

solve array equation numericalSymbolic Math Toolbox

Hello, I'm having problem when using "solve" function. For example,
clear a k x y;
a=[1, 2];
for k=1:1:2
y=subs(abs(solve('x^2+a(k)^2-9')))
end
Matlab gave me
y =
abs(a(2)^2-9)^(1/2)
abs(a(2)^2-9)^(1/2)
instead of numerical results. Of course, the real problem I'm trying to solve is much more complicated, and the array "a" is calculated by another code and actually a more complicated matrix, so I cannot manually assign each a(k) for a numerical number. Could anybody help me on this? I tried to search for an answer here but failed. Many thanks for any help.

Best Answer

syms x;
a=[1, 2];
for k=1:length(a)
y=abs(double(solve(x^2+a(k)^2-9)))
end
Alternately,
syms x A;
SolveX = matlabFunction(vectorize(abs(solve(x^2 + A^2 - 9, x))));
y = SolveX(a);
You might have problems with imaginary roots: if any a is greater than 3 then the roots become imaginary. If a is 3 exactly then there is a single root, 0: that could be a problem for vectorization.