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

• Mathematica
• MartinV
In summary, the conversation discusses the use of Mathematica to import, process, and export files containing pairs of coordinates. The main loop is used to perform NSolve and create output files with designated names. The conversation also mentions the desire to create plots and animations using Table and ListAnimate. There are difficulties with the code not working on the computer and seeking help to troubleshoot the issue.
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?

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];
]

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.

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?

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++

Mathematica theNameOfTheFunctionYouAreHavingTroubleWith
and seeing if they have an example similar to what you want often seems helpful

## 1. How can I create a loop in Mathematica to number my output files?

To create a loop in Mathematica, you can use the For or Do loop functions. These functions allow you to specify a starting and ending value, as well as the increment, for the loop. You can then use the loop index to name your output files with a number.

## 2. Can I use a counter variable in my loop to number the output files?

Yes, you can use a counter variable in your loop to number the output files. You can initialize the counter variable to 1 and then increment it by 1 in each iteration of the loop. This will give you a sequential numbering of your output files.

## 3. Is it possible to have a different naming convention for my output files in the loop?

Yes, you can use a string variable in your loop to name your output files with a different naming convention. You can concatenate the string variable with the counter variable to create a unique name for each output file.

## 4. How can I save my output files in a specific folder using the loop?

To save your output files in a specific folder, you can use the NotebookDirectory[] function to get the directory of your current notebook. Then, you can use the Export function with the correct file path to save your output files in the desired folder.

## 5. Can I use a conditional statement in my loop to selectively name my output files?

Yes, you can use a conditional statement in your loop to selectively name your output files. You can use the If or Switch functions to specify a condition and then name your output files accordingly. This allows for more flexibility in naming your output files based on certain criteria.

• MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
6K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
52
Views
11K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
9K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
• MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K