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

Click For Summary

Discussion Overview

The discussion revolves around creating a loop in Mathematica to process a series of text files containing coordinate pairs, applying the NSolve function, and exporting the results to new files. Participants also explore generating plots for the solutions and creating animations from these plots.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant seeks help with a loop to import files named P1.txt, P2.txt, etc., process them with NSolve, and export results as Q1.txt, R1.txt, etc.
  • Another participant shares their initial code but struggles with file naming in the export function, later realizing that using ToString[j] resolves the issue.
  • A participant expresses a desire to create plots for each iteration of the loop and compile them into an animation, indicating uncertainty about how to implement this.
  • One participant provides a suggestion to use Table for generating the list of plots instead of defining multiple functions, highlighting a change in the loop structure.
  • Another participant mentions that their implementation does not work as expected, seeking further assistance on potential issues.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach for creating animations, as there are differing methods proposed and some participants express uncertainty about their implementations.

Contextual Notes

Some code snippets contain unresolved issues, such as the correct syntax for loops and the handling of plot generation. There are also indications of potential misunderstandings regarding the use of ListAnimate and Table.

MartinV
Messages
68
Reaction score
0
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?
 
Physics news on Phys.org
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++

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

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 52 ·
2
Replies
52
Views
13K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K