Skip to content
Snippets Groups Projects

Raza master patch 42918

18 files
+ 4583
286
Compare changes
  • Side-by-side
  • Inline

Files

+ 368
0
%% Cell type:markdown id: tags:
# Übung 1 zur Computersimulation und Spektroskopie in der kondensierten Phase
---
# LÖSUNG
%% Cell type:code id: tags:
``` python
import numpy as np
```
%% Cell type:markdown id: tags:
<a id='uebung'></a>
### Übung
#### Aufgabe 1
Schreiben Sie den Python Code für folgende mathematische Ausdrücke und berechnen Sie das Ergebnis:
%% Cell type:markdown id: tags:
#### 1.a)
\begin{equation}
2 \ (3 + 2)
\end{equation}
%% Cell type:code id: tags:
``` python
2*(3+2)
```
%% Output
10
%% Cell type:markdown id: tags:
#### 1.b)
\begin{equation}
\frac{10^2+3}{2\cdot3}
\end{equation}
%% Cell type:code id: tags:
``` python
(10**2+3)/(2*3)
```
%% Output
17.166666666666668
%% Cell type:markdown id: tags:
#### 1.c)
\begin{equation}
\sqrt{2} + \cos(\pi + 1)
\end{equation}
%% Cell type:code id: tags:
``` python
np.sqrt(2)+np.cos(np.pi+1)
```
%% Output
0.8739112565049553
%% Cell type:markdown id: tags:
#### 1.d)
\begin{equation*}
\sum_{i\,=\,0, 2, 4, ...}^{50} i + i^2
\end{equation*}
%% Cell type:code id: tags:
``` python
result = 0
for i in range(0, 51, 2):
result = result + i + i**2
result
```
%% Output
22750
%% Cell type:markdown id: tags:
#### 1.e)
\begin{equation}
\sum_{i\,=\,1}^{10} \mathrm{e}^{-\,f(i\,)}
\end{equation}
\begin{equation}
f(i\,)=
\begin{cases}
i, & \text{falls}\ i \ \mathrm{gerade} \\
1, & \text{falls}\ i \ \mathrm{ungerade}
\end{cases}
\end{equation}
%% Cell type:code id: tags:
``` python
result = 0
for i in range(1, 11):
if i % 2 == 0:
result += np.exp(-i)
else:
result += np.exp(-1)
result
```
%% Output
1.9959077427168899
%% Cell type:markdown id: tags:
#### 1.f)
\begin{equation}
1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} -\ ...
\end{equation}
berechnen Sie das Ergebnis für 100 Terme.
%% Cell type:code id: tags:
``` python
# Leibniz-Reihe
result = 0
for i in range(0, 100):
result += (-1)**i/(2*i+1)
result
```
%% Output
0.7828982258896384
%% Cell type:code id: tags:
``` python
result = 0
for i in range(0, 100):
if i % 2 == 0:
result += 1/(2*i+1)
else:
result += -1/(2*i+1)
result
```
%% Output
0.7828982258896384
%% Cell type:code id: tags:
``` python
result*4
```
%% Output
3.1315929035585537
%% Cell type:markdown id: tags:
#### 1.g)
\begin{equation}
\sum_{i\,=\,0}^{50} \sum_{j\,=\,0}^{50} (\,i\,-\,j\,)^2
\end{equation}
%% Cell type:code id: tags:
``` python
result = 0
for i in range(0, 51):
for j in range(0, 51):
result += (i-j)**2
result
```
%% Output
1127100
%% Cell type:code id: tags:
``` python
for i, k in zip(range(0, 51), range(-50,1)):
print(i, k)
```
%% Output
0 -50
1 -49
2 -48
3 -47
4 -46
5 -45
6 -44
7 -43
8 -42
9 -41
10 -40
11 -39
12 -38
13 -37
14 -36
15 -35
16 -34
17 -33
18 -32
19 -31
20 -30
21 -29
22 -28
23 -27
24 -26
25 -25
26 -24
27 -23
28 -22
29 -21
30 -20
31 -19
32 -18
33 -17
34 -16
35 -15
36 -14
37 -13
38 -12
39 -11
40 -10
41 -9
42 -8
43 -7
44 -6
45 -5
46 -4
47 -3
48 -2
49 -1
50 0
%% Cell type:markdown id: tags:
#### Aufgabe 2
Eine beliebte Aufgabe in Vorstellungsgesprächen ist die Implementierung des Spiels *FizzBuzz*. Bei diesem Spiel wird laut ab Eins aufwärts gezählt. Falls die Zahl durch Drei oder Fünf teibar ist, wird jedoch nicht die Zahl selbst, sondern *Fizz*, bzw. *Buzz* gesagt. Falls Die Zahl durch 15 teilbar ist, wird entsprechend *FizzBuzz* gesagt. Die Sequenz ist also wie folgt: 1, 2, *Fizz*, 4, *Buzz*, *Fizz*, 7, 8, *Fizz*, *Buzz*, 11, *Fizz*, 13, 14, *FizzBuzz*, ...
**Implementieren Sie dieses Spiel bis zur zur Zahl 30.**
Tipp: Denken Sie an den Modulu (%) Operator.
%% Cell type:code id: tags:
``` python
for i in range(1,31):
if i % 15 == 0:
print("Fizz Buzz")
elif i % 5 == 0:
print("Buzz")
elif i % 3 == 0:
print("Fizz")
else:
print(i)
```
%% Output
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz Buzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
Fizz Buzz
%% Cell type:code id: tags:
``` python
for i in range(1,31):
s = ""
if i % 3 == 0:
s += "Fizz"
if i % 5 == 0:
s += "Buzz"
if s == "":
print(i)
else:
print(s)
```
%% Output
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
%% Cell type:markdown id: tags:
---
Loading