# Mathematica - help with a loop that would number the output files

• Mathematica
MartinV
Hello. I have a question about Mathematica.

I have a series of files named P1.txt, P2.txt, etc. Each file contains 200 pairs of coordinates [x,y]. I want to write a loop that would import each of these files in turn, process them with NSolve, and then export the output files as Q1.txt, R1.txt, Q2.txt, etc. NSolve returns three solutions for each inputed file.

I can't seem to make Mathematica to create files with names according to the number of the loop. Can anyone help?

MartinV
This is what I have so far.

For[j = 1, j < 32, j++, b = {}; c = {}; d = {}; e = {}; f = {};
P = Import["P" <> ToString[j] <> ".txt", "Table"];
For[i = 1, i <= 200, i++,
a = NSolve[{x - (1 - eps)*x/(x^2 + y^2) -
eps*(x - p)/((x - p)^2 + (y - q)^2) == P[[i, 1]],
y - (1 - eps)*y/(x^2 + y^2) -
eps*(y - q)/((x - p)^2 + (y - q)^2) == P[[i, 2]]}, {x, y}];
aa = Sort[a];
b = Append[b, {aa[[1, 1, 2]], aa[[1, 2, 2]]}];
c = Append[c, {aa[[2, 1, 2]], aa[[2, 2, 2]]}];
d = Append[d, {aa[[3, 1, 2]], aa[[3, 2, 2]]}];
e = Append[e, {aa[[4, 1, 2]], aa[[4, 2, 2]]}];
f = Append[f, {aa[[5, 1, 2]], aa[[5, 2, 2]]}];
]
Export["Q[j].txt", b]; Export["R[j].txt", c]; Export["S[j].txt", d];
Export["T[j].txt", e]; Export["U[j].txt", f];
]

MartinV
OK, ironically I have just figured it out. Just use ToString[j].

I also want to create plots of the solutions. I want to make a graphics for each step of the main loop and then put them together into animation. I know I should use Table and Graphics but I have no idea how.

MartinV
This is what I have.

For[j = 1, j < 3, j++,
P = Import["P" <> ToString[j] <> ".txt", "Table"]; b = {}; c = {};
d = {}; e = {}; f = {};
For[i = 1, i <= 200, i++,
a = NSolve[{x - (1 - eps)*x/(x^2 + y^2) -
eps*(x - p)/((x - p)^2 + (y - q)^2) == P[[i, 1]],
y - (1 - eps)*y/(x^2 + y^2) -
eps*(y - q)/((x - p)^2 + (y - q)^2) == P[[i, 2]]}, {x, y}];
aa = Sort[a];
b = Append[b, {aa[[1, 1, 2]], aa[[1, 2, 2]]}];
c = Append[c, {aa[[2, 1, 2]], aa[[2, 2, 2]]}];
d = Append[d, {aa[[3, 1, 2]], aa[[3, 2, 2]]}];
e = Append[e, {aa[[4, 1, 2]], aa[[4, 2, 2]]}];
f = Append[f, {aa[[5, 1, 2]], aa[[5, 2, 2]]}];
];
myplot[j] =
ListLinePlot[{b, c, d, e, f}, PlotRange -> {{-1, 1}, {1.3, -0.8}}];
];
ListAnimate[myplot[j], {j, 3}];

I've heard from someone who says it should work. It does not work on my computer.
Does anyone know what I'm doing wrong?

Bill Simpson
Based on the documentation here:
http://reference.wolfram.com/mathematica/ref/ListAnimate.html
I would suggest trying:

ListAnimate[
Table[
P = Import["P" <> ToString[j] <> ".txt", "Table"];
b = {}; c = {}; d = {}; e = {}; f = {};
For[i = 1, i ≤ 200, i++, a = NSolve[{x - (1 -
eps)*x/(x^2 + y^2) - eps*(x - p)/((x - p)^2 + (y - q)^2) == P[[i, 1]],
y - (1 - eps)*y/(x^2 + y^2) - eps*(y - q)/((x - p)^2 + (
y - q)^2) == P[[i, 2]]}, {x, y}];
aa = Sort[a];
b = Append[b, {aa[[1, 1, 2]], aa[[1, 2, 2]]}];
c = Append[c, {aa[[2, 1, 2]], aa[[2, 2, 2]]}];
d = Append[d, {aa[[3, 1, 2]], aa[[3, 2, 2]]}];
e = Append[e, {aa[[4, 1, 2]], aa[[4, 2, 2]]}];
f = Append[f, {aa[[5, 1, 2]], aa[[5, 2, 2]]}];
];
ListLinePlot[{b, c, d, e, f}, PlotRange -> {{-1, 1}, {1.3, -0.8}}],
{j, 1, 2}
]
]

ALL that changed from what you had was to use Table to create a list of ListLinePlot s instead of creating several function definitions to hold the frames of your animation, and for that Table I used {j,1,2} instead of j=1,j<3,j++