Lab 03 - Curlss/Computational_physics GitHub Wiki

For Lab 03, the objective of this lab is to produce models of systems that are changing over time using the Euler method. If we can show how the system changes over time, we can figure out how it changes when we step forward in time by very small amounts of time.

We did a little research to understand the Euler method for integrating an ordinary differential equation and answered these questions:

  1. Define what an ordinary differential equation is? -There are two types of differential equations: ordinary and just differential. An ordinary differential equation only has ordinary derivatives of one or more unknown functions with respect to a single independent variable. A differential equation is just an equation that has the derivatives of one or more unknown functions (or dependent variables) related to one or more independent variables.

  2. Describe what a Taylor Expansion is and why it is useful? -Based on a function's derivatives at a certain point, a Taylor Expansion is a way to approximate it as an infinite sum of polynomial terms. It lets you write complicated functions in a simpler polynomial form, which makes them easier to calculate and understand. This method is very helpful for math calculations, physics, and engineering because it makes it easy to evaluate functions like e^x, sin, and cos. Taylor expansions also help you study how a function behaves locally near a certain place and give you an idea of how accurate an estimate is. One special case is the Maclaurin Series, which is a Taylor expansion with a=0 and 𝑓 = 0.

  3. Describe the general form of the Euler Method and why this is useful for approximating a solution to a differential equation? -Euler's Method is a simple way to use numbers to get close to the answers to ordinary differential equations of the form $\frac{dy}{dx} = f(x, y) $ where y(0) = y(0) at the start. Small steps of size h are taken along the curve, and the formula $y_{n+1} = y_n + h f(x_n, y_n)$ is used to guess the next number. Here, f(x_n, y_n) is the slope at the current point. This method is widely used because it is simple to use and gives a good approximation of the answer when accurate methods are not possible. It may not be very accurate, but it is useful in physics, engineering, and computational modeling, and it can be used as a base for more advanced numerical methods.

We used the Euler method to get a rough idea of how the amount of Uranium atoms decreases over time in the first part of this lab. To do this, we coded in functions that would return time and nucleus arrays. To have a list of different values for one variable.

def initialize(N0, tau, tmax, dt):
  '''
this function sets up the time and number arrays
  '''
  nsteps = int(tmax/dt)
  t = np.zeros(nsteps)
  n_nuclei = np.zeros(nsteps)
  n_nuclei[0] = N0
  return t, n_nuclei

This way, when we write code for our Euler equation, it will be able to use the values in our array. This is how our calculate, Euler method was created. It starts with a value in our array and keeps going through the other values in our array in a loop, moving from one point to the next until it reaches our maximum time. We use dt to keep moving the system forward, and as we can see from the graphs for our different dts, changing dt will change our graphs. Our dt is very important because it tells us what kind of steps we should take from one point to the next. Depending on our dt, we can take either smaller or larger steps.

def calculate(t, n_nuclei, dt, tau):
  '''
This function calculates the number of nuclei at each time step
  '''
  for i in range(1, len(t)):
    n_nuclei[i] = n_nuclei[i-1] - n_nuclei[i-1]/tau * dt
    t[i] = t[i-1] + dt
  return t, n_nuclei

Continuing with this first part, you need to set initial values for your variables, and then call your initialize function, and then your calculate function. Once you have set that up, you can create graphs for your nuclei vs time. We also created an analytic solution, an exact formula that solves an equation without needing approximation, our calculated values lined up perfectly with the analytic solution.

In the second part of the lab, we looked to see how the time steps affect the accuracy of the approximation, so we chose 3 different time steps to see the accuracy between them, (0.05,0.2,0.5), We can see from this second part that if dt is bigger, steps will be farther apart. This means that we will move from one point to the next in bigger chunks, which could cause data that isn't accurate and isn't in line with the real analysis answer. Our data was closest to the analytical answer when dt was small. This is because when dt is small, we move from point A to point B in smaller steps, which means there is less room for mistake and variation. For example, this shows how important dt is. This fact would not have been so clear without the analytical answer we found in the code. This analytic solution tells us how important different variables are to our approximation. This is important because these complex equations don't always have an analytic solution, so we need to know how each variable affects the equation and in what way in order to get our approximations as close to right as possible. That's not the only time this Euler method was used. It was also useful for finding speed and time for a complicated force, like a bike with air resistance, which is what we modeled in class. The air resistance and frag force were taken into account in this cycling modeling. We were able to break down the complicated equation into parts that we could use as inputs to get an output that gives us velocity and time. The same method was used in. Since we knew that dt should be kept low, we chose 0.1 as the number. This will make the graph move from point to point in small steps and give us superior accuracy. A small number for dt lets us see the details of how we want to see velocity slowly going up over time. image image image

image