Assessment test 2022

Assessment test 2022

You may gain maximum of 2 points. To obtain the assessment, it is neccessary to gain at least 1 point.
Fill your solution to this notebook and send it to petr.valenta[at]email.com no later than 13/05/2022 9:00 CEST.

import numpy as np
import scipy.linalg as la
import matplotlib.pyplot as plt

Task 1

Fit the following data

\( i \)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

\( x_i \)

0.00

0.45

0.90

1.35

1.79

2.24

2.69

3.14

3.59

4.04

4.49

4.93

5.38

5.83

6.28

\( y_i \)

2.74

1.70

1.57

0.04

-1.30

-0.70

-1.36

-0.78

-0.50

-0.31

1.63

2.74

2.78

3.77

1.39

using the least squares method with the model function defined as

\[ f \left( x; \beta \right) = \beta_0 + \beta_1 \sin \left[ \frac{2 \pi \left( x - x_{\mathrm{min}} \right)}{x_{\mathrm{max}} - x_{\mathrm{min}}} \right] + \beta_2 \cos \left[ \frac{2 \pi \left( x - x_{\mathrm{min}} \right)}{x_{\mathrm{max}} - x_{\mathrm{min}}} \right], \]

where \( \beta = \left[ \beta_0, \, \beta_1, \, \beta_2 \right] \) is the vector of adjustable parameters and \( x_{\mathrm{min}} \) and \( x_{\mathrm{max}} \) are the minimum and maximum values of \( x \), respectively.

[0.75 points] Print the values of \( \beta_0 \), \( \beta_1 \), and \( \beta_2 \).
[0.25 points] Plot the datapoints \( \left( x_i, y_i \right) \) as well as the calculated fitting function \( f \left( x; \beta \right) \) in the interval \( \left[ x_{\mathrm{min}}, \, x_{\mathrm{max}} \right] \).

x_p = np.array([0.00, 0.45, 0.90, 1.35, 1.79, 2.24, 2.69, 3.14, 3.59, 4.04, 4.49, 4.93, 5.38, 5.83, 6.28])
y_p = np.array([2.74, 1.70, 1.57, 0.04, -1.30, -0.70, -1.36, -0.78, -0.50, -0.31, 1.63, 2.74, 2.78, 3.77, 1.39])
def least_squares(x, y):
    
    # add your code here
    
    return lambda x: beta[0] + beta[1] * np.sin((2.0 * np.pi * (x - np.min(x)) / (np.max(x) - np.min(x)))) \
        + beta[2] * np.cos(2.0 * np.pi * (x - np.min(x)) / (np.max(x) - np.min(x)))

Task 2

Solve the following ordinary differential equation

\[ y^{\ \prime \prime} \! \left( x \right) = \frac{1}{2 \, \left[ 1 + y \left( x \right) \right]^{\, 2}} - \frac{1}{2} \]

with the initial conditions \( y \left( 0 \right) = 2 \) and \( y^{\ \prime} \! \left( 0 \right) = 0 \) in the interval \( x \in \left[ 0, \ 25 \right] \) using the \( 4^{\mathrm{th}} \) order Runge-Kutta method with the finite step of size \( h = 0.1 \).

[0.75 points] Print the values of \( y \left( 25 \right) \) and \( y^{\ \prime} \! \left( 25 \right) \).
[0.25 points] Plot \( y \left( x \right) \), \( y^{\ \prime} \! \left( x \right) \), and \( y^{\ \prime \prime} \! \left( x \right) \) in the interval \( x \in \left[ 0, \ 25 \right] \).

def runge_kutta_4(f, g, x_0, x_n, y_0, z_0, h):

    # add your code here
    
    return x, y, z