Question

Exercise 8.3 The Lorenz equations One of the most celebrated sets of differential equations in physics is the Lorenz equation

Please answer both (a) and (b) and code using Python3.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for Lorentz force:

## Hello, Python!
## Catherine Moresco
## PHYS 22500
## Winter 2015
## Modeling of a charged particle trajectory
from mpl_toolkits.mplot3d import axes3d
import numpy as np
import matplotlib.pyplot as plt
# THIS IS THE PART WHERE YOU MODIFY THE PARAMETERS # ====================================================================================
# =======================================================================================================================================
# It would be cool to be able to parse arguments from the command line, but it's 5:00 AM right now, and while I am having a
# fun time right now that isn't exactly necessary functionality. We'll just hard-code the arguments here, instead.
# Here's how it works: you code the number of particles that you want to plot, and then you create arrays with the properites of those
# particles, *in order*. That is, the first velocity in the velocity list should correspond with the first mass in the mass list.
# If the lengths of all these lists are not equal to numTrajectories, you are doing something wrong.
numTrajectories = 3 # Number of trajectories that you want to plot
masses = [.05, .075, .075] # Two numbers, representing trajectories
positions = [[0., 0., 0.], [0., 0., 0], [1., 1., 0.]] # An array of 3-vectors representing startig position
velocities = [[1., 0., 0.], [1., 1., .1], [0., 0., .1]] # An array of 3-vectors representing velocities
charges = [1, 1, 1]
formatStrings = ["k", "r", "b"] # Standard pyplot formatting strings for each trajectory plot
# A note about time:
# The smaller your steps are, the more precise your result will be.
# This is important when using Euler's method; if too-large steps are used, Euler is incapable of properly representing a
# force that should result in uniform circular motion, and will instead show an outward spiral.
# This can be combated by using an alternate numerical integration method, such as Runge Kutta; however,
# since here we are calculating relatively few and short paths, it is sufficient to use Euler with appropriately small steps.
# Just make your timestep small relative to your speed, and you'll be fine.
timestep = .0001
numsteps = 100000 # This will process in a reasonable amount of time.
# This is where you define your B field. Pick a function, any function!
def b(x, y, z):
''' What the B field evaluates to at a given x, y, z, location.
Returns B as a vector. '''
# For now, let's make it a constant B field in the z direction.
b = np.array([0, 0, .1])
# Here's one that varies a little bit:
# b = np.array([2, 0, .1*z])
return b
# =======================================================================================================================================
# =======================================================================================================================================
class Particle:
def __init__(self, positionVector, velocityVector, mass, charge, format):
self.position = np.array(positionVector)
self.velocity = np.array(velocityVector)
self.mass = mass
self.charge = charge
self.trajectory = []
self.formatString = format
# Keep track of minimum and maximum positions, for choosing dimensions of colume to render later
# Arbitrary constant added to each dimension to provide a minimum axis length--in the case where the
# motion lies entirely in one plane, this ensures that the neglected axis is not assigned a length
# of zero when graphing. That is problematic when it occurs.
self.minPos = np.copy(self.position) - [.1, .1, .1]
self.maxPos = np.copy(self.position) + [.1, .1, .1]
def step(self, fieldFunction, timeStep):
''' Given a force, find the resulting acceleration, and update the position and
velocity of the particle over a given time interval using Euler's method. '''
field = fieldFunction(self.position[0], self.position[1], self.position[2])
# force = q * (v x b)
force = self.charge * np.cross(self.velocity, field)
# force = mass * acceleration => acceleration = mass/force
acceleration = force/self.mass
self.velocity += acceleration * timeStep
self.position += self.velocity * timeStep
self.trajectory.append(np.copy(self.position))
self.updateMinPos(self.position)
self.updateMaxPos(self.position)
def updateMinPos(self, position):
''' Update the minimum position vector of the particle element-by-element with the given position. Generalized to vectors of arbitrary length. '''
for i in range (0, len(position)):
self.minPos[i] = min(position[i], self.minPos[i])
def updateMaxPos(self, position):
''' Update the maximum position vector of the particle element-by-element with the given position. Generalized to vectors of arbitrary length. '''
for i in range (0, len(position)):
self.maxPos[i] = max(position[i], self.maxPos[i])
def calcLorentz(q, v, b):
''' Calculate the Lorentz force for a given charge (scalar), v (vector), and B field (vector),
returning the Lorentz force as a vector. '''
return q * np.cross(v, b)
# Create figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Create list of particles
particles = []
for i in range (0, numTrajectories):
particles.append(Particle(positions[i], velocities[i], masses[i], charges[i], formatStrings[i]))
# Plot particle trajectories
for particle in particles:
print "Processing next particle..."
for time in range(0, numsteps):
particle.step(b, timestep)
trajectory = np.array([[p[0] for p in particle.trajectory], [p[1] for p in particle.trajectory], [p[2] for p in particle.trajectory]])
ax.plot(trajectory[0], trajectory[1], trajectory[2], particle.formatString)
plt.show()
Add a comment
Know the answer?
Add Answer to:
Please answer both (a) and (b) and code using Python3. Exercise 8.3 The Lorenz equations One...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please complete using R. Show all code needed to complete exercise. Will Thumbs up if done...

    Please complete using R. Show all code needed to complete exercise. Will Thumbs up if done neatly and correctly. Exercise 5 (More Simulation) Let X follow an exponential distribution with rate parameter λx = 2. Let Y follow a Poisson distribution with rate parameter λ 3. We write sd(X) for the true standard deviation of X and m(Y) for the true median of Y Let s be the sample standard median which is an estimate of m(Y) Suppose we take...

  • Please answer with screenshot of code in Python sistor and one capacitor. Exercise 8.1: A low-pass...

    Please answer with screenshot of code in Python sistor and one capacitor. Exercise 8.1: A low-pass filter Here is a simple electronic circuit with on - Vour Vin - W- This circuit acts as a low-pass filter: you send a signal in on the left and it comes out filtered on the right Using Ohm's law and the capacitor law and assuming that the output load has very high impedance, so that a negligible amount of current flows through it,...

  • PART C PLEASE!!! IF YOU KNOW HOW TO CODE MATLAB PLEASE HELP THANK YOU Write a Matlab/Python scrip...

    PART C PLEASE!!! IF YOU KNOW HOW TO CODE MATLAB PLEASE HELP THANK YOU Write a Matlab/Python script which computes the trajectory of P according to the system of equations (1) and the corresponding (non-dimensional) range for given values of (α,φ,0), varying values of φ 0 Plot the trajectories for 6 values of φ 0 of your choice in the interval 0< φ0 π/2. Compute the corresponding range reached by each trajectory. Note: Special attention will be given to the...

  • For this exercise we will run a regression using Swiss demographic data from around 1888. The...

    For this exercise we will run a regression using Swiss demographic data from around 1888. The sample is a cross-section of French speaking counties in Switzerland This data come with the R package datasets. The first step is to load the package into your current environment by typing the command libraryldatasets) in to the R console. This loads a number of datasets including one called swiss. Type help/swiss) in the console for additional details. The basic variable definitions are as...

  • A group of physics students collected data from a test of the projectile motion problem that...

    A group of physics students collected data from a test of the projectile motion problem that was analyzed in a previous lab exercise (L5). In their test, the students varied the angle and initial velocity Vo at which the projectile was launched, and then measured the resulting time of flight (tright). Note that tright was the dependent variable, while and Vo were independent variables. The results are listed below. (degrees) Time of Flight (s) Initial Velocity V. (m/s) 15 20...

  • winkngs spring i(t) v(t) st VEE Figure 1: (a)Solenoid with retu spring. (b) Equivalent lumped electrical cireuit (...

    winkngs spring i(t) v(t) st VEE Figure 1: (a)Solenoid with retu spring. (b) Equivalent lumped electrical cireuit (e) Equivalent mechanical diagram Figure 1(a) illustrates a solenoid with a return spring The voltage e(t) across the winding, causes a current it) to flow through the winding. which in turn generates a magnetic field The magnetic field induces a force f(t) on the plunger mass, . The magnitude of this force is related to the current in the windings via the solenoid's...

  • Please help me correct my MATLAB script code for this problem, thank you!! A projectile PA is launched from point A towards the east with an initial launch velocity voa and an initial...

    Please help me correct my MATLAB script code for this problem, thank you!! A projectile PA is launched from point A towards the east with an initial launch velocity voa and an initial lauw angle of 0x. The impact point of the projectile Pa is a point B in a valley with an ordinate, yon, located below the clevation of point A. The launch from point A is instantaneously detected at point B, and a counter projectile P launched simultaneously...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT