# Numerical Methods II

很多很好玩的数值方法

是 Matlab 苦手，所以很多高血压代码

# Machine Numbers

## 机器数转十进制

Our first M-file computes the value of a machine number.

Let us choose fl1 as name of function and of course as name of the file also.

We give a vector as input parameter. The last coordinate of vector gives the

characteristic of machine number (in tenary numeral system). We store the signed

mantissa in the other coordinates.

The output argument be the real number what is represented by our machine number.

- The first bit of mantissa we can use for storing the sign of the number.

(Originally this bit is surely 1.) When the number is positive, then the signbit be 0,

in case of negative numbers we use 1 as first bit. - We don’t have to know the parameters of machine number set for converting

the number. The length of mantissa can be read from input data. And we

assume that the bounds of characteristic are such that our carachteristic be allowed. - Before starting computation let us check whether the given data can be a

machine number or not. (All but last coordinates are from set {0,1} and last is an integer.)

1 | % 1. 机器数转十进制 |

## 在数轴上展示机器数

Let us write anothe M-file, called fl2. It displays the elements of a given machine

number set on the real axis. Compute the number of elements and the following

parameters: M∞, ε0, ε1. (They will be the output arguments of the function)

The function waits 3 (integer) number as input.

They are the parameters of the set: t, k1, k2.

- Let us check whether the input parameters are appropriate.

(t ∈ N+ and k1, k2 ∈ Z, and of course k1 < k2) - For the computation we can call our first function (fl1)
- The set is symmetric. We can use this property for the faster computation.

1 | % 1. 在数轴上展示机器数，输出最大最小值，和与1的偏差 |

## 十进制转机器数

The third function finds the machine number what representates the given real number.

The name be fl3. The function waits the real number and parameters of set of

machine numbers (t, k1, k2.) as input. And gives back a vector with t+1 coordinates.

The last coordinate be the characteristic and the firs t stores the signed

mantissa. (As in case of input argument of function fl1).

- The first bit of mantissa is the sign-bit as in exercise 1.

The value of sign is 0 when number is positive and 1 when it is negative. - Check the input arguments whether they are appropriate.

And the real number whether can representate in machine number set. (ε0 ≤ |r| ≤ M∞)

1 | % 1. 十进制转机器数 |

## 机器数相加

Let us write a function for addition between machine numbers.

Let us call the file to fl4. It waits for two vectors as input. (They are representate

the machine numbers as before). The output be a vector with the machine number of the sum.

- Use machine-addition. The double conversion is not acceptable. (To compute

the real numbers belongs to inputs, summing them and reconverting to

machin number is not allowed.) - Check the inputs. (They have to have same length and have to be machine numbers)
- If one of the numbers is negative (the first bit is 1) then in real the operation

is a substraction.

1 | % 1. 机器数相加 |

# Gaussian Elimination

## 计算高斯消去

Write an M-file to compute Gaussian Elimination.

The name of the file be gaussel1

- Input parameters: the coefficient matrix (A) and the right-side vector (b) of LES.
- Output argument: the solution vector x
- Use the Matlab row-operations for organisation of algorithm.
- If GE can’t be solved without row or coloumn swap write an error message

and terminate the program. - In case of underdetermined LES give a base solution and warn the user of this.
- In case the user asked it, display the matrices A(i) during computation.
- To checking our function we can use the exercises from numerical I.

+1 We can prepare our function to accept LES with multiple right sides.

1 | % 2. 计算高斯消去 |

## Whole Pivoting

Extend the previous m-file (but save as a new name for example gaussel2) with

steps of partial and whole pivoting method.

- Using partial or whole pivoting could be choosen by user

(for example according to a boolean input parameter), but if the partial pivoting is stucked

then automatically switch to whole pivoting method. If we used whole pivoting despite user has choosen partial pivoting, then inform user about the switch - Give an opportunity for displaying matrices A(i) during computation. Don’t

forget that pivoting method can be changed the matrix so we have to display

it after row and coloumn swap. - Don’t forget that the pivoting method can be changed the solution.

1 | % 2. 完全交换，注释看前面一个 |

## 求逆矩阵

Apply Gaussian elimantion for computing inverse of an square matrix.

The name of function: gaussel3

- Check input argument(s) before computing
- Compute the determinant of matrix.

If you have written the function gaussel1 such that it accepts multiple

right-sides, then you can call it during computation.

1 | % 2. 求逆矩阵 |

# QR-decomposition

## Gram-Schmidt

Write an M-file for QR-decomposition using Gram-Schmidt orthogonalization. Let

us call the function to: gramschmidt

- Input parameter: a square matrix (A)
- Output arguments: an orthogonal matrix (Q) and an upper triangular matrix (R),

such that satisfy A = Q·R - To check existence of decomposition (the columns of A have to be linear

independent) we can use any included function of Matlab. - The included functions can be used for computing norms,

but we can compute via definition also.

1 | % 3. Gram-Schmidt正交法 |

## Householder

Write an M-file to give the matrix of a Householder transformation, from a known

point and its image. The name of function let be: householder

- Input parameters: the coordinatas of the point and its image (P, P’) Point

P (and ofcourse P’ also) can be from Rn where n is not predetermined. - Output argument: the matrix of Householder-transformation
- Take care of choosing sign during transformation (the parameter σ effects

the stability of the method)

1 | % 3. Householder 变换主要就是那个公式，没有什么别的 |

## 点映射

The third function will asking data via graphical input. (It works for 2D points)

Display points and the hyperspace of reflection. Ask for another point (also via

graphical input) and apply the transformation to the new point. The function

householder can be called during the algorithm.

the name of function: hhgraph

1 | % 3. 交互点映射 |

## Householder QR 分解

Write an M-filet to realize QR-decomposition with Householder algorithm. Let

us call our function to: hhalg

- Input parameter: a square matrix (A)
- Output arguments: an orthogonal matrix (Q) and an upper triangular matrix (R),

such that satisfy A = Q·R - The previous functions can be called.

1 | % Householder QR 分解 |

# Iterative solutions of LES

## Jacobi 迭代

Write an M-file for Jacobi iteration. The file name be: jacobi

- Input parameters: The matrix of LES A and a vector for the right-side: b
- Output argument: the approximation of the solution vector: x
- We can use the vectorial form of the iteration

1 | % 4. Jacobi迭代 |

## Gauss Seidel

Write an M-file for Gauss-Seidel iteration. The file name be: gaussseid

- As in previously at Jacobi iteration.

1 | % 4. 用 Gauss Seidel 迭代法解线性方程组 Ax=b |

# Iterative solution of non-linear equations, Interpolation

## 二分法

Write an m-file for bisection method and call the file: bisect

- Input arguments: the function f (we want to find one of the zeros). Give it

as a string (the variable can be denoted by x or we can give the notation

as another parameter. We will need the ends of the starting interval (a, b),

the number of steps (n). - Output arguments: the appropriate approximation of root: x^∗

and the error estimation ε. - Before start we have to check the interval (is there a root inside?)
- To evaluate function we can use function eval

1 | % 5. 二分法 |

# Least Squares Method, Generalised inverse

## 最小二乘法

Write an m-file for approximation with least squares method.

The name of file: lsmapprox

- Input arguments: order of polynomial (n), nodes of approximation (in a

vector), vector of function values in nodes - Output argument(s): the coefficients of the polynomial
- Let us draw a picture to illustrate the approximation. (We can use the

included function polyval)

1 | % 6. 最小二乘法计算拟合多项式系数 |

## 广义逆矩阵

Write an m-file to find the generalised inverse of a given matrix.

The name of file: geninv

- Input argument: the matrix (A).
- Output argument: The generalised inverse (A+)
- Use the rank factorisation if the matrix is not fullranked. For the matrix

operations we can use the included functions of Matlab (eg.: rank, inv,

instead of solving LES we can use the command G=F\A, etc.)

1 | % 6. 求矩阵的广义逆矩阵 |

Numerical Methods II