# Homework 1 Solution

\$30.00

Category:

## Description

Please submit to CANVAS a .zip le 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 de ned as

n! = n(n 1)(n 2) 1; 0! = 1: (1)

Write a Matlab/Octave function compute factorial.m that takes an integer number as input and returns (1). The function should be of 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 de ned as

 x = v : (2) xk2 k k u n k=1 uX

t

Write a Matlab/Octave function compute Euclidean norm.m that computes the norm (2), for an arbitrary input vector x. The function should be of the following form

function [z] = compute Euclidean norm(x)

1

Input:

x: n-dimensional vector (either column vector 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/Octave documentation). You can compare the output of your function with the Matlab/Octave function norm(x).

Exercise 3 Write a Matlab/Octave program matrix times vector.m that computes the prod-uct between an n-dimensional square matrix A and an n-dimensional (column) vector x. The components of the (column) vector y = Ax are de ned

 n Xj yi = Aij xj i = 1; :::; n: (3) =1

The function should be of 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.

Hint: You can use two nested for loops to compute the vector y (one loop computes the sum

1. 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/Octave documentation). You can debug your function by comparing the output with the Matlab expression A*x, for simple matrices A and vectors x.

2

Exercise 4 The number can be de ned as a limit of various converging series of numbers. Among them

 n 16k 8k + 1 8k + 4 8k + 5 8k + 6 Simon Plou e (1995); (4) = n!1 k=0 X 1 4 2 1 1 lim u !1 k=1 k p u X = 6 v nlim n 12 Euler (1735): (5) t

Write a Matlab/Octave function pi series.m retuns the rst 10 partial sums of the series (4)-(5), i.e., the vectors P and E with components

 Pn+1 n 16k 8k + 1 8k + 4 8k + 5 8k + 6 n = 0; 1; 2; ::: (6) = k=0 X 1 4 2 1 1 v p n 1 En = 6 n = 1; 2; ::: (7) 2 u k=1 k uX t

The function should be of the following form

function [P,E,n1,n2]=pi series()

Output:

P, E: row vectors with 10 components de ned by the rst 10 partial sums in (6) and (7).

n1, n2: see the Extra Credit exercise hereafter. If you do not want to code the extra credit part, just set n1=0 and n2=0 in the function pi series.m.

Extra Credit: At the end of the Matlab function pi series.m, write a code that returns the smallest integer numbers n1 and n2 such that

jPn1+1 j < 10 5 jEn2 j < 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 !!