# CFL and Nyquist conditions

**Nicolás Guarín-Zapata**

This notebook shows interactively the CFL and Nyquist-Shanon conditions

This document is based on a Notebook from ["Practical Numerical Methods with Python"](http://openedx.seas.gwu.edu/courses/GW/MAE6286/2014_fall/about) written by L.A. Barba, G.F. Forsyth, C. Cooper (2014).


```
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from matplotlib import rcParams
rcParams['font.family'] = 'serif'
rcParams['font.size'] = 16
```

## CFL Condition

The *one-dimensional linear convection equation* is the simplest case for study the CFL condition. Here it is:

\begin{equation}\frac{\partial u}{\partial t} + c \frac{\partial u}{\partial x} = 0\end{equation}

The equation represents a *wave* propagating with speed $c$ in the $x$ direction, without change of shape. For that reason, it's sometimes called the *one-way wave equation* (sometimes also the *advection equation*).

With an initial condition $u(x,0)=u_0(x)$, the equation has an exact solution given by:

\begin{equation}u(x,t)=u_0(x-ct). 
\end{equation}


```
def linearconv(nx):
    """Solve the linear convection equation.
    
    Solves the equation d_t u + c d_x u = 0 where 
    * the wavespeed c is set to 1
    * the domain is x \in [0, 2]
    * 20 timesteps are taken, with \Delta t = 0.025
    * the initial data is the hat function
    
    Produces a plot of the results
    
    Parameters
    ----------
    
    nx : integer
        number of internal grid points
        
    Returns
    -------
    
    None : none
    """
    dx = 2./(nx-1)
    nt = 20    
    dt = .025  
    c = 1

    u = np.ones(nx)      
    u[.5/dx : 1/dx+1]=2  

    un = np.ones(nx) 

    for n in range(nt): 
        un = u.copy() 
        u[1:] = un[1:] -c*dt/dx*(un[1:] -un[0:-1]) 
        u[0] = 1.0
        

    plt.plot(np.linspace(0,2,nx), u, color='#003366', ls='-', lw=3)
    plt.ylim(0,2.5); 
```

### Different spatial discretizations


```
linearconv(11)
```


```
linearconv(21)
```


```
linearconv(41)
```


```
linearconv(81)
```


```
linearconv(91)
```


```
linearconv(201)
```

### Nyquist-Shanon condition


```
from scipy import interpolate
n = 120
x = np.linspace(-np.pi, np.pi, n)
y = np.sin(5*x)
plt.plot(x,y, lw=2);
plt.xlim(-np.pi, np.pi);
f = interpolate.interp1d(x, y, kind='quadratic')
```


```
def plot_smooth(x, y, n):
    f = interpolate.interp1d(x, y, kind='quadratic')
    xnew = np.linspace(-np.pi, np.pi, n)
    plt.plot(x,y, 'ro', xnew, f(xnew) , '--r', lw=2);
```

Now we are going to play a little bit with the sampling rate


```

```


```
n = 5
xnew = np.linspace(-np.pi, np.pi, n)
ynew = f(xnew)
plt.plot(x,y, lw=2);
plot_smooth(xnew, ynew, 100)
plt.ylim(-1.5,1.5)
```


```
n = 8
xnew = np.linspace(-np.pi, np.pi, n)
plt.plot(x,y, lw=2);
plot_smooth(xnew, f(xnew), 100)
plt.ylim(-1.5,1.5)
```


```
n = 10
xnew = np.linspace(-np.pi, np.pi, n)
plt.plot(x,y, lw=2);
plot_smooth(xnew, f(xnew), 100)
plt.ylim(-1.5,1.5)
```


```
n = 15
xnew = np.linspace(-np.pi, np.pi, n)
plt.plot(x,y, lw=2);
plot_smooth(xnew, f(xnew), 100)
plt.ylim(-1.5,1.5)
```


```
n = 20
xnew = np.linspace(-np.pi, np.pi, n)
plt.plot(x,y, lw=2);
plot_smooth(xnew, f(xnew), 100)
plt.ylim(-1.5,1.5)
```


```
n = 30
xnew = np.linspace(-np.pi, np.pi, n)
plt.plot(x,y, lw=2);
plot_smooth(xnew, f(xnew), 100)
plt.ylim(-1.5,1.5)
```


```
n = 50
xnew = np.linspace(-np.pi, np.pi, n)
plt.plot(x,y, lw=2);
plot_smooth(xnew, f(xnew), 100)
plt.ylim(-1.5,1.5)
```

---

######The cell below loads the style of the notebook.


```
from IPython.core.display import HTML
css_file = 'styles/numericalmoocstyle.css'
HTML(open(css_file, "r").read())
```




<link href='http://fonts.googleapis.com/css?family=Alegreya+Sans:100,300,400,500,700,800,900,100italic,300italic,400italic,500italic,700italic,800italic,900italic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Arvo:400,700,400italic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=PT+Mono' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Shadows+Into+Light' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Nixie+One' rel='stylesheet' type='text/css'>
<style>

@font-face {
    font-family: "Computer Modern";
    src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');
}

#notebook_panel { /* main background */
    background: rgb(245,245,245);
}

div.cell { /* set cell width */
    width: 750px;
}

div #notebook { /* centre the content */
    background: #fff; /* white background for content */
    width: 1000px;
    margin: auto;
    padding-left: 0em;
}

#notebook li { /* More space between bullet points */
margin-top:0.8em;
}

/* draw border around running cells */
div.cell.border-box-sizing.code_cell.running { 
    border: 1px solid #111;
}

/* Put a solid color box around each cell and its output, visually linking them*/
div.cell.code_cell {
    background-color: rgb(256,256,256); 
    border-radius: 0px; 
    padding: 0.5em;
    margin-left:1em;
    margin-top: 1em;
}

div.text_cell_render{
    font-family: 'Alegreya Sans' sans-serif;
    line-height: 140%;
    font-size: 125%;
    font-weight: 400;
    width:600px;
    margin-left:auto;
    margin-right:auto;
}


/* Formatting for header cells */
.text_cell_render h1 {
    font-family: 'Nixie One', serif;
    font-style:regular;
    font-weight: 400;    
    font-size: 45pt;
    line-height: 100%;
    color: rgb(0,51,102);
    margin-bottom: 0.5em;
    margin-top: 0.5em;
    display: block;
}	
.text_cell_render h2 {
    font-family: 'Nixie One', serif;
    font-weight: 400;
    font-size: 30pt;
    line-height: 100%;
    color: rgb(0,51,102);
    margin-bottom: 0.1em;
    margin-top: 0.3em;
    display: block;
}	

.text_cell_render h3 {
    font-family: 'Nixie One', serif;
    margin-top:16px;
	font-size: 22pt;
    font-weight: 600;
    margin-bottom: 3px;
    font-style: regular;
    color: rgb(102,102,0);
}

.text_cell_render h4 {    /*Use this for captions*/
    font-family: 'Nixie One', serif;
    font-size: 14pt;
    text-align: center;
    margin-top: 0em;
    margin-bottom: 2em;
    font-style: regular;
}

.text_cell_render h5 {  /*Use this for small titles*/
    font-family: 'Nixie One', sans-serif;
    font-weight: 400;
    font-size: 16pt;
    color: rgb(163,0,0);
    font-style: italic;
    margin-bottom: .1em;
    margin-top: 0.8em;
    display: block;
}

.text_cell_render h6 { /*use this for copyright note*/
    font-family: 'PT Mono', sans-serif;
    font-weight: 300;
    font-size: 9pt;
    line-height: 100%;
    color: grey;
    margin-bottom: 1px;
    margin-top: 1px;
}

.CodeMirror{
        font-family: "PT Mono";
        font-size: 90%;
}

</style>





f2ef90e5a9d1febb284d8435a66acc25d89b9ba7
ipynb
Jupyter Notebook
2014/ce795/CFL_Nyquist_conditions.ipynb
nicoguaro/talks
01e9ddc4a44952a1ea8b1d8acf3cbc17ddbc31e2
MIT
2014/ce795/CFL_Nyquist_conditions.ipynb
nicoguaro/talks
01e9ddc4a44952a1ea8b1d8acf3cbc17ddbc31e2
MIT
2014/ce795/CFL_Nyquist_conditions.ipynb
nicoguaro/talks
01e9ddc4a44952a1ea8b1d8acf3cbc17ddbc31e2
MIT
Qwen/Qwen-72B
1. YES
2. YES