Please submit to CANVAS a .zip file that includes the following Matlab functions:
compute Euclidean norm.m
matrix times vector.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)
n: natural number (possibly including 0)
b: factorial of n
EXERCISE 2 The Euclidean norm of an n-dimensional vector is defined as
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)
x: n-dimensional vector (either column or row vector)
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
|yi =Aij xji = 1, …, n.||(3)|
The Matlab function should be in the following form
function [y] = matrix times vector(A,x)
A: n × n matrix
x: n × 1 vector
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)|
|π =||6||v||lim||n 1||Euler (1735).||(5)|
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
|Pn+1 =||−||−||−||n = 0, 1, 2, …||(6)|
|16k||8k + 1||8k + 4||8k + 5||8k + 6|
|En =||6||n = 1, 2, …||(7)|
The function should be in the following form
function pi series()
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 n1 and n2 such that
||Pn1 +1 − π| < 10−5|En2 − π| < 10−5.||(8)|
To determine n1 and n2, you can use a for loop combined with an if statement or, equivalently, a while loop.