# Solving for vector elements in a function to which the vectors are not passed explicitly

I want to define a function which takes in two integers (indicating the lengths of 2 vectors), and solves a simple set of expressions at a set of points to find all the values in both the vectors.

so far I have managed to successfully create a function which does exactly what I say above, except that rather than taking two integers as arguments, it takes the two vectors itself. Forgive me for the length of this next code fragment:

solution[svec_, nvec_] :=
NSolve[
Table[
approx[svec, nvec, -Cos[n*Pi/(Length[nvec] + Length[svec])]] ==
exact[-Cos[n*Pi/(Length[nvec] + Length[svec])]],
{n, 1, Length[nvec] + Length[svec]}],
Join[svec, nvec]];


The

-Cos[n*Pi/(Length[nvec] + Length[svec])


part is just a set of points. All of the above works fine.

My attempt to change it so that it takes in just integers was to do the following:

STEP ONE: create a function which takes an integer, gives a vector of length integer:

nvec[Q_] := Table[Unique["n"], {Q}];
svec[M_] := Table[Unique["s"], {M}];


STEP TWO: replace all of the svecs and nvecs in the previous code fragment with nvec[Q] and svec[M]. (could also get rid of Lengths obviously):

solution[Q_, M_] :=
NSolve[
Table[
approx[svec[M], nvec[Q], -Cos[n*Pi/(Q + M)]] == exact[-Cos[n*Pi/(Q + M)]],
{n, 1, M + Q}],
Join[svec[M], nvec[Q]]];


STEP THREE: Call the function

sol = solution[10, 20]


For some reason, this doesn't work. I never get an error; it just never finishes evaluating. Any help would be very much appreciated

-
Does it work if you replace your solution[Q_,M_] by solution2[Q_, M_] := With[{sv = svec[M], nv = nvec[Q]}, NSolve[Table[ approx[sv, nv, -Cos[n*Pi/(Q + M)]] == exact[-Cos[n*Pi/(Q + M)]], {n, 1, M + Q}], Join[sv, nv]]]? –  kguler Mar 4 '13 at 16:43
yes! Thank you very much, i'll have to look up why that works :) –  user5866 Mar 4 '13 at 16:54
btw, using this method is there a simple way to get out the vectors being solved? for example I used to use sing = 1.0*svec /. sol –  user5866 Mar 4 '13 at 16:58