- #1
MartinV
- 69
- 0
Hello, everyone.
I have a code written in Mathematica that loads a single file in turn. Each file contains a 200×2 array of numbers which are x,y coordinates of points. I feed each line of each file in a loop that contains a NSolve equation system. The equation system returns 5 outputs for every input. The problem is that those solutions are not sorted.
The input points represent positions of a circle, each file a different position of that circle. The five outputs sometimes give me a whole circle (or whatever shape the input circle turns into), sometimes two or three files contain fractions of those shapes. Sometimes the outputs are even complex. If I plot everything and make an animation with all the different circle positions, I only get real solutions and all the shapes are complete. But now I want to use those output arrays into another program, one that would calculate the surface of those shapes. If I want to do this, I need to put those fractions of shapes together so that each file contains points that mark a single shape.
Here is the code I use for doing the NSolve bit:
SetDirectory["C:\Documents and Settings..."];
p = 1.0; q = 0; eps = 0.5; M = 187;
For[j = 1, j <= M, 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]]}];
]
Export["Q" <> ToString[j] <> ".txt", b];
Export["R" <> ToString[j] <> ".txt", c];
Export["S" <> ToString[j] <> ".txt", d];
Export["T" <> ToString[j] <> ".txt", e];
Export["U" <> ToString[j] <> ".txt", f];
]
This particular code exports the outputs into .txt files just like the input files are. I have a similar code, only this one plots all the solutions, making an animation. I tried using Sort[] but it didn't do anything. Up until now I tried using Matlab to import the newly made files but that is also giving me problems. Now I realize I need to have both the animations and the surface graphs on one screen so I need to make this work in Mathematica now.
Any help would be greatly appreciated.
I have a code written in Mathematica that loads a single file in turn. Each file contains a 200×2 array of numbers which are x,y coordinates of points. I feed each line of each file in a loop that contains a NSolve equation system. The equation system returns 5 outputs for every input. The problem is that those solutions are not sorted.
The input points represent positions of a circle, each file a different position of that circle. The five outputs sometimes give me a whole circle (or whatever shape the input circle turns into), sometimes two or three files contain fractions of those shapes. Sometimes the outputs are even complex. If I plot everything and make an animation with all the different circle positions, I only get real solutions and all the shapes are complete. But now I want to use those output arrays into another program, one that would calculate the surface of those shapes. If I want to do this, I need to put those fractions of shapes together so that each file contains points that mark a single shape.
Here is the code I use for doing the NSolve bit:
SetDirectory["C:\Documents and Settings..."];
p = 1.0; q = 0; eps = 0.5; M = 187;
For[j = 1, j <= M, 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]]}];
]
Export["Q" <> ToString[j] <> ".txt", b];
Export["R" <> ToString[j] <> ".txt", c];
Export["S" <> ToString[j] <> ".txt", d];
Export["T" <> ToString[j] <> ".txt", e];
Export["U" <> ToString[j] <> ".txt", f];
]
This particular code exports the outputs into .txt files just like the input files are. I have a similar code, only this one plots all the solutions, making an animation. I tried using Sort[] but it didn't do anything. Up until now I tried using Matlab to import the newly made files but that is also giving me problems. Now I realize I need to have both the animations and the surface graphs on one screen so I need to make this work in Mathematica now.
Any help would be greatly appreciated.