Skip to content
Snippets Groups Projects
Commit 8e4fb478 authored by Berend Bouvy's avatar Berend Bouvy
Browse files

Updating GA_1_^6 and restoring the solution file as well

parent 334f8ab2
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:b5955612-0118-4fd4-8694-5fe52ce1be5a tags:
 
# GA 1.6: An ODE to Probably Doing Enough (PDE)
 
<h1 style="position: absolute; display: flex; flex-grow: 0; flex-shrink: 0; flex-direction: row-reverse; top: 60px;right: 30px; margin: 0; border: 0">
<style>
.markdown {width:100%; position: relative}
article { position: relative }
</style>
<img src="https://gitlab.tudelft.nl/mude/public/-/raw/main/tu-logo/TU_P1_full-color.png" style="width:100px" />
<img src="https://gitlab.tudelft.nl/mude/public/-/raw/main/mude-logo/MUDE_Logo-small.png" style="width:100px" />
</h1>
<h2 style="height: 10px">
</h2>
 
*[CEGM1000 MUDE](http://mude.citg.tudelft.nl/): Week 1.6. For: 11 October, 2024.*
 
%% Cell type:markdown id:e143a00c-1b09-435f-b581-80190df01a1c tags:
 
# Overview
 
This assignment contains two parts: treating non-linear ODEs and treating the diffusion equation (PDE).
 
%% Cell type:code id:453992c1 tags:
 
``` python
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as widgets
from ipywidgets import interact
```
 
%% Cell type:markdown id:0933143e tags:
 
## Part 1: Solving Non-linear ODEs
 
In task 1 you will solve first a very simple equation using Newton-Rhapson to understand exactly how to implement it. Task 2 treats the solution of a non-linear ODE w.r.t. time, first with Explicit Euler and then with Implicit Euler. The latter will require again Newton-Rhapson to find the solution.
 
%% Cell type:markdown id:735043d3 tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 1</b>
 
The equation to solve using Newton-Rhapson is
 
$$
x^2=9
$$
</p>
</div>
 
%% Cell type:markdown id:17ca3c02 tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 1.1</b>
 
Formally Newton-Rhapson is implemented iterating the solution as follows:
$$
z_{j+1} = z_{j} - g(z_j)/g'(z_j)
$$
where $g(z_j) = 0$ and $z_j$ is a guess and $z_{j+1}$ is the improved guess.
 
As we do not care about retaining the values of every guess, it can be written in code as:
 
$$
x = x - g(x)/g'(x)
$$
 
**Transform the equation $x^2=9$ to g(x) and write it below, together with g'(x).**
 
</p>
</div>
 
%% Cell type:markdown id:769c78ae tags:
 
Write your answer here.
 
%% Cell type:markdown id:4f3fc628 tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 1.2</b>
 
Implement your equations $g(x)$ and $g'(x)$ in the code below, as well as the Newton-Rhapson expression inside the loop. Test the code with the initial guess of $x=10$.
</p>
</div>
 
%% Cell type:code id:25bcec4e tags:
 
``` python
import numpy as np
def g(x):
return YOUR_CODE_HERE
 
def g_der(x):
return YOUR_CODE_HERE
 
x = YOUR_CODE_HERE
for j in range(100):
x = YOUR_CODE_HERE
# Next task will go here
 
print("The solution found is ", x, " it took " ,j , " iterations to converge.")
```
 
%% Cell type:markdown id:c210989a tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 1.3</b>
 
The code is taking 100 iterations without stopping. **Add a condition to the code above to stop the loop once the solution is good enough**, i.e., when the solution is closer than $\epsilon = 1e-6$ to the exact solution. How many iterations does it take now to converge?
The code is taking 100 iterations without stopping. **Add a condition to the code above to stop the loop once the solution is good enough**, i.e., when the solution is closer than $\epsilon = 10^{-6}$ to the exact solution. How many iterations does it take now to converge?
 
</p>
</div>
 
%% Cell type:markdown id:728a8de9 tags:
 
Write your answer here.
 
%% Cell type:markdown id:e4723ca1 tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 1.4</b>
 
Change the intial guess to $x=0.01$, which is closer to the exact solution than the initial guess in the previous task. How many iterations does it take to converge? Explain the difference.
 
</p>
</div>
 
%% Cell type:markdown id:99d7f4c8 tags:
 
Write your answer here.
 
%% Cell type:markdown id:fcb52681 tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 2</b>
 
Solve the following ODE using Explicit and Implicit Euler.
 
$$
\frac{dy}{dt} = \sin(y^3)+\sin(t)
$$
 
with initial value $y(t=0) = 1$
 
 
</p>
</div>
 
%% Cell type:markdown id:6ef1b02a tags:
 
Write your answer here.
 
%% Cell type:markdown id:8b10ee75 tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 2.1</b>
 
Write in paper the Explicit and Implicit Euler schemes of the equation above.
Which term from your Implicit Euler scheme represents $z$?
 
 
</p>
</div>
 
%% Cell type:markdown id:bcaed558 tags:
 
Write your answer here.
 
%% Cell type:markdown id:3a0f172f tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 2.2</b>
 
Just as before, Newton-Rhapson must be implemented following:
$$
z_{j+1} = z_{j} - g(z_j)/g'(z_j)
$$
where $g(z_j) = 0$ and $z_j$ is a guess and $z_{j+1}$ is the improved guess.
 
**Transform your Implicit Euler scheme into g(*) and write it below, together with g'(*).**
 
</p>
</div>
 
%% Cell type:markdown id:e40042c0 tags:
 
Write your answer here.
 
%% Cell type:markdown id:726c43cb tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 2.3</b>
 
Implement the Explicit Euler and Implicit Euler schemes by filling the lines of code below:
 
- a) Code the functions g() and g'()
- b) Implement Explicit Euler
- c) Implement Implicit Euler. Tip: use as initial guess the value of the previous time step.
- d) Use a dt = 0.25s
 
</p>
</div>
 
%% Cell type:code id:57c0662a tags:
 
``` python
def g(y_iplus1,y_i, t_iplus1):
def g(y_iplus1, y_i, t_iplus1):
return YOUR_CODE_HERE
 
def g_der(y_iplus1):
return YOUR_CODE_HERE
 
 
# Define parameters:
dt = .3
dt = .25
t_end = 10
t = np.arange(0,t_end+dt,dt)
 
y_EE = np.zeros(t.shape)
y_IE = np.zeros(t.shape)
 
# Define Initial Conditions
y_EE[0] = YOUR_CODE_HERE
y_IE[0] = YOUR_CODE_HERE
 
# Perform time-integration
newtonFailed = 0
for i in range(0, len(t)-1):
 
# Forward Euler:
y_EE[i+1] = YOUR_CODE_HERE
 
# Backward Euler:
y_IE[i+1] = YOUR_CODE_HERE # Initial guess
for j in range(200):
y_IE[i+1] = YOUR_CODE_HERE
if np.abs(g(y_IE[i+1],y_IE[i],t[i+1])) < 1e-6:
if np.abs(g(y_IE[i+1], y_IE[i], t[i+1])) < 1e-6:
break
 
if j >= 199:
newtonFailed = 1
 
 
# Plotting the solution
plt.plot(t, y_EE, 'r', t, y_IE, 'g--')
if newtonFailed:
plt.title('Nonlinear ODE with dt = ' + str(dt) + ' \nImplicit Euler did not converge')
else:
plt.title('Nonlinear ODE with dt = ' + str(dt))
 
plt.xlabel('t')
plt.ylabel('y')
plt.gca().legend(('Explicit','Implicit'))
plt.grid()
plt.show()
```
 
%% Cell type:markdown id:6b6d9964 tags:
 
## Part 2: Diffusion Equation in 1D
 
The 1-D diffusion equation reads $$\frac{\partial u}{\partial t}=v\frac{\partial^2 u}{\partial x^2}$$
 
where $u$ is a continuous function in space and time, $v$ is a constant and often referred to as the **diffusivity coefficient**, giving rise to the name 'diffusion equation'. This is a Partial Differential Equation which independent variable, $u$, varies on space and time. This equation is virtually present across all fields of civil engineering and science. Here, we use it to represent the temperature on a rod (see the sketch below).
 
Unlike the problem of Wednesday, here there is no exchange of heat with the ambient and the temperature evolves in time. The temperature initially is uniform along the rod, equal to $7°C$. Then it is heated at both ends with the temperatures shown in the figure.
 
![Thermal Gradient](./figures/thermal_gradient.png)
 
The problem is schematized as a one-dimensional $0.3 m$ steel rod of with a diffusivity coefficient of $4e-6 m^2/s$. Run the simulation for $10,000 s$ to see the progression of the temperature through the model. Start with $200$ time steps and use 15 points to represent the rod.
 
%% Cell type:markdown id:c300a7fd tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 3</b>
 
Solve the diffusion equation using Central Differences in space and Forward Differences in time. You will do this step by step in the following sub-tasks.
 
For convenience we write here the diffusion equation with the temperature variable:
 
$$
\frac{\partial T}{\partial t}=v\frac{\partial^2 T}{\partial x^2}
$$
 
 
 
</p>
</div>
 
%% Cell type:markdown id:84b32366 tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 3.1:</b>
 
How many constraints are needed in the 1D diffusion equation to have a well-posed problem?
</p>
</div>
 
%% Cell type:markdown id:1b363563 tags:
 
Write your answer here.
 
%% Cell type:markdown id:4ae351ba tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 3.2:</b>
 
Draw a grid of 6 points with subindexes. Although your actual grid will be much larger, 6 points are enough to visualize the procedure. The initial condition states that the temperature of the rod is $7^o$ C. Does that mean that one single point of your grid is initialized or all of them?
 
 
</p>
</div>
 
%% Cell type:markdown id:1d59533c tags:
 
Write your answer here.
 
%% Cell type:markdown id:71a1284a tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 3.3:</b>
 
Now, the differential equation needs to be expressed in algebraic form using central differences in space AND forward differences in time. **Start by just transforming the PDE into a first-order ODE by ONLY applying Central Differences to the spatial derivative term.**
 
 
</p>
</div>
 
%% Cell type:markdown id:d0d45222 tags:
 
Write your answer here.
 
%% Cell type:markdown id:6ab571e7 tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 3.4:</b>
 
Before applying Forward Differences in time to the equation. You need to add a superscript to the notation that indicates the time step: $T^j_i$. So, $i$ indicates the spatial location and $j$ the time location. For example, $T^0_2$ indicates the temperature at the node $i=2$ and at the initial moment $j=0 (t=0)$.
 
***Hint***: To express in a general form a node of over the next time step, you can write $T^{j+1}_i$.
 
**Apply Forward Differences to the equation to obtain an algebraic expression.**
</p>
</div>
 
%% Cell type:markdown id:c46ee55f tags:
 
Write your answer here.
 
%% Cell type:markdown id:d6b4b1c9 tags:
 
 
<div style="background-color:#facb8E; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>NOTE</b>
 
If you have doubts of your solution, <b>stop</b> and ask a staff member! It is important to be on the right track!!
 
</p>
</div>
 
%% Cell type:markdown id:2ad1f7c0-14d4-4363-8ed9-681e1e271741 tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 3.5:</b>
 
Finally, some coding! Let's start with defining the parameters and creating the grid. **Fill in the missing parts of the code.**
 
</p>
</div>
 
%% Cell type:code id:efd223ed-a7db-4680-8c81-649ea88b5275 tags:
 
``` python
# Initial conditions
 
T_left = YOUR_CODE_HERE
T_right = YOUR_CODE_HERE
T_initial = YOUR_CODE_HERE
T_left = YOUR_CODE_HERE # Temperature at left boundary
T_right = YOUR_CODE_HERE # Temperature at right boundary
T_initial = YOUR_CODE_HERE # Initial temperature
 
length = YOUR_CODE_HERE
nu = YOUR_CODE_HERE
length = YOUR_CODE_HERE # Length of the rod
nu = YOUR_CODE_HERE # Thermal diffusivity
 
dx = YOUR_CODE_HERE
x = YOUR_CODE_HERE
dx = YOUR_CODE_HERE # spatial step size
x = YOUR_CODE_HERE # spatial grid
 
dt = YOUR_CODE_HERE
dt = YOUR_CODE_HERE # time step size
```
 
%% Cell type:markdown id:a139660e-4185-41a4-99db-c54c68c54b76 tags:
 
Let's initialise the system with the initial and boundary conditions. Say $t_0 =0$.
 
%% Cell type:markdown id:0e235c76 tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 3.6:</b>
 
Define the initial conditions and the boundary conditions. **Fill in the missing parts of the code.**
 
We define a 2-dimensional Numpy array <code>T</code> where the first index, <code>j</code>, represents time and the second index, <code>i</code>, represents space, for example: <code>T[j, i]</code>. Initialize <code>T</code> with a matrix of zeros.
 
Then, implement the equation you derived earlier.
 
</p>
</div>
 
%% Cell type:code id:673b1ff5-06e2-44c8-b6a4-031a00b0f190 tags:
 
``` python
T = YOUR_CODE_HERE
T[0, :] = YOUR_CODE_HERE
T[:, 0] = YOUR_CODE_HERE
T[:, -1] = YOUR_CODE_HERE
 
b = YOUR_CODE_HERE
```
 
%% Cell type:markdown id:e7c5bf2e tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 3.7:</b>
 
Write in paper the equations that come out from your algebraic representation of the diffusion equation, solving for the unknowns. Use it then to write the matrix A, the unknown vector T and vector b. As in the workshop and textbook, the <code>A</code> matrix consists only of the unknowns in the problem.
 
</p>
</div>
 
%% Cell type:markdown id:7e0147f1 tags:
 
Your answer here.
 
%% Cell type:code id:787c37f6 tags:
 
``` python
for j in range(m-1):
A = YOUR_CODE_HERE
b = YOUR_CODE_HERE
 
T[j+1,1:-1] = YOUR_CODE_HERE
```
 
%% Cell type:markdown id:794f6329 tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 3.8:</b>
 
Use this code cell if you would like to verify your numerical implementation. For example, to visualize the temperature profile at different time steps.
 
</p>
</div>
 
%% Cell type:code id:56f6fdea tags:
 
``` python
# plt.plot(x, T[YOUR_CODE_HERE,:])
# plt.plot(x, T[YOUR_CODE_HERE,:])
# plt.plot(x, T[YOUR_CODE_HERE,:])
# plt.xlabel('x')
# plt.ylabel('T')
# plt.show()
```
%% Cell type:markdown id:17e7be50-79a7-4699-b0d8-908a58ce36d7 tags:
def plot_T(T):
'''
Function to plot the temperature profile at different time steps.
'''
def plot_temperature(time_step):
plt.plot(x, T[time_step, :])
plt.xlabel('x [m]')
plt.ylabel('T [°C]')
plt.title(f'Temperature profile at time step {time_step}')
plt.grid()
plt.ylim(5, 40)
plt.show()
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 3.9:</b>
interact(plot_temperature, time_step=widgets.Play(min=0, max=len(t)-1, step=3, value=0))
```
 
Describe the time evolution of the temperature along the rod. Does the temperature reach a steady-state? What does that mean for heat flow?
%% Cell type:code id:c1c62fba tags:
 
Write your answer in the following markdown cell.
</p>
</div>
``` python
plot_T(T)
```
 
%% Cell type:markdown id:8db518cd tags:
 
Your answer here.
 
%% Cell type:markdown id:9fcbb39f tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 4</b>
 
Alter the right boundary condition, with the following equation:
 
 
$$
u^t_{x=L} = 25 + 10 \sin \left(\frac{2\pi t}{period}\right)
$$
 
where L refers to the last point of the rod. Put your whole code together in a single cell. Copy the code cells from task 3.5 until task 3.8. Modify the right boundary condition as stated above, the period is 6000 seconds.
 
 
</p>
</div>
 
%% Cell type:code id:05534522 tags:
 
``` python
YOUR_CODE_HERE
```
 
%% Cell type:markdown id:852b05c5 tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 5</b>
 
Solve the diffusion equation using Central Differences in space but **now with Backward Differences in time**. You will do this step by step just as before.
 
 
</p>
</div>
 
%% Cell type:markdown id:81fe7677 tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 5.1:</b>
 
Draw the stencils (two in total) of this equation when solving it with Central Differences in space and **Forward Differences in time** and when solving it with Central Differences in space and **Backward Differences in time**.
 
</p>
</div>
 
%% Cell type:markdown id:6df8a151 tags:
 
Your answer here.
 
%% Cell type:markdown id:ca16ee10 tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 5.2:</b>
 
Now, the differential equation needs to be expressed in algebraic form using central differences in space and forward differences in time. **Start by just transforming the PDE into a first-order ODE by ONLY applying Central Differences to the spatial derivative term.**
 
 
</p>
</div>
 
%% Cell type:markdown id:4a25a0d0 tags:
 
Your answer here.
 
%% Cell type:markdown id:139d33af tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 5.3:</b>
 
Apply Backward Differences to the equation to obtain an algebraic expression.
 
</p>
</div>
 
%% Cell type:markdown id:73b82177 tags:
 
Your answer here.
 
%% Cell type:markdown id:25b64dff tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 5.4:</b>
 
Write in paper the equations that come out from your algebraic representation of the diffusion equation, solving for the unknowns. Use it then to write the matrix A, the unknown vector T and vector b.
 
</p>
</div>
 
%% Cell type:markdown id:8c1db830 tags:
 
Your answer here.
 
%% Cell type:markdown id:36284ee9 tags:
 
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 5.5</b>
 
Copy the code of task 4 and make sure to use the Dirichlet conditions of task 3. Implement the Implicit scheme by modifying the code of how the matrix A and vector b are built.
 
</p>
</div>
 
%% Cell type:code id:4edddcaf tags:
 
``` python
YOUR_CODE_HERE
```
 
%% Cell type:markdown id:3d1dfe3d tags:
 
**End of notebook.**
<h2 style="height: 60px">
</h2>
<h3 style="position: absolute; display: flex; flex-grow: 0; flex-shrink: 0; flex-direction: row-reverse; bottom: 60px; right: 50px; margin: 0; border: 0">
<style>
.markdown {width:100%; position: relative}
article { position: relative }
</style>
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">
<img alt="Creative Commons License" style="border-width:; width:88px; height:auto; padding-top:10px" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" />
</a>
<a rel="TU Delft" href="https://www.tudelft.nl/en/ceg">
<img alt="TU Delft" style="border-width:0; width:100px; height:auto; padding-bottom:0px" src="https://gitlab.tudelft.nl/mude/public/-/raw/main/tu-logo/TU_P1_full-color.png"/>
</a>
<a rel="MUDE" href="http://mude.citg.tudelft.nl/">
<img alt="MUDE" style="border-width:0; width:100px; height:auto; padding-bottom:0px" src="https://gitlab.tudelft.nl/mude/public/-/raw/main/mude-logo/MUDE_Logo-small.png"/>
</a>
 
</h3>
<span style="font-size: 75%">
&copy; Copyright 2023 <a rel="MUDE Team" href="https://studiegids.tudelft.nl/a101_displayCourse.do?course_id=65595">MUDE Teaching Team</a> TU Delft. This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>.
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment