Sie sind auf Seite 1von 6

Homework Assignment #2

Student: Vinicius Fontes


ASU # ID: 1208318367

1. The answers of this homework will be compiled in only one code, but each part of the question
will be commented separately.

(a) The 8x8 random matrix can be generated with the rand(8,8) command. The condition number
we need to find is calculated in MATLAB as cond(R,2) or just cond(R).
The part of the code that represent this information is as follows.
format long e
%Creating a random matrix (R) and finding its condition number (cond_R).
R = rand (8,8);
cond_R = cond (R,2);

Notice that, as this is the first part of the code, the statement format long e is written in the
beginning as asked in the question. The condition number is 39.156.
(b) In the next part of the code is created a Hilbert matrix, which is, accordingly to MATLAB, a famous
example of a badly conditioned matrix. The condition number for this matrix is also calculated.
The part of the code that represent this information is as follows.
%Creating the Hilbert matrix (H) and finding its condition number (cond_R).
H = hilb (8);
cond_H = cond (H,2);

The condition number of the Hilbert matrix is 1.528e10.


(c) The following section of the code estimate the error of the solution of the equation
= .
First, a random vector matrix is created the same way the random matrix was.
Second, the solution ( ) for the equation is found in the following way.
=
= 1
= 1
= 1
1

To do so, the inverse of is needed, which can be found with the command inv(R).

Using the result found, the residual for this example can be found. To do so, is needed, which
can be found multiplying the random matrix by the result found. The residual is the diference
between and .
With the inverse of the random matrix and the residual, the error
can be found multiplying
them both.
The part of the code that represent this information is as follows.
%Defining a random vector b (8x1).
b = rand (8,1);
%Finding the inverse of R (Rinv).
Rinv = inv (R);
%Calculating the solution x_R multiplying the inverse of R by b.
x_R = Rinv * b;
%Defining btilt_R to calculate the residual (residual_R) and
%estimating its error (error_x_R).
btilt_R = R * x_R;
residual_R = b - btilt_R;
error_x_R = Rinv * residual_R;

As the result of this question is a matrix, it is attached in the annex page in the end of this
document.
(d) To find the solution of the equation
= , the same operations are done, except defining the
random vector, as the same will be used.
The part of the code that represent this information is as follows.
%Now, calculating the same steps for the Hilbert matrix.
Hinv = inv (H);
x_H = Hinv * b;
btilt_H = H * x_H;
residual_H = b - btilt_H;
error_x_H = Hinv * residual_H;

(e) The condition number shows how well-conditioned a matrix is. An ill conditioned matrix, such
as Hilberts, is likely to have a big condition number. Such matrix needs a great precision to find a
solution with a small error. To estimate how many digits of accuracy a solution has, the following
equation can be used:
| |
||
= ()
| |
||
||
Where is a given matrix and is the solution to the equation = . The term
can be
||

calculated with the expression 2 , where is the number of digits in the mantissa. For single
precision, which was used in the code, is 23.

Applying the equation above for the solution of equation the result is 4.67e-6 and for the
result is 1.82e3. This shows the influence of the condition number in the accuracy of solution for
each case, as the Hilbert matrix that has a much greater condition number also has more digits in
the solution that cannot be trusted.
Therefore, for the first solution (
), the first 5 digits after the decimal point are accurate by this
method, because the expected error is on the sixth digit, while for the second solution (
) the
third digit from left to right after the decimal point is already not accurate. For instance, the first
value of the solution
which is 3.2258e5 has only 2 digits of accuracy, as the expected error
by the method above is on the third digit.

Attachments
Full code:
format long e
%Creating a random matrix (R) and finding its condition number (cond_R).
R = rand (8,8);
cond_R = cond (R,2);
%Creating the Hilbert matrix (H) and finding its condition number (cond_R).
H = hilb (8);
cond_H = cond (H,2);
%Defining a random vector b (8x1).
b = rand (8,1);
%Finding the inverse of R (Rinv).
Rinv = inv (R);
%Calculating the solution x_R multiplying the inverse of R by b.
x_R = Rinv * b;
%Defining btilt_R to calculate the residual (residual_R) and
%estimating its error (error_x_R).
btilt_R = R * x_R;
residual_R = b - btilt_R;
error_x_R = Rinv * residual_R;
%Now, calculating the same steps for the Hilbert matrix.
Hinv = inv (H);
x_H = Hinv * b;
btilt_H = H * x_H;
residual_H = b - btilt_H;
error_x_H = Hinv * residual_H;

Attachments
As only the some information is needed for the analysis, some variables are not present in this
attachment.

Matrix R ()

Solution of R (
)

Error of R (
)

Attachments

Matrix H ()

Result of H (
)

Error of H (
)

Das könnte Ihnen auch gefallen