Converting Standard For Loop to Mathematica Code for 2D Iteration?

  • Context: Mathematica 
  • Thread starter Thread starter rhythmiccycle
  • Start date Start date
  • Tags Tags
    2d Mathematica
Click For Summary
SUMMARY

The discussion focuses on converting a standard Java for loop into equivalent Mathematica code for 2D iteration. The original Java code initializes coordinates (x0, y0) and iterates 100 times, updating the coordinates based on the angle calculated using the atan2 function. The provided Mathematica code successfully replicates this logic using the For loop and ArcTan function, ensuring the correct order of arguments as specified in the Wolfram documentation. Key functions utilized include ArcTan, Cos, and Sin for angle and coordinate calculations.

PREREQUISITES
  • Understanding of basic programming concepts, specifically loops.
  • Familiarity with Mathematica syntax and functions.
  • Knowledge of trigonometric functions: ArcTan, Cos, and Sin.
  • Basic understanding of coordinate systems and 2D iteration.
NEXT STEPS
  • Explore Mathematica's For loop and its alternatives like Table and Do.
  • Learn about the differences between ArcTan[x, y] and ArcTan[y, x] in Mathematica.
  • Investigate advanced 2D graphics capabilities in Mathematica for visualizing iterations.
  • Study optimization techniques for iterative algorithms in Mathematica.
USEFUL FOR

Mathematica users, programmers transitioning from Java to Mathematica, and anyone interested in 2D iterative algorithms and trigonometric calculations.

rhythmiccycle
Messages
9
Reaction score
0
here is the pseudo java (processing) code I'm trying to run


Code:
float x0,y0,x,y,t;

x0 = 2; //arbitrary value
y0 = 5; //arbitrary value

x = x0;
y = y0;

for(int t = 1; t <= 100; t++){   //one hundred iterations
    t =atan2(y,x);
    x += cos(t);
    y += sin(t);

    print( x, y)

}

I'm trying to run something more complex than this, but the code above shows the main point where I can't convert standard "for loop" programming to mathemitica.


please help me write an equivalent code in mathematica.
 
Physics news on Phys.org
Perhaps better posted in the Math and Science Software section.

Note/Warning: read
http://reference.wolfram.com/mathematica/ref/ArcTan.html
for information on the order of arguments in ArcTan to tell
whether you need ArcTan[x,y] or ArcTan[y,x].

x0 = 2;
y0 = 5;
x = x0;
y = y0;
For[t = 1, t <= 100, t++,
t = ArcTan[y,x];
x += Cos[t];
y += Sin[t];
Print[ x," ",y];
];
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K