MATLAB: Solve vs. fsolve

solve

I am trying to solve non linear equations with solve or fsolve.
But I found difficulties in using them.
for example,
i would like to solve the equation below.
a^2 = 4
then, i can get the answer with the script below.
syms a;
eqn1 = a^2 == 4;
[a1] = solve(eqn1);
then, it gives me a = -2, 2
but when I used fsolve, it gives me only one value according to initial point.
this is the script.
fun = @test;
x0 = [1];
[a2] = fsolve(fun,x0)
function F = test(x)
F(1) = -x(1)^2 + 4;
end
if I set x0 as [-1], then it gives me another value but still only one value.
How can I get the all values with fsolve?

Best Answer

Give fsolve different starting points, one positive and one negative:
eqn1 = @(x) x.^2 - 4;
for k = 1:2
a2(k) = fsolve(eqn1, 5*(-1)^k);
end
a2
producing:
a2 =
-2 2