- #1
MartinV
- 69
- 0
Hello, friends. I just registered because I desperately need some help.
For my bachelor's degree in physics, I'm trying to simulate gravitational microlensing of a background source on a system of objects, acting as gravitational lenses. I have the equation up and running, the problem is I have barely started using Mathematica and I find myself struggling to make the work smoother. I have seen what Mathematica can do with the Manipulate command and it blew my mind. If I can make that work, things will be easy. I'm doing this because I'm running out of time and it's hard keeping up by learning new programming languages all the time.
This is what I put in Mathematica:
p = 0; q = 0.4; eps = 0.5; (*p and q are coordinates where the second lens is located; the first one is in point (0,0). Eps is the mass ratio between the primary and secondary lens.*)
b1 = {}; c1 = {}; d1 = {}; e1 = {}; f1 = {}; (*declaring output objects*)
P = Import["P1.txt", "Table"]; (*Importing a 200×2 array of the source image (background star). I created this .txt file in another program; it's simply a series of positions [x,y]. I was thinking of replacing this with a manipulator tool in Mathematica so the image could respond quickly.*)
(*Below is the main loop which reads each line of the imported file and inputs in the equation system, solved by the NSolve method. There are five outputs, they can be real as well as complex. NSolve does not give the outputs any order so I tried to solve this by putting the aa = sort[a] command. It works most of the time. The outputs b1,c1,d1,e1,f1 are adapted each time by adding the new result to the storage of old ones. In the end this gives me five 200×2 output arrays, showing five images of the inputted source.*)
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];
b1 = Append[b1, {aa[[1, 1, 2]], aa[[1, 2, 2]]}];
c1 = Append[c1, {aa[[2, 1, 2]], aa[[2, 2, 2]]}];
d1 = Append[d1, {aa[[3, 1, 2]], aa[[3, 2, 2]]}];
e1 = Append[e1, {aa[[4, 1, 2]], aa[[4, 2, 2]]}];
f1 = Append[f1, {aa[[5, 1, 2]], aa[[5, 2, 2]]}]]
Export["Q1.txt", b]; Export["R1.txt", c]; Export["S1.txt", d];
Export["T1.txt", e]; Export["U1.txt", f];
ListLinePlot[{b1, c1, d1, e1, f1}, PlotRange -> {{-1, 1}, {1.3, -0.8}}
(*I export the output arrays into .txt files where I can edit them or I plot them in Mathematica.*)
The code above works. What I want is to augment it.
The first thing I want to do is to is stop doing this for 31 input files (P2.txt, P3.txt, etc.) manually and create a loop around this whole code. The problem is I want to make the numbering of the output files automatic so the code creates Q1.txt, Q2.txt etc. by itself. The index of the outer loop must be in the name of the output files.
Secon, I want to make the results presentable. I've seen what Mathematica can do with Manipulate and Animate commands but it would take me too long to learn it by myself. I need this done in a month and if I keep doing this manually I will not succeed.
Thank you for any help you can offer.
Homework Statement
For my bachelor's degree in physics, I'm trying to simulate gravitational microlensing of a background source on a system of objects, acting as gravitational lenses. I have the equation up and running, the problem is I have barely started using Mathematica and I find myself struggling to make the work smoother. I have seen what Mathematica can do with the Manipulate command and it blew my mind. If I can make that work, things will be easy. I'm doing this because I'm running out of time and it's hard keeping up by learning new programming languages all the time.
Homework Equations
This is what I put in Mathematica:
p = 0; q = 0.4; eps = 0.5; (*p and q are coordinates where the second lens is located; the first one is in point (0,0). Eps is the mass ratio between the primary and secondary lens.*)
b1 = {}; c1 = {}; d1 = {}; e1 = {}; f1 = {}; (*declaring output objects*)
P = Import["P1.txt", "Table"]; (*Importing a 200×2 array of the source image (background star). I created this .txt file in another program; it's simply a series of positions [x,y]. I was thinking of replacing this with a manipulator tool in Mathematica so the image could respond quickly.*)
(*Below is the main loop which reads each line of the imported file and inputs in the equation system, solved by the NSolve method. There are five outputs, they can be real as well as complex. NSolve does not give the outputs any order so I tried to solve this by putting the aa = sort[a] command. It works most of the time. The outputs b1,c1,d1,e1,f1 are adapted each time by adding the new result to the storage of old ones. In the end this gives me five 200×2 output arrays, showing five images of the inputted source.*)
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];
b1 = Append[b1, {aa[[1, 1, 2]], aa[[1, 2, 2]]}];
c1 = Append[c1, {aa[[2, 1, 2]], aa[[2, 2, 2]]}];
d1 = Append[d1, {aa[[3, 1, 2]], aa[[3, 2, 2]]}];
e1 = Append[e1, {aa[[4, 1, 2]], aa[[4, 2, 2]]}];
f1 = Append[f1, {aa[[5, 1, 2]], aa[[5, 2, 2]]}]]
Export["Q1.txt", b]; Export["R1.txt", c]; Export["S1.txt", d];
Export["T1.txt", e]; Export["U1.txt", f];
ListLinePlot[{b1, c1, d1, e1, f1}, PlotRange -> {{-1, 1}, {1.3, -0.8}}
(*I export the output arrays into .txt files where I can edit them or I plot them in Mathematica.*)
The Attempt at a Solution
The code above works. What I want is to augment it.
The first thing I want to do is to is stop doing this for 31 input files (P2.txt, P3.txt, etc.) manually and create a loop around this whole code. The problem is I want to make the numbering of the output files automatic so the code creates Q1.txt, Q2.txt etc. by itself. The index of the outer loop must be in the name of the output files.
Secon, I want to make the results presentable. I've seen what Mathematica can do with Manipulate and Animate commands but it would take me too long to learn it by myself. I need this done in a month and if I keep doing this manually I will not succeed.
Thank you for any help you can offer.