Forward Error, Backward Error, Condition Number and Numerical Linear Algebra
In this lab you will be exploring forward and backward error and condition number. You will also be exploring numerical linear algebra commands for using matrices in Matlab.
First we suggest that you make sure you do the exercises
1.3 /1ab, 3ab Computer Problems / 1, 5
Copyright By PowCoder代写 加微信 powcoder
To define a matrix in Matlab we use square brackets and inside the brackets we place each element of a row separated by commas or spaces (commas recommended) and each row separated by a semi-colon. For example, the matrix:
can be defined in Matlab using
A=12 0 −7 (1)
0 4 4 0 −3 −3
A = [9, 0, 26; 12, 0, -7; 0, 4, 4; 0, -3, -3];
To pick out the element in the i-th row and j-th column we use normal parentheses (curved brackets) and the indices separated by a comma. For example
should give you back the element in the 2nd row and 3rd column, or -7 in this case (try it). You can also pull out parts of a matrix by using a sequence of numbers. For example,
extracts the second column, rows two through 4 (try it). If you give the colon by itself, you will extract the entire row or column, for example
extracts the entire 2nd row (try it). For more examples and a video demonstration try looking at:
https://www.mathworks.com/videos/row-and-column-indexing-97492.html
Some other useful commands are ways to find out the size of a matrix or vector. The Matlab command
[n, m] = size(A)
gives us the row and column dimensions of the matrix A. In Matlab, all objects are matrices, even if they print like scalars or vectors. Thus in Matlab π is pi=3.14159… , but even so, size(pi)=[1,1], showing that it is stored as a 1 Ö 1 matrix. Similarly, a row vector is defined by [1,2,3,4] and has size [1, 4], while a column vector is defined by [1; 2; 3; 4] and has size [4, 1].
Matrices can also be constructed by putting blocks together (partitioned matrices). Given
u=[1,2,3,4]; v=[5,6,7,8]; w=[0,-1,-2,-3];
see what happens when you enter
A1=[u,v,w]; A2=[u;v;w]; A3=[u’, v’, w’];
A very important, command is norm. In this lab, you will be comparing very large vectors and matrices. If you have two vectors with 7000 entries, you do not, do not, and again do not print all 7000 entries to compare them. You take the norm.
The Hilbert MONSTER: Execute the commands for Text’s Example 2.12 concerning n × n Hilbert matrices H for n = 6 and 10. Use Matlab’s most powerful solver to solve Hx = b where b = H[1, …, 1]T which has the obvious exact solution x = [1, …, 1]T . You should also find the relative forward error and relative backward error and compare their ratio to the condition number.
Generate the 6 × 6 and find its condition number in the ∞ norm: n=6; H = hilb(n)
cond(H,inf)
The condition number is huge (approximately of order 10p and indicates approximately that p digits will be lost in the approximate solution. What is p for this value of n? Now check the approximate solution, to see how many digits were lost in the computation:
b=H*ones(n,1);
xa=H $\texttt{\textbackslash}$ b
Note that H\b is Matlab’s quirky way of writing H−1 b, that is \ represents a (backwards) division. Check the relative forward error:
norm(xa – ones(n,1), Inf)/norm(ones(n,1), Inf)
and also compute the relative backward error. Now repeat these commands for n = 10. How big does n have to be for the to destroy all digits of accuracy even the for the best solvers?
Translate the following equations into matrix form, Ax = b:
2×1 −x2 +3×3 +4×4 = 9
x1−2×3+7×4 = 11 (2)
3×1 −3×2 +x3 +5×4 = 8 2×1 +x2 +4×3 +4×4 = 10
Find the PA=LU decomposition of A using Matlab’s lu command
[L, U, P] = lu(A)
Also try using the backslash operator, by finding the inverse of the matrix with the inv function. You can build bigger matrices by using the random matrix command
You might try also problems from the AssignedProblems list: 2.3 /1ab, 3a, 5ae, 7 Computer Problems / 1ab, 3, 5
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com