# S - General

## S-GEN-RootFinding (1 point)

### Root Finding

Write a program that takes in a function and finds the roots of a quadratic polynomial ax2 + bx + c .

Instructions:

• Your program must contain a function whose prototype is:
``def find_roots(a,b,c):``
• The function `find_roots()` must take care of all three possible cases:
• when the roots are real and distinct,
• when the roots are real and equal, and
• when the roots are complex.
• Your main code should contain:
• an infinite loop to enter three coefficients `a`, `b`, `c` (see the lecture notes for an example).
• to break the infinite loop during execution hit `Ctrl-C`
• check how many numbers entered; if it is not 3 give an error message. Otherwise, it should give the result and ask for the next set of coefficients.
• You may assume that user enters valid integers or floats and not strings and characters.
• Beware of comparisons of integers and floats in if statements:
• `if (a==0)` will not work if `a` is a float. You must find other ways of comparing. Ask your instructor.

Sample Execution:

``user@machine:~\$ python S_GEN_RootFinding.py``
``Enter coefficients: 0,1,2``
``Not a quadratic polynomial.``

``Enter coefficients: 1,2``
``You must enter three integers.``

``Enter coefficients: 1,2,3,4``
``You must enter three integers.``

``Enter coefficients: 1,-3,4``
``The roots are imaginary: 1+1.32287565553 i, -2-1.32287565553 i``

``Enter coefficients: 1,-3,2``
``The roots are real: 1.0, 2.0``

``Enter coefficients: 1,-2,1``
``There is a single real root: 1.0``

``Enter coefficients: <Ctrl-C>``
``user@machine:~\$ ``

## S-GEN-RandomWalk2D (2 points)

### 2D Random Walker

Make a two-dimensional random walker. Please read this informative explanation of randow walk.

Instructions:

• Take the total number of steps (N) from the user.
• Generate a random number (R) between 1 and 4.
• If R is 1, walker takes one step to east ( -x ).
• If R is 2, walker takes one step to south ( -y ).
• If R is 3, walker takes one step to west ( +x ).
• If R is 4, walker takes one step to north ( +y ).
• Each step size should be 1.
• At the end of your program print out the root mean square distance from the starting point.
• When your code run many times, you will observe that the distance traveled roughly grows like √N. To prove this, add an extra code to run the walker code for growing number of N's.

Note:

• You have to use `random` library but only to generate integers from 1 to 4.

## S-GEN-MakeGraphene (3 points)

### Make Graphene

Build a graphene sheet whose size is determined by the user.

Instructions:

• Graphene is a two-dimensional material that is extremely strong, has many incredible electronic properties and is at the same time very flexible. As such, it has found applications in numerous fields including physics, chemistry, medicine, and device technology.
• Graphene is built up of repeating units that each contain two atoms.
• Your code should take from the user two positive integers, N1 and N2. You should then generate a piece of graphene with N1 x N2 x 2 atoms.
• Your code should produce a text file that has the x and y coordinates of graphene.
• You can get help from your instructor on how to visualize your file.

## S-GEN-SmoothData (3 points)

### Moving Average Method for Data Smoothing

In this project, you are expected to convert a noisy signal to a clean one. In particular, you will use the moving average algorithm. Your code should read some noisy data (e.g. filename) and produce a smoothed out version. It include the following functions :

• `moving_average` : Read each point of data and replace it with the average of N neighboring data points -- N should be an input to this function.
• `chi_square` : In order to prevent the resulting data from being too smooth or too rough, you should assess its quality. Calculate the chi-square difference of your input and output curve.

The main function should read the data and compute the moving average over a set of N's and return the smoothed curve when it finds the desired chi-square difference.

Example : For a real life example, see this link.

# S-Kinematics

## General Framework

### Use the below computational framework to deal with kinematics problems

Assume that a particle with mass m moves under a force vector F. Its classical trajectory is described by Newton’s third law: where vector a is the acceleration. In the 3D space that this particle moves in, we can separate the components of this equation as follows: The above set of three second-order differential equations can be converted into an equivalent set of six first-order differential equations through the introduction of velocity where vi = (vx, vy, vz), xi = (x, y, z) and Fi = (Fx, Fy, Fz).

In order to computationally calculate vector r and vector v, we discretize the equations using the definition of the derivative from calculus. We are only going to focus on a single component, x, of the motion. The approach can be generalized to the y and z components in a similar manner: ## S-KIN-OneDimFriction (2 points)

### One dimensional trajectory with friction

Given the initial velocity and height of a one-dimensional point particle that is thrown up or down, calculate the velocity as a function of time in the presence of air friction.

Instructions:

• The equation of motion that governs one-dimensional motion with air friction (see Wikipedia): where ρ is air density, A is the cross section area of the object Cd is a geometric constant. The drag force (the second term) is always in opposite direction to the velocity.

• Your program does not need to have any functions in it but you are welcome to use them.
• It should ask the user for the initial velocity in m/sec and the height. The simulation of the motion should last until the particle hits the ground.
• Your program should be able to handle objects being thrown up (i.e. positiive initial velocity) or down (i.e. negative initial velocity).
• You can take reasonable values for the mass (e.g. 1 kg), the time step (e.g. 0.001 sec) and the friction paramaters.
• The program should produce a file, S21.dat with three columns, where the first column should be the time, the second the height and third the velocity.

Sample Execution:

Two different input entered (upper panel: upwards throw, lower panel: downward throw). Each run will produce the S21.dat data file. When the file is plotted it should look like the graph at the bottom panel. Note that for both sets of initial conditions, terminal velocity has been reached.

``user@machine:~\$ python S_KIN_OneDimFriction.py``
``Initial Velocity (m/s): 10``
``Height             (m): 30``
``user@machine:~\$ cat S21.dat``
``0.0001 30.001 9.998``
``0.0002 30.002 9.996``
``0.0003 30.003 9.995``
``0.0004 30.004 9.993``
``.      .      .``
``.      .      .``
``.      .      .``
``4.6875 0.002 -10.878``
``4.6876 0.001 -10.878``
``4.6877 0.000 -10.878 `` ``user@machine:~\$ python S_KIN_OneDimFriction.py``
``Initial Velocity (m/s): -1``
``Height             (m): 30``
``user@machine:~\$ cat S21.dat``
``0.0001 30.000 -1.001``
``0.0002 30.000 -1.002``
``0.0003 30.000 -1.003``
``0.0004 30.000 -1.004``
``.      .      .``
``.      .      .``
``.      .      .``
``3.4237 0.002 -10.861``
``3.4238 0.001 -10.861``
``3.4239 0.000 -10.861`` ## S-KIN-TwoDimFriction (3 points)

### Two dimensional trajectory with friction

Given the initial speed (m/sec) and the launch angle (in degrees) of a point particle projectile calculate the trajectory (x and z values, in meters) as a function of time in the presence of air friction.

Instructions:

• This question is a generalization of project S21 Almost all instructions are valid here as well.
• The equation in this case involves vectors: • Your code should follow the same guidelines as S21 but the angle (in degrees) and the initial velocity (in m/s) have to be inputted.
• Remember that the `math.sin` and `math.cos` functions work only with angles in radians.
• Your code should produce `S22.dat` file with two columns, x and z values in meters.
• Check the example below and note the asymmetric trajectory due to the air drag.

NOTE that the numbers and the figure below will not be the same as your own results.

``user@machine:~\$ python S_Kin_TwoDimFric.py``
``Initial Velocity (m/s): 30``
``Angle            (deg): 45``
``user@machine:~\$ cat Fric.dat``
``0.0001 0.002 0.102``
``0.0002 0.004 0.104``
``0.0003 0.006 0.106``
``0.0004 0.008 0.108``
``0.0005 0.011 0.111``
``0.0006 0.013 0.113``
``.      .      .``
``.      .      .``
``.      .      .``
``2.4468 17.847 0.003``
``2.4469 17.847 0.002``
``2.4470 17.848 0.001``
``2.4471 17.848 0.000`` ## S-KIN-MotionField (3 points)

### Motion in an electric and magnetic field

Calculate the trajectory of a charged particle that is in an electric and magnetic field.

Instructions:

• If a particle moves under the influence of a joint electric and magnetic field, it will experience the Lorentz force defined as • Let the point charge actually be an electron.
• Your code should contain a function `cross(v1,v2)` that calculates the cross product of two vectors.
• The E and B fields have to be inputted. The vectors should be entered as two vectors with three components.
• Make sure that you use appropriate units.
• The main body of your code should then calculate the trajectory using the kinematics logic explained above.
• Your simulation of the trajectory should run for a large enough number of steps to see enough of the trajectory; therefore, choose your time step accordingly.
• Your code should produce an output file, S23.dat, with three columns x, y and z (the coordinates of the particle at each time step).
• You may take initial position vector and initial velocity vector components as r0= (0, 0, 0) and v0 = (0, 0, 0).

## S-KIN-PlanetMotion (3 points)

### Planetary motion

Calculate the trajectory of planet X around the Sun in the presence of planet Y.

Instructions:

• Planetary motion is governed by the universal law of gravity which dictates the force between two objects: where G is the gravitational constant, m1 and m2 are the masses of the two objects, r12 is the distance between them and ř12 is a unit vector pointing in the direction along the two objects.

• Assume that all are in two-dimensions.
• Assume that the Sun is stationary at the origin.
• Assume that planet Y is also stationary at some point in space, chosen by the user. Note that we create a non-physical model for the sake of project.
• The followings have to be inputted (use appropriate units - e.g don't use meters):
• The location, mass and initial position of planet Y.
• The location, mass of planet X.
• Your code should run for a large enough number of steps for a meaningful trajectory to be produced.
• You may define any number of functions you want, or use no functions at all if you don’t wish to.
• Your code should produce S24.dat file with two columns x and y coordinates of planet X.

## S-KIN-Rocket (3 points)

### Fuel consuming rocket

Simulate the trajectory of a fuel-cosuming rocket.

Instructions:

• Take a rocket leaving the surface of Earth with an variable accelaration:
• Take into account of decrease in mass due to fuel consumption.
• Take Earth’s gravity variable too.
• Rocket's distance from the center of the Earth increases as it speeds up.
• Continuously (i.e real clock time) calculate the rocket’s velocity and stop when it reaches the escape speed of the Earth.

# S - Electricity & Magnetism

## S-EMT-Apollo11 (3 points)

### Apollo 11

Calculate the magnetic field on the track of Apollo 11.

Instructions:

• Take Earth’s core as a specific type of magnet.
• Take Apollo 11 as a charged particle.
• Apollo 11 moves within the Earth’s magnetic field.
• As it drifts out from Earth, its distance from Earth increases (therefore magnetic field decreases).

Note:

• In the Van Allen belts certain types of charges are trapped which effects any object (mass and/or particle) passing through them.

## S-EMT-ChargedParticles (3 points)

### Charged point particles

Calculate the electric field of a collection of point charges at a given point in 3D space.

Instructions:

• Your code should ask the user for the coordinate, `P`, in x, y, z (see figure).
• Your code should read a file `S32.dat` that contains four columns (in the following order) for each point particle:
• the charge of the particle
• coordinates (x, y, z)
• The number of charges in the file is arbitrary.
• The charges can be positive and negative.
• Your code should contain a function `efield(q,r1,r2)` that calculates the eletric field created at point `r2` by a point particle of charge `q`, located at `r1`.
• Call this function (in the main body) as many times as the number of charges to calculate the total electric field (e.g `Etot`).
• Your code should print the three components of `Etot` vector at point P.

Note:

• `Etot` is a vector having three components (i.e numbers). 