# HOMEWORK 1 solution

\$30.00

Category:

## 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 x2 . (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 yi =Aij xji = 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→∞16k X 1 4 2 1 1 π    lim , k=0 √ u n→∞ k2 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 Pn+1 = − − − n = 0, 1, 2, … (6) 16k 8k + 1 8k + 4 8k + 5 8k + 6 k=0 X √ v n 1 En = 6 n = 1, 2, … (7) k2 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 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.

3

error: Content is protected !!