Solving equation system, need to augment my code

In summary, for your project on simulating gravitational microlensing, I recommend automating the process of reading and exporting files using Mathematica's built-in functions and taking the time to learn the Manipulate and Animate commands for visualization.
  • #1
MartinV
69
0
Hello, friends. I just registered because I desperately need some help.

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.
 
Physics news on Phys.org
  • #2


Hello there,

As a fellow physicist, I understand the struggle of trying to learn new programming languages while also keeping up with coursework. It can be overwhelming at times.

Firstly, I would recommend looking into Mathematica's built-in functions for reading and writing files, such as "FileNames", "ReadList", and "Write". These can help you automate the process of reading in your input files and exporting your output files. You can also use the "StringJoin" function to create the file names with the appropriate index.

Secondly, for visualization, I highly recommend taking the time to learn the Manipulate and Animate commands. They are very powerful tools for interactive visualization and can save you a lot of time in the long run. There are also many resources available online, such as tutorials and forums, where you can get help and tips on using these commands.

I hope this helps and best of luck with your project! Don't hesitate to reach out if you have any further questions.
 

1. What is an equation system?

An equation system is a set of equations that are related to each other and must be solved together to find the values of the unknown variables.

2. How do I solve an equation system?

To solve an equation system, you can use a variety of methods such as substitution, elimination, or graphing. It is important to choose a method that best suits the type of equations in the system.

3. How do I augment my code to solve an equation system?

To augment your code for solving an equation system, you can use techniques such as creating new functions, implementing loops, or using arrays to store and manipulate the equations. It is also important to make sure that your code is efficient and accurate.

4. What are the benefits of using code to solve an equation system?

Using code to solve an equation system can save time and reduce the chances of human error. It also allows for more complex and larger systems to be solved, compared to solving them by hand.

5. Can I use any programming language to solve an equation system?

Yes, you can use any programming language to solve an equation system as long as it has the necessary functions and tools to handle mathematical calculations and variable manipulation.

Similar threads

  • Calculus and Beyond Homework Help
Replies
4
Views
684
  • Calculus and Beyond Homework Help
Replies
24
Views
2K
  • Calculus and Beyond Homework Help
Replies
5
Views
619
  • Calculus and Beyond Homework Help
Replies
3
Views
817
  • Calculus and Beyond Homework Help
Replies
4
Views
938
  • Calculus and Beyond Homework Help
Replies
8
Views
758
  • Calculus and Beyond Homework Help
Replies
12
Views
982
  • Calculus and Beyond Homework Help
Replies
3
Views
814
  • Calculus and Beyond Homework Help
Replies
6
Views
287
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top