Skip to content
Snippets Groups Projects
Commit a19b5533 authored by William Dos Santos's avatar William Dos Santos
Browse files

Added exercise and lecture week 10

parent 142543ed
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:becc5c8b tags:
# 8: Plotting functions
## 1 Introduction to matplotlib.pyplot
%% Cell type:markdown id:c9723f78 tags:
## 1.1 Plotting task: representation of geometric shapes
### Aim
The aim of this task is to plot various geometric shapes using Python and Matplotlib
#### Requirements
- Use `matplotlib.pyplot` to plot the functions. Each function group should have a different color.
- **Plot settings**:
- Add a grid to make orientation easier.
- Add a title “Students face”
- Add a legend that identifies the individual function groups.
%% Cell type:code id:cba143bf tags:
``` python
import numpy as np
# Funktionsgruppe 1
t = np.linspace(0, 2*np.pi, 100)
x_1 = 5 * np.cos(t)
y_1 = 5 * np.sin(t)
# Funktionsgruppe 2
x_2 = 2 * np.cos(t) - 2
y_2 = 2 * np.sin(t) + 2
# Funktionsgruppe 3
x_3 = 2 * np.cos(t) + 2
y_3 = 2 * np.sin(t) + 2
# Funktionsgruppe 4
t2 = np.linspace(0, np.pi, 100)
x_4 = 3 * np.cos(t2)
y_4 = 3 * np.sin(t2 + np.pi ) - 0.5
```
%% Cell type:markdown id:68f742ed tags:
## 1.2 Plotting data and style it
### 1. Generate Data with NumPy:
- Given is a Function that generates a sinus with random noise
$$
y = \sin(t) + \text{random noise}
$$
### 2. Create the Plot:
- Plot the time series as a solid line.
- Highlight the actual measurement points with circles.
### 3. Plot Styling:
- Add axis titles for time (`x-axis`) and measurement values (`y-axis`).
- Add a plot title.
- Differentiate the markers by using a distinct color and a large size.
%% Cell type:code id:bf165039 tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
# 1. Generate data
t = np.linspace(0, 10, 11) # Timevalues
y = np.sin(t) + np.random.normal(0, 0.1, len(t)) # Sinus values with noise
# 2. Generateplot
```
%% Cell type:markdown id:ff1d99ae tags:
## 1.2 Plot task: Damped oscillation of a resonant circuit
#### Aim
The aim of this task is to graphically represent the natural oscillation of a damped oscillating circuit. The oscillation is described by a mathematical function that represents the change in amplitude over time as a function of time.
#### Function to be plotted
The oscillation is described by the following function:
$$
a(t) = a_0 e^{-t/\tau} \cos(2\pi f_n t + \phi)
$$
given are:
- $a_0 = 5$ the amplitude
- $\tau = 0.2$ s the time constant of the attenuation
- $f_n = 10$ Hz the frequency of the resonant circuit
- $\phi = -\pi/2$ the phase shift
## Task definition
1 **Visualize the function**:
- Create a plot of the function \( a(t) \) over a period of 0 to 1 second.
- Make sure that the x-axis shows the time in seconds and the y-axis shows the amplitude.
2 **Adjust the plot settings**:
- Label the x-axis and y-axis appropriately.
- Add an appropriate legend to clearly identify the data being plotted.
- Add a grid to improve the readability of the plot.
%% Cell type:code id:83578527 tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
# Parameter
a0 = 8
tau = 0.3
fn = 16
phi = -np.pi / 2
# Zeitvektor
t = np.linspace(0, 1, 1000)
# Schwingungsfunktion
def damped_oscillation(t, a0, tau, fn, phi):
return a0 * np.exp(-t / tau) * np.cos(2 * np.pi * fn * t + phi)
# Berechnung der Amplitude
a_oscillation = damped_oscillation(t, a0, tau, fn, phi)
# Term Hüllkurve
decay = a0 * np.exp(-t / tau)
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment