Description
Please submit to CANVAS a .zip file that includes the following Matlab functions:
compute factorial.m
compute Euclidean norm.m
matrix times vector.m
pi series.m
EXERCISE 1 The factorial of a natural number is defined as
n! = n(n − 1)(n − 2) · · · 1,0! = 1. | (1) |
Write a Matlab function compute factorial.m that takes an integer number as input and returns (1). The function should be in the following form
function [b] = compute factorial(n)
Input:
n: natural number (possibly including 0)
Output:
b: factorial of n
EXERCISE 2 The Euclidean norm of an n-dimensional vector is defined as
X | = | v | n | x^{2} . | (2) |
k k | u | k |
X
u
t
k=1
Write a Matlab function compute Euclidean norm.m that computes the norm (2), for an arbitrary input vector X. The function should be in the following form
function [z] = compute Euclidean norm(x)
Input:
x: n-dimensional vector (either column or row vector)
Output:
z: norm of the vector
Hint: You can use the for loop. The number of components of the input vector can be determined by using the Matlab command length(x) (see the Matlab documentation). You can compare the output of your function with the Matlab command norm(x).
EXERCISE 3 Write a Matlab program matrix times vector.m that computes the product between an n-dimensional square matrix A and an n-dimensional (column) vector X. The components of the (column) vector Y = AX are defined
n | |
X | |
y_{i} =A_{ij} x_{j}i = 1, …, n. | (3) |
j=1 |
The Matlab function should be in the following form
function [y] = matrix times vector(A,x)
Input:
A: n × n matrix
x: n × 1 vector
Output:
y: n × 1 vector
You are not allowed to use the Matlab expression A*x within your function.
Hints: You can use two nested for loops to compute the vector Y (one loop computes the sum (3) while the other one controls the index i in (3))). The size of the matrix A can be determined by using the Matlab command size(A) (see the Matlab documentation). You can debug your function by comparing the output with the Matlab expression A*x, for simple matrices A and vectors x.
EXERCISE 4 The number π can be defined as a limit of various converging series of numbers. Among them
n | 8k + 1 | ^{−} 8k + 4 ^{−} 8k + 5 | ^{−} 8k + 6 | Simon Plouﬀe (1995) | (4) | |||||||||||||||
^{=} n→∞16^{k} | ||||||||||||||||||||
X | 1 | 4 | 2 | 1 | 1 | |||||||||||||||
π lim | , | |||||||||||||||||||
k=0 | ||||||||||||||||||||
√ | u | |||||||||||||||||||
n→∞ | k^{2} | ^{ } | ||||||||||||||||||
u | X | |||||||||||||||||||
π = | 6 | v | lim | n _{1} | Euler (1735). | (5) | ||||||||||||||
t |
k=1
Write a Matlab function pi series.m that plots in the same figure the first 10 partial sums of the series (4)-(5), that is the vectors P(1:10) and E(1:10) with components
t
n | 1 | 4 | 2 | 1 | 1 | |||||||||||||||||
^{P}n+1 = | − | − | − | n = 0, 1, 2, … | (6) | |||||||||||||||||
16^{k} | 8k + 1 | 8k + 4 | 8k + 5 | 8k + 6 | ||||||||||||||||||
k=0 | ||||||||||||||||||||||
X | ||||||||||||||||||||||
√ | v | n | 1 | |||||||||||||||||||
E_{n} = | 6 | n = 1, 2, … | (7) | |||||||||||||||||||
k^{2} | ||||||||||||||||||||||
u |
k=1
The function should be in the following form
function pi series()
Output:
One figure that includes the plots of P(1:10) and E(1:10)
Hints: Once you have constructed the vectors P and E with as many components as you like, you can plot them in the same figure by using the sequence of Matlab commands figure(1), clf, plot(P(1:10),’r.’) (plots (6) with red dots), hold, plot(E(1:10),’b.’) (plots (7) with blue dots) (see the Matlab documentation for further details). Which sequence between (6) and (7) converges faster to π?
EXTRA CREDIT: At the end of the Matlab function pi series.m, write a code that returns in the command window the smallest integer numbers n_{1} and n_{2} such that
|P_{n}_{1} _{+1} − π| < 10^{−5}|E_{n}_{2} − π| < 10^{−5}. | (8) |
To determine n_{1} and n_{2}, you can use a for loop combined with an if statement or, equivalently, a while loop.
3