Where did it go?

  • Topic: simple simulation, visualization and file handling

  • Task:
    1. Implement the movement rule.

    2. Plot the taken path.

    3. Also animate the movement.

    4. Implement a function to read a trajectory from a file, then read and visualize the trajectory in brownian.txt

  • Template: motion.py

  • Data: brownian.txt

  • Further reading:

motion.py

motion.animate()[source]

Animates the trajectory.

A trajectory of the shifting coordinates are saved in the global variable trajectory as a list of coordinates. This function animates the system up to the specified frame.

motion.draw(frame)[source]

Draws one frame for animation.

A trajectory of the shifting coordinates are saved in the global variable trajectory as a list of coordinates. This function draws the system as defined in trajectory[frame].

Parameters:

frame – index of the frame to be drawn.

motion.main(x_start, y_start, angle_start, simulation_length=500)[source]

Moves a point step by step and draws the trajectory.

The point is moved using move(). The path is visualized using plot_trajectory() and animate().

Parameters:
  • x_start (float) – starting point x coordinate

  • y_start (float) – starting point y coordinate

  • angle_start (float) – angle defining the starting direction

  • simulation_length (int) – total number of steps to take

motion.move(x, y, step, angle)[source]

Calculates new coordinates by taking a step from a given starting point.

The function starts from position \((x, y)\) and moves the distance \(L\) in the direction defined by the angle \(\theta\), where \(\theta = 0\) means moving in positive x direction and \(\theta = \pi/2\) means moving in positive y direction.

The function returns the coordinates of the final position.

Note

This function is incomplete!

Parameters:
  • x (float) – initial x coordinate

  • y (float) – initial y coordinate

  • step (float) – step length \(L\)

  • angle (float) – step direction \(\theta\) in radians

Returns:

final coordinates (x, y)

Return type:

float, float

motion.plot_trajectory(trajectory)[source]

Plots a trajectory.

The trajectory is drawn as a line and the current position is drawn as a point.

Note

This function is incomplete!

Parameters:

trajectory (list) – list of coordinate pairs

motion.read_file(filename)[source]

Reads a trajectory from file.

The file must obey the format specified in write_file().

Note

This function is incomplete!

Parameters:

filename (str) – name of the file to read.

Returns:

trajectory as an array of coordinate pairs

Return type:

array

motion.write_file(trajectory, filename)[source]

Writes the trajectory to file.

Each line in the file will contain a pair x and y coordinates separated by whitespace:

x0   y0
x1   y1
x2   y2
...
Parameters:
  • trajectory (list) – list of coordinate pairs

  • filename (str) – name of file to write