Questions:

Objectives:

Keypoints:


A Python script is a .py file that contains Python code only

Use a plain text editor to write a Python script

Use a shebang at the top of your file to indicate that it is Python code


#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt

print("Initialising the simulation...")

# define function that describe the Lorenz system.
def Lorenz(sigma,r,b,xyz):
    
    x = xyz[0]
    y = xyz[1]
    z = xyz[2]
    
    fx = sigma*(y-x)
    fy = (r*x)-y-(x*z)
    fz = (x*y)-(b*z)
    
    return np.array([fx,fy,fz],float)

# Simulation parameters
start = 0                  # start time
end = 50                 # end time
num_steps = 3000         # number of time steps
h = (end-start) / num_steps  # time step size

# intitial conditions: x=0, y=1, z=0
xyz = np.array([0,1,0],float)

# constants
sigma = 10
r = 28
b = 8/3

# generate times at which to evaluate xyz
time_list = np.arange(start,end,h)

# create empty arrays to hold the calculated values
x_points = []
y_points = []
z_points = []

print("Applying Euler's method...")

# Apply Euler's method
for time in time_list:
    
    x_points.append(xyz[0])
    y_points.append(xyz[1])
    z_points.append(xyz[2])
    xyz += h*Lorenz(sigma,r,b,xyz)
    
print("Plotting the results...")

# Plot the strange attractor
plt.plot(x_points,z_points)
plt.savefig("Strange_attractor.png")

Terminals are a way to interact with a computer without using a Graphical User Interface (GUI)

You can navigate around your computer using the Unix shell

There is much, much more you can do with the Unix shell! We won’t discuss them here but for more details see this workshop from Software Carpentry.

To run your python code type the filepath into the terminal

cd /Users/lucy/Code/
Lorenz.py

Use sys.argv to read in command line arguments


#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt
import sys

print("Initialising the simulation...")

# define function that describe the Lorenz system.
def Lorenz(sigma,r,b,xyz):
    
    x = xyz[0]
    y = xyz[1]
    z = xyz[2]
    
    fx = sigma*(y-x)
    fy = (r*x)-y-(x*z)
    fz = (x*y)-(b*z)
    
    return np.array([fx,fy,fz],float)

# Simulation parameters
start = 0                                     # start time
end_time = float(sys.argv[1])                 # end time
num_steps = 3000                              # number of time steps
h = (end-start) / num_steps                   # time step size

# intitial conditions: x=0, y=1, z=0
xyz = np.array([0,1,0],float)

# constants
sigma = 10
r = 28
b = 8/3

# generate times at which to evaluate xyz
time_list = np.arange(start,end,h)

# create empty arrays to hold the calculated values
x_points = []
y_points = []
z_points = []

print("Applying Euler's method...")

# Apply Euler's method
for time in time_list:
    
    x_points.append(xyz[0])
    y_points.append(xyz[1])
    z_points.append(xyz[2])
    xyz += h*Lorenz(sigma,r,b,xyz)
    
print("Plotting the results...")

# Plot the strange attractor
plt.plot(x_points,z_points)
plt.savefig("Strange_attractor.png")

TASKS

  1. Adapt the programme Lorenz.py so that it accepts 3 command line arguments: one for the start_time, one for end_time and one for num_steps.
  2. Each time the Lorenz.py script runs the previous .png file is overwritten. Adapt the script so that the plot filename contains the values of the start_time, end_time and num_steps variables. You may need to search the internet for hints on how to do this (hint: look-up string formatting)