# Vectorise NSolve

I am very new to Mathematica. What I am trying to do is the following. I have a two-dimensional surface which depends on an additional parameter. I would like to plot the locus of the saddle points, as the parameter is varied.

For simplicitys sake, the surface is given by

F[x_, y_, a_] := (a + x)^2 - y^2

where $$a$$ is the parameter.

What I did is to define functions for the partial derivatives

Fpartx[x_, a_] = D[F[x, y, a], x] Fparty[y_, a_] = D[F[x, y, a], y]

and then issue an Nsolve command to find the coordinates of the saddle point

s[a_] = NSolve[Fpartx[x, a] == 0 && Fparty [y, a] == 0 , {x, y}]

and I was hoping that applying the function $$s$$ to a list containing the values of $$a$$ I am interested in, I could get the saddle point coordinates to later plot.

But it does not work at all, I am completely unable to understand why. Not to add, I am sure there are far more elegant ways to achievem target, thanks for any suggestions.

Thanks

• Perhaps SetAttributes[s, Listable] would do it. You'd still have to do something like Join @@ s[a] on your list a, I think, to get a usable solution set. – Michael E2 Nov 25 '18 at 22:48

F[x_, y_, a_] := (a + x)^2 - y^2
Fpartx[x_, a_] = D[F[x, y, a], x]
Fparty[y_, a_] = D[F[x, y, a], y]


For toy list a,

a = Range[10] (*a={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}*)

Table[NSolve[
Fpartx[x, a[[i]]] == 0 && Fparty[y, a[[i]]] == 0, {x, y}], {i, 1,
10}]


{{{x -> -1., y -> 0.}}, {{x -> -2., y -> 0.}}, {{x -> -3., y -> 0.}}, {{x -> -4., y -> 0.}}, {{x -> -5., y -> 0.}}, {{x -> -6., y -> 0.}}, {{x -> -7., y -> 0.}}, {{x -> -8., y -> 0.}}, {{x -> -9., y -> 0.}}, {{x -> -10., y -> 0.}}}

Does this do what you wanted?

EDIT - To use the solutions, this transformation turns the output into a list of pairs.

list = Table[
NSolve[Fpartx[x, a[[i]]] == 0 && Fparty[y, a[[i]]] == 0, {x,
y}], {i, 1, 10}]
{x, y} /. Flatten /@ list


{{0.282229, 0.}, {1.71274, 0.}, {-0.235518, 0.}, {0.341353, 0.}, {-0.901989, 0.}, {0.594274, 0.}, {0.320023, 0.}, {1.35982, 0.}, {-0.546657, 0.}, {-1.29242, 0.}}

EDIT 2 - You had not used SetDelayed, or :=, to define s[a]. So, correcting the typo,

s[a_] := NSolve[Fpartx[x, a] == 0 && Fparty[y, a] == 0, {x, y}]
s /@ a


The last bit is shorthand for Map, which cursors (maps) function s over list a. I checked it and it yields the same results.

• It seems it does indeed, thanks a lot! Could you please just expand on why my approach was not working?? – Smerdjakov Nov 25 '18 at 20:56
• As you can see I used the Solve parts as you had written them. I essence, you had not "told" your function to move over a list containing your a's. This is what my Table did, but there are more elegant and straightforward ways to do the same with Map, MapAt and Apply. Feel free to have a look. – Titus Nov 25 '18 at 20:59
• I will definitely accept your answer, thanks once more, and I see what you mean about not telling the function to move over a list. I would have done that, but my problem is that when I issue a command such as s[3]` I do not get the solution of Nsolve for a value of the parameter $a$ set to 3. – Smerdjakov Nov 25 '18 at 21:07
• Please see my new edit. – Titus Nov 25 '18 at 21:15
• Heartfelt thanks – Smerdjakov Nov 25 '18 at 21:16