Lesson #6 - May 19, 2022


The examples are adapted from:

R. Johansson, "Numerical Python: Scientific Computing and Data Science Applications With Numpy, Scipy and Matplotlib", Apress, 2018

Solution of linear systems

Solve the system of linear equations

$$\left\{ \begin{array}{l} 2x_0 +3x_1 = -4 \\ -x_0 +4x_1 = 2 \end{array}\right.$$

In a matrix form, set

$$A =\left[ \begin{array}{cc} 2 & 3 \\ -1 & 4 \end{array} \right], b =\left[ \begin{array}{c} -4 \\ 2 \end{array} \right]$$

and find

$$ x =\left[ \begin{array}{cc} x_1 \\ x_2 \end{array} \right]$$

so that

$$Ax = b$$

Check that $x$ is indeed the solution

Condition number

Th econdition number should as small (i.e. close to $1$) as possible, otherwise small changes in $b$ results in large variation in $x$. If it is small, the system is well conditionated.

What happens if c.n. is large?

Norm, rank

Least squares

Can be useful if our data have and underlying law which depends linearly on some parameters, such as:

$$ y(x) = a + b\cdot x + c \cdot x^2$$

Add some noise...

The system to solve is this one:

$$\left[ \begin{array}{ccc} 1 & x_0 & x_0^2 \\ 1 & x_1 & x_1^2 \\ 1 & x_2 & x_2^2 \\ \ldots \\1 & x_m & x_m^2 \end{array} \right] \left( \begin{array}{c} a \\ b \\ c \end{array} \right) = \left( \begin{array}{c} y_0 \\ y_1 \\ y_2 \\ \ldots \\ y_m\end{array} \right) $$

Zeros of functions

Find the zero (also named roots) of

$$ f(x) = e^x -2$$

Note that

$$ f'(x) = e^x$$

Bisection method

The possible zeros is bracket by two points where the function has different sign.

We note that $f(0)$ and $f(1)$ differ in sign and so a zero must be between $0$ and $1$.

Newton's method

Using a first-order Taylor expansion

$$ f(x+dx) = f(x) + f'(x) dx $$

and, starting from $x$, looking for $dx$ such that $f(x+dx)=0$ allows to estimate $dx$ as:

$$ dx = - \frac{f(x)}{f'(x)}$$

The prime derivative can be omitted (it is evaluated numerically)

Brent's method

It is an hybrid method.

Requires bracketing points with sign change.

Solving nonlinear system of equations

We have to solve this sytem

$$ \left\{ \begin{array}{l} y - x^3 -2 x^2 +1 = 0 \\ y + x^2 -1 =0 \end{array} \right.$$

Graphically...

We define a vector function in two variables: $f:\mathbb{R}^{2} \to \mathbb{R}^2$

$$ f(x) = \left[ \begin{array}{l} f_0(x_0, x_1) \\ f_1(x_0, x_1) \end{array} \right] = \left[ \begin{array}{l} x_1 - x_0^3 -2 x_0^2 +1 \\ x_1 + x_0^2 -1 \end{array} \right] $$

Note that $x_0 = x$ and $x_1=y$.

A guess value is needed, for example $x = \left[ 1, 0.5 \right]^T$

Integration

Integration of a function, given its expression

$$\int_{-1}^{1} e^{-x^2} dx $$

The first element of the tuple is the estimation of the integral, the second element an estimation of the error.

Sometimes the function has a singularity.

$$\int_{-1}^{+1} \frac{1}{\sqrt{|x|}} dx$$

Excluding the point with singulaity do the job.

Integration of a function, given a sample

Using the trapezoidal rule (linear interpolation)

Using the Simpson's rule (quadratic interpolation)