Skip to content
Snippets Groups Projects
Commit 31a0185a authored by JuanPi Carbajal's avatar JuanPi Carbajal
Browse files

Explicit euler exercise

parent 0f7b0cec
No related branches found
No related tags found
No related merge requests found
'''
Copyright (C) 2018 - Juan Pablo Carbajal
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
# Author: Juan Pablo Carbajal <juanpablo.carbajal@hs.ch>
import numpy as np
import time
# Die sind die Problemparameter
# Du kannst verschiedene Werte ausprobieren!
a = -9.81 # Beschleunigung == Erdbeschleunigung [m/s^2]
v0 = 0.0 # Anfangsgeschwindigkeit [m/s]
T = 3 # Simulierte Dauer [s]
dt = 0.1 # Zeitschritt [s]
# Hier wir weisen Speicher zu
t = np.arange(0, T + dt/2, dt) # Zeitwertarray
nT = len(t) # Anzahl der Zeitschritte
v = np.zeros(nT) # Speicherzuweisung für Geschwindigkeitarray
'''
Implementiert deinen Explizite Euler-Verfahren in den folgenden Zeilen
Erinnerung: Das explizite Euler-Verfahren, kurz gesagt
a) Approximation der Ableitung
∂v v(t+dt) - v(t)
----(t) ~ ------------------- := a
∂t dt
b) Aktualisierung des Unbekannten
v(t+dt) = v(t) + a * dt
'''
# Gibt hier deinen Code ein
# Hier plotten wir die Ergebnisse der Integration
import matplotlib.pyplot as plt
plt.ion() # interaktive plotten "on"
#plt.clf() # Diese Zeile auskommentieren, um den Plot zu leeren
plt.plot(t,v,'.-') # Punkt-Dash-Plot der Funktion
plt.show() # Zeigt den Plot
'''
Copyright (C) 2018 - Juan Pablo Carbajal
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
# Author: Juan Pablo Carbajal <juanpablo.carbajal@hs.ch>
import numpy as np
import time
# Die sind die Problemparameter
# Du kannst verschiedene Werte ausprobieren!
a = -9.81 # Beschleunigung == Erdbeschleunigung [m/s^2]
v0 = 0.0 # Anfangsgeschwindigkeit [m/s]
T = 3 # Simulierte Dauer [s]
dt = 0.1 # Zeitschritt [s]
# Hier wir weisen Speicher zu
t = np.arange(0, T + dt/2, dt) # Zeitwertarray
nT = len(t) # Anzahl der Zeitschritte
v = np.zeros(nT) # Speicherzuweisung für Geschwindigkeitarray
'''
Implementiert deinen Explizite Euler-Verfahren in den folgenden Zeilen
Erinnerung: Das explizite Euler-Verfahren, kurz gesagt
a) Approximation der Ableitung
∂v v(t+dt) - v(t)
----(t) ~ ------------------- := a
∂t dt
b) Aktualisierung des Unbekannten
v(t+dt) = v(t) + a * dt
'''
# Gibt hier deinen Code ein
''' Lösung: Schleife '''
wallclk = time.time()
v[0] = v0
for i in range(nT - 1):
v[i+1] = v[i] + a * dt
print ('Elapsed', time.time() - wallclk)
''' Lösung: itertools'''
from itertools import accumulate
wallclk = time.time()
dv = a * dt
step = lambda v,dummy: v + dv
v_ = np.array(list(accumulate(v, step)))
print ('Elapsed ', time.time() - wallclk)
# Hier plotten wir die Ergebnisse der Integration
import matplotlib.pyplot as plt
plt.ion() # interaktive plotten "on"
#plt.clf() # Diese Zeile auskommentieren, um den Plot zu leeren
plt.plot(t,v,'.-') # Punkt-Dash-Plot der Funktion
plt.show() # Zeigt den Plot
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment