week 8 report

14
MA124 Maths By Computer Week 8 Assignment Thomas Dove 1403349 Kyle Byrne 1407493 March 2, 2015 Solution 8A :Root Finding Considering the function f (x)=2xcos(x) - sin(x) ,we want to investigate 3 different methods for approximating roots of the function. By first plotting the function we can get some idea about the approximate location of the functions zero’s. The one which we are interested in approximating is the smallest strictly positive one, circled in the figure below. Figure 1: Plot of f (x) with the root we are interested in circled. 1

Upload: kyle-byrne

Post on 22-Dec-2015

228 views

Category:

Documents


1 download

DESCRIPTION

meh

TRANSCRIPT

Page 1: Week 8 Report

MA124 Maths By Computer

Week 8 Assignment

Thomas Dove 1403349Kyle Byrne 1407493

March 2, 2015

Solution 8A :Root Finding

Considering the function f(x) = 2xcos(x) − sin(x) ,we want to investigate 3different methods for approximating roots of the function. By first plotting thefunction we can get some idea about the approximate location of the functionszero’s. The one which we are interested in approximating is the smallest strictlypositive one, circled in the figure below.

Figure 1: Plot of f(x) with the root we are interested in circled.

1

Page 2: Week 8 Report

Solution 8A(a) : Bisection & Newton-Raphson

The bisection method works by taking some initial interval [a, b] and throughsuccessive iterations shrinking the interval, trapping the root inside. The firstchoices of a and b are made so that f(a) and f(b) have different signs. Thisdecision guarantees that the root will be in the interval by the intermediatevalue theorem. The point x = (a + b)/2 is then considered, if the function atthis point has the same sign as a then the interval [a, b] is replaced by [x, b] andif it has the same sign as b then the interval is replaced by [a.x] this procedureis repeated until the length of the interval is down to the desired tolerance.In the case of our function it is clear from the function plot shown above thatf(x) has different signs at x = 1 and x = 2 , so we took these values to be our aand b respectively to give us our starting interval [1, 2]. The tolerance we wereworking to is 10−12 so we would have to perform enough iterations such that|a′ − b′| < 10−12 where a′ and b′ is some altered value of a and b. Carrying outthese iterations in Matlab gave the output shown below.

Figure 2: Matlab output of using the bisection method to approximate a rootof f(x)

The Newton-Raphson method is another way of approximating roots, againby iteration. This method is derived using a Taylor appoximation to reducethe error in an estimate. It involves taking tangents to a function at someinitial approximation of a root to find a better approximation. Each successiveapproximation is found according to the formula :

xn+1 = xn −f(xn)

f ′(xn)(1)

By using Newton-Raphson we can find an approximation to our root of f(x)within 10−12 obtaining the following Matlab output:

2

Page 3: Week 8 Report

Figure 3: Matlab output of using the Newton-Raphson method to approximatea root of f(x)

Solution 8A(b) : Iterations

If we consider the effect of each iteration in the bisection method we can seethat the size of the interval is halved each time, so that is after N trials the sizeof the interval will be;

b′ − a′ =b− a2N

(2)

Using this fact we can calculate the number of iterations necessary to reach ourrequired tolerance so for some general tolerance 10−M we can see that it willtake N iterations where :

b− a2N

≤ 10−M

2N

b− a≥ 10M

2N ≥ 10M (b− a)

N ≥ log(10M (b− a)

log(2)

Where log(x) is the natural logarithm of x. So from this we can see it will

take at least log(10M (b−a)log(2) iterations to have an approximation of a root to 10−M

accuracy. When carrying out these computations it is much more favourable touse a method with fewer iterations necessary , for example the Newton-Raphsonmethod. Taking the error at the nth iteration to be en = xn + r in the Newton-Raphson scheme, where r is the actual value of the root. We can clearly seethat the errors satisfy the difference equation ;

xn+1 = xn −f(xn)

f ′(xn)

en+1 + r = en + r − f(xn)

f ′(xn)

en+1 =enf

′(xn)− f(xn)

f ′(xn)

3

Page 4: Week 8 Report

Then by Taylor’s theorem ;

f(r) = f(xn) + (r − xn)f ′(xn) +1

2(r − xn)2f ′′(ξ) (3)

for some ξ between r and xn. So as f(r) = 0 we have and r − xn = −en ;

0 = f(xn) + (r − xn)f ′(xn) +1

2(r − xn)2f ′′(ξ)

0 = f(xn)− enf ′(xn) +1

2en

2f ′′(ξ)

enf′(xn)− f(xn) =

1

2en

2f ′′(ξ)

en+1 =en

2f ′′(ξ)

2f ′(xn)

So letting Cn =f ′′(ξ)

2f ′(xn)

We get that;en+1 = Cnen

2 (4)

Solution 8A(c) : Secant Method

The secant method is another alternative root finding scheme. It is similar toNewton-Raphson in that it works using tangents, but unlike Newton-Raphsonthere is no need to calculate the derivative. This uses the following formula tocalculate each approximation;

xn+1 = xn −f(xn)

tnwhere tn =

f(xn)− f(xn−1)

xn − xn−1(5)

As we did with the Bisection and Newton-Raphon methods we can use Matlabto carry out the necessary computation to calculate our root of f(x) to within10−12. This method requires to initial values as with the bisection method soagain we used x0 = 2 and x1 = 1. Then we obtained the following output;

4

Page 5: Week 8 Report

Figure 4: Matlab output of using the Secant method to approximate a root off(x)

This output was obtained via the following m file secant.m;

%Uses the Secant method to approximate roots of f(x)= 2xcos(x)-sin(x)clearx(1)=2;x(2) = 1; % Input initial guesstol = 10ˆ(-12); % Sets tolerance.

xdiff = inf; % initializes difference between successive estimatesk=2; % initializes counteri=0;while xdiff > tol

t = (f(x(k))-f(x(k-1)))/(x(k)-x(k-1));x(k+1) = x(k) - (f(x(k))/t);xdiff = abs(x(k+1)-x(k));k=k+1 ;i = i+1;

end

x' % Print out column vector of approximations

fprintf('The Secant method gives the root as %f \n',x(length(x)))fprintf('In %.0f steps \n ',i);

Solution 8B : Magic Squares

Solution 8B(a)

Consider a 3 × 3 matrix A with entries aij . For A to be a magic square, thismeans that the rows, columns and main diagonals of this matrix sum to thesame number, say r. Defining A as the following:

A =

a11 a12 a13a21 a22 a23a31 a32 a33

(6)

5

Page 6: Week 8 Report

So for A to be a magic square, its entries must satisfy the following equations;

a11 + a12 + a13 = r; a21 + a22 + a23 = r; a31 + a32 + a33 = r;

(Sum of Rows)

a11 + a21 + a31 = r; a12 + a22 + a32 = r; a13 + a23 + a33 = r;

(Sum of Columns)

a11 + a22 + a33 = r; a31 + a22 + a13 = r;

(Sum of Main Diagonals)

(7)

The system of linear equations:

Mv =

−1 1 1 1 0 0 0 0 0 0−1 0 0 0 1 1 1 0 0 0−1 0 0 0 0 0 0 1 1 1−1 1 0 0 1 0 0 1 0 0−1 0 1 0 0 1 0 0 1 0−1 0 0 1 0 0 1 0 0 1−1 1 0 0 0 1 0 0 0 1−1 0 0 1 0 1 0 1 0 0

ra11a12a13a21a22a23a31a32a33

= 0 (8)

can be shown to be equal to the equations satisfied by the magic square matrixA. This can be done by using matrix multiplication of M and v to gain 8 linearequations as follows:

− r + a11 + a12 + a13 = 0; −r + a21 + a22 + a23 = 0; −r + a31 + a32 + a33 = 0;

− r + a11 + a21 + a31 = 0; −r + a12 + a22 + a32 = 0; −r + a13 + a23 + a33 = 0;

− r + a11 + a22 + a33 = 0; −r + a31 + a22 + a13 = 0;

It is clear to see this set of linear equations of is equivalent to that of (7).Therefore the set of solutions the equation (8) is equal to the possible 3 × 3magic square’s entries with corresponding row sum.

Solution 8B(b)

Using the command rref in Matlab, we are able to reduce the matrix M to rowreduced echelon form.By inputting the following into the command line of Matlab, we get the outputas follows:

6

Page 7: Week 8 Report

Figure 5: Using the rref command to find the row reduced form of M

And hence the row reduced echelon form of M is:

1 0 0 0 0 0 0 −1 −1 −1

0 1 0 0 0 0 0 −2/3 −2/3 1/3

0 0 1 0 0 0 0 −2/3 1/3 −2/3

0 0 0 1 0 0 0 1/3 −2/3 −2/3

0 0 0 0 1 0 0 2/3 −1/3 −4/3

0 0 0 0 0 1 0 −1/3 −1/3 −1/3

0 0 0 0 0 0 1 −4/3 −1/3 2/3

0 0 0 0 0 0 0 0 0 0

(9)

From the row reduced form of M above we can see that the rank of M is equalto 7, as that is the number of non-zero rows of M in its row reduced form.Therefore as the rank of a matrix is also equal to its column rank, this tells usthat the matrix has 7 linearly independent columns (The 7 left most columns)and these are the pivot columns.So as our system of linear equations has 10 variables but only 7 pivot columnsthis means that there are 3 free variables. As this is so, this means the basicvariables , r, a11, a12, a13, a21, a22, a23, corresponding to the pivot columns,depend on the free variables a31, a32 and a33, which can take any real number.So by choosing values for the free variables a31, a32 and a33, this will then de-termine the values for the rest of the variables, which for any choice of the threevariables is the only solution to (8), as each basic variable depends on the allthree of the free variables as follows:

7

Page 8: Week 8 Report

r − a31 − a32 − a33 = 0;

a11 −2

3a31 −

2

3a32 +

1

3a33 = 0;

a12 −2

3a31 +

1

3a32 −

2

3a33 = 0;

a13 +1

3a31 −

2

3a32 −

2

3a33 = 0;

a21 +2

3a31 −

1

3a32 −

4

3a33 = 0;

a22 −1

3a31 −

1

3a32 −

1

3a33 = 0;

a23 −4

3a31 −

1

3a32 +

2

3a33 = 0;

From here, we want to show that the set of 3× 3 magic squares , S3 , (the setof all 3 × 3 matrices, of form A, which satisfy (8) ) forms a vector space. Forthis it must be shown this set satisfies all properties of a vector space.

� Addition of the magic square matrices satisfies A1, A2 , A3 andA4. As matrix addition for any two matrices in R3×3 are associative andcommutative, so A1 and A4 hold.To show A2 holds, consider the zero element for addition of R3×3 whichis: 0 0 0

0 0 00 0 0

It is clear to see this is a magic square as all rows, columns and diagonalssum to zero, meaning the system of linear equations (8) is satisfied andhence the zero element of addition is also an element of S3. So A2 holds.Finally, for a given element A in S3, there exists a matrix (−A) ε R3×3

such that:

A+ (−A) =

a11 a12 a13a21 a22 a23a31 a32 a33

+

−a11 −a12 −a13−a21 −a22 −a23−a31 −a32 −a33

=

0 0 00 0 00 0 0

By considering (-A), we can see that the sum of rows, columns and leadingdiagonals of (-A) are all equal to −(a11 + a12 + a13) = −r, where r is therow sum of (A). Hence (-A) is also a magic matrix and A3 holds.

� For any two 3× 3 magic squares, u and v , that: α · (u + v) =α · u + α · v holds for all α ε R.This holds by the distributive property of matrices when multiplied byscalars. That is:

p · (X + Y) = p ·X + p ·Y(p+ q) ·A = p ·A + q ·A

8

Page 9: Week 8 Report

where X,Y are m× n matrices and p, q are scalars.So as the set S3 contains only elements which are 3× 3 matrices with realelements, then S3 is a subset of R3×3. And hence as every element to S3

, is a matrix, this distributive property holds.

� For any 3× 3 magic square, v , that: (α+β) ·v = α ·v +β ·v holdsfor all α, β ε R.This property is also true by the previous argument.

� For any 3× 3 magic square, v , that: (αβ) ·v = α · (β ·v) holds forall α, β ε R.This holds by the associative property of matrices by scalar multiplication.That is:

p · (q ·X) = pq · (X)

where X is a m×n matrix and p, q are scalars. Hence as S3 is a subset ofR3×3, then the associative property also holds for all elements of S3.

� For any 3× 3 magic square, v , that: 1 · v = vThis holds by the identity property of matrices by scalar multiplication.That is:

1 ·X = X

where X is a m×n matrix. And so by a similar arguement for the previousproperties, this also holds for the set S3.

And so as the above properties hold this means that the set S3 is a vector spaceof the field R.

Note that the number of free variables in the system also lets us know thedeterminant of the vector space of 3 × 3 magic squares, and that the determi-nant is equal to the number of these free variables. This is because all othervariables in the magic squares are determined by the choice of these free vari-ables, and as the free variables can take any real number, the vector space mustcontain all magic squares where a31, a32 and a33 are real numbers. So our basismust span all possible values of the free variables, and a way to ensure this isby considering a basis of:

� The magic square with free variables, a31 = 1, a32 = 0 and a33 = 0

� The magic square with free variables, a31 = 0, a32 = 1 and a33 = 0

� The magic square with free variables, a31 = 0, a32 = 0 and a33 = 1

So any linear combination of these magic squares allows for a magic squarewith any value of the free variables to be made, and as for each choice of free

9

Page 10: Week 8 Report

variables there is only one magic square corresponding to it, if the set spans allpossible values of the magic square with free variables, a31, a32 and a33 it spansall possible magic squares. It is also clear to see that the three magic squares inthe basis are linearly independent as no linear combination of two of the magicsquares free elements can create that of the third. Therefore the dimension ofthe vector space is 3.

Solution 8B(c)

Given the matrices below, we want to show that they form a basis. This can beachieved by considering linear independence between the three matrices. It isalso key to note that each of the matrices are magic matrices as all satisfy thesystem of linear equations in (7).

M1 =

1 1 11 1 11 1 1

with r = 3

M2 =

0 1 −1−1 0 11 −1 0

with r = 0

M3 =

1 −1 0−1 0 10 1 −1

with r = 0

We can show that these matrices are linearly independent as follows.By considering M2 and M3, it is clear to see that M3 is not a linear combinationof M2 or vice versa. This is because no scalar multiple of M2 can equal M3,this is even show by considering M2’s top left entry, a zero, which no multipleof that can be made to equal 1 in the top left entry of M3. Hence M2 and M3

are linearly independent.Also M1 is not a linear combination of M2 and M3, as by considering there rowsums, the row sum of M2 and M3 is zero, and the row sum of M1 is 3. As it istrue that through addition of two magic matrices to form a new magic matrix,the total of the row sums of the two magic matrices are equal to the row sumof the resultant matrix (Proof below). Then any linear combination of any twomatrices, say M2 and M3, with row sum zero cannot create a matrix of row sumother than zero. Hence M1 is linear independent from M2 and M3.

Therefore as the 3 matrices are linearly independent and are all elements ofthe vector space of 3 × 3 magic matrices, it is so that as the vector space hasdimension 3, any set of 3 linear independent 3× 3 magic matrices form a basisto the vector space.

10

Page 11: Week 8 Report

Proof that summing two magic matrices also sums their row sum

Define:

A =

a11 a12 a13a21 a22 a23a31 a32 a33

B =

b11 b12 b13b21 b22 b23b31 b32 b33

to be to be two magic matrices of row sum, rA = α and rB = β respectively.Then:

A+B =

(a11 + b11) (a12 + b12) (a13 + b13)(a21 + b21) (a22 + b22) (a23 + b23)(a31 + b31) (a32 + b32) (a33 + b33)

So the hence the row sum rA+B equals:

rA+B = (a11 + b11) + (a12 + b12) + (a13 + b13)

= (a11 + a12 + a13) + (b11 + b12 + b13)

= rA + rB �

Solution 8B(d) 4× 4 magic squares

Now by considering a 4× 4 matrix A with entries aij , we can define A to be amagic square by the same definition as for the 3× 3 case, which states that therows, columns and main diagonals of this matrix sum to the same number, sayr. Define A as the following:

A =

a11 a12 a13 a14a21 a22 a23 a24a31 a32 a33 a34a41 a42 a43 a44

(10)

And so for A to be a 4×4 magic square, its entries satisfy the following equations:

a11 + a12 + a13 + a14 = r; a21 + a22 + a23 + a24 = r;

a31 + a32 + a33 + a34 = r; a41 + a42 + a43 + a44 = r;

(Sum of Rows)

a11 + a21 + a31 + a41 = r; a12 + a22 + a32 + a42 = r;

a13 + a23 + a33 + a43 = r; a14 + a24 + a34 + a44 = r;

(Sum of Columns)

a11 + a22 + a33 + a44 = r; a41 + a32 + a23 + a14 = r;

(Sum of Main Diagonals)

(11)

From these linear equations satisfied by A being defined as a magic square, wecan then show this is equivalent to the linear equations Mv = 0,where v is the

11

Page 12: Week 8 Report

column vector that is:

v =

ra11a12a13a14a21a22a23a24a31a32a33a34a41a42a43a44

And M is the 10× 17 matrix:

−1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0−1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0−1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0−1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1−1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0−1 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0−1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0−1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1−1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1−1 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0

And so the system of linear equations Mv = 0 can be shown to equal to theequations satisfied by the magic square A. By using matrix multiplication of Mand v we can gain 10 linear equations as follows:

− r + a11 + a12 + a13 + a14 = 0; −r + a21 + a22 + a23 + a24 = 0; −r + a31 + a32 + a33 + a34 = 0;

− r + a41 + a42 + a43 + a44 = 0;

− r + a11 + a21 + a31 + a41 = 0; −r + a12 + a22 + a32 + a42 = 0; −r + a13 + a23 + a33 + a43 = 0;

− r + a14 + a24 + a34 + a44 = 0;

− r + a11 + a22 + a33 + a44 = 0; −r + a41 + a32 + a23 + a14 = 0;

It is clear to see that this system of equations gained from Mv = 0 are equiva-lent to those in (12) however each equation is rearranged slightly.

Then by using Matlab’s command rref again, we are able to reduce the matrixM to its row reduced echelon form.By inputting the following into the command line of Matlab, we get the outputas follows:

12

Page 13: Week 8 Report

Figure 6: Using the rref command to find the row reduced form of M

And hence the row reduced echelon form of M is:

1 0 0 0 0 0 0 0 0 0 0 0 0 −1 −1 −1 −1

0 1 0 0 0 0 0 0 −1 0 0 −1/2 −1/2 1 0 0 −1/2

0 0 1 0 0 0 0 0 −1 0 1 −1/2 −3/2 1 1 0 −1/2

0 0 0 1 0 0 0 0 1 0 −1 1 1 −2 −1 0 0

0 0 0 0 1 0 0 0 1 0 0 0 1 −1 −1 −1 0

0 0 0 0 0 1 0 0 1 0 −1 −1/2 −1/2 0 0 0 1/2

0 0 0 0 0 0 1 0 1 0 0 1/2 3/2 −2 −1 −1 −1/2

0 0 0 0 0 0 0 1 −1 0 1 0 −1 1 0 0 −1

0 0 0 0 0 0 0 0 0 1 1 1 1 −1 −1 −1 −1

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

From the row reduced form of M above we can see that the rank of M is equalto 9, as this is the number of non-zero rows of M in its row reduced echelonform. As the rank of a matrix is also equal to its column rank, this tells usthat the matrix has 9 linearly independent columns (These are the 8 left mostcolumns and also the 10th column). These columns are also the pivot columns.

Therefore as our new system of linear equations for 4× 4 magic squares, has 17variables but only 9 pivot columns this means that there are 8 free variables.As this is so, this means the basic variables, r, a11, a12, a13, a14, a21, a22, a23,a31, depend on the free variables a24, a32, a33, a34, a41, a42 , a43, and a44, whichcan take any real number. So by choosing values for the free variables this willthen determine the values for the basic variables, which for any choice of the 8free variables is the only solution to Mv = 0

13

Page 14: Week 8 Report

Now we want to show that the set of 4 × 4 magic squares with real entriesform a vector space, showing this is very similar to that of the proof that setof 3× 3 magic squares form a vector space. So we can show the set S4, the setof 4 × 4 magic squares, is a vector space by showing it satisfies the followingproperties.

� Addition of the magic square matrices satisfies A1, A2 , A3 andA4. As matrix addition for any two matrices in R4×4 are associative andcommutative, so A1 and A4 hold.To show A2 holds, consider the zero element for addition of R4×4 whichis:

0 0 0 00 0 0 00 0 0 00 0 0 0

It is clear to see this is a magic square as all rows, columns and diagonalssum to zero, meaning the system of linear equations Mv = 0 is satisfiedand hence the zero element of addition is also an element of S4. So A2holds.Finally, for a given element A in S4, there exists a matrix (−A) ε R4×4

such that:

A+(−A) =

a11 a12 a13 a14a21 a22 a23 a24a31 a32 a33 a34a41 a42 a43 a44

+

−a11 −a12 −a13 −a14−a21 −a22 −a23 −a24−a31 −a32 −a33 −a34−a41 −a42 −a43 −a44

=

0 0 0 00 0 0 00 0 0 00 0 0 0

By considering (-A), we can see that the sum of rows, columns and leadingdiagonals of (-A) are all equal to −r, where r is the row sum of (A). Hence(-A) is also a magic matrix and A3 holds.

For the remaining properties of the vector space:

� For any two 4× 4 magic squares, u and v , that: α · (u + v) =α · u + α · v holds for all α ε R.

� For any 4× 4 magic square, v , that: (α+β) ·v = α ·v +β ·v holdsfor all α, β ε R..

� For any 4× 4 magic square, v , that: (αβ) ·v = α · (β ·v) holds forall α, β ε R.

� For any 4× 4 magic square, v , that: 1 · v = v

These can be shown to be true using very similar arguments to those in the 3×3magic square case, this is because all the distributive, associative and identityproperties for matrices hold for all elements of S4, as S4 is a subset of R4×4.And hence this shows that the set of 4× 4 magic squares, S4 is a vector space.

With a similar argument for that off the set of 3 × 3 magic squares, we knowthat the dimension of the set of all 4× 4 magic squares is equal to the numberof free variables in the system of linear equations which the set must satisfy.Hence the dimension of the vector space of 4× 4 magic squares is 8.

14