Professional Documents
Culture Documents
Math5335 2017new4
Math5335 2017new4
• At the beginning of each file write your name and student number.
• Question 5 also requires you to define a function M-file q5g.m, in addition to q5.m.
• Questions 1, 2, 3, and 4 are each worth 10 marks, while Question 5 is worth 15 marks.
• Each script must require no interactive input, or depend on any variables defined
outside the script. To obtain the output for Question 1, you must be able to type
>> clear
>> q1
Do not include the clear statement in your scripts q1.m, q2.m etc.
• Save all files in your home folder. Do NOT create any sub-folders.
>> what
should list the MAT file q3val.mat. At the end you should also see your M-files
q1.m, q2.m, ...
1
Question 1 (10 marks): Answer in the file q1.m
Consider upper triangular n × n matrix
1 0 ... ... 0
2 1 0 . . . 0
. ..
. .
A = .. . . . . .
. ... ...
.. 0
n ... ... 2 1
(c) Find the SVD factorization of A and store it in the matrices S, D, V such that A =
SDV > . (Hint: help svd)
(d) Check that the singular value decomposition is correct by computing kA − SDV > k2 .
Store the result in Acheck.
(e) Define a rank 100 approximation of the matrix A (i.e., use only the singular values
σ1 , σ2 , . . . , σ100 ). Store it in the matrix Aapp.
(a) Set x0 to 3 and use it as starting value for the following Newton iteration.
(b) Modify Newton’s iteration (1) so that it can be used to approximate the inverse of
a function. Write a Matlab program which uses this modified Newton iteration (do
Not use build-in functions like fzero in Matlab to do that). Use 3 iterations and
store the result in x3.
(c) Run the Newton iteration till the difference between successive approximations is
smaller than the machine epsilon eps, i.e. |xn+1 − xn | < eps = 2−52 . Store the result
in xe.
(d) Use fprintf to print the values x3 and xe. Do not type in the value directly, but
use the print function to do that.
2
Question 3 (10 marks): Answer in the file q3.m
The file q3val.mat holds two arrays
(c) Find the linear least-squares fit to the data (tj , yj ), j = 1, . . . , n of the form
2
y = x1 ∗ e−x2 ∗t ∗ tx3 .
(Hint: Take the logarithm of the equation first, to make it linear in log(x1 ), x2 , x3 .)
Store the vector (x1 , x2 , x3 )> in the variable xls as a column vector.
(d) Calculate the cubic spline that interpolates the data and uses the (default) not-a-knot
end condition.
(e) On the same axes, plot the data, the least-squares fit and the cubic spline interpolant
for t ∈ [1, 2]. Make sure you include a legend.
(b) Use the Matlab build-in function integral to approximate the integral. Store the
result in II. (Hint: Use help integral to find out how this Matlab function works.)
(c) In the following we use the composite Simpson rule, which for a grid a = x0 < x1 <
x2 < . . . < xN = b with h = xi − xi−1 = 1/N for i = 1, 2, . . . , N , is defined by
h
QSimp
N (f ) = f (x0 ) + 4f (x1 ) + 2f (x2 ) + 4f (x3 ) + 2f (x4 ) + · · ·
3
+ 2f (xN −2 ) + 4f (xN −1 ) + f (xN )
i) Set NI = 1000.
ii) Define a vector xI of NI+1 equally spaced points such that the first point is 0
and the last point is 1.
iii) Use the composite Simpson rule to approximate the integral I. Store the value
in IS.
3
Question 5: Answer in the files q5g.m and q5.m
Let
g(x) = x2 + exp(x).
We consider the initial value problem
u00 (z) + exp(−z)u(z) =g(z), 0≤z≤1 (2)
u(0) =1
u0 (0) =1.
i) Set N to 103 .
ii) Let Un ≈ u(zn ), where zn = n/N for n = 0, 1, . . . , N .
We use the forward difference approximation
U1 − U0
u0 (z0 ) ≈
1/N
and the central difference approximation to approximate
Un+1 − 2Un + Un−1
u00 (zn ) ≈ .
1/N 2
4
iii) Solve the linear system to find the vector U.
iv) Set now Nlarge = 1e6.
v) Define an (Nlarge + 1) x (Nlarge + 1) matrix Blarge and a vector blarge
of length Nlarge+1 in Matlab such that the above linear system can be written
as Blarge * Ularge = blarge. (Hint: spdiags)
vi) Solve the linear system Blarge * Ularge = blarge.