Sie sind auf Seite 1von 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
zeros. 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

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


The bisection method works by taking some initial interval [a, b] and through
successive iterations shrinking the interval, trapping the root inside. The first
choices of a and b are made so that f (a) and f (b) have different signs. This
decision guarantees that the root will be in the interval by the intermediate
value theorem. The point x = (a + b)/2 is then considered, if the function at
this point has the same sign as a then the interval [a, b] is replaced by [x, b] and
if it has the same sign as b then the interval is replaced by [a.x] this procedure
is 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 that
f (x) has different signs at x = 1 and x = 2 , so we took these values to be our a
and b respectively to give us our starting interval [1, 2]. The tolerance we were
working to is 1012 so we would have to perform enough iterations such that
|a0 b0 | < 1012 where a0 and b0 is some altered value of a and b. Carrying out
these iterations in Matlab gave the output shown below.

Figure 2: Matlab output of using the bisection method to approximate a root


of f (x)
The Newton-Raphson method is another way of approximating roots, again
by iteration. This method is derived using a Taylor appoximation to reduce
the error in an estimate. It involves taking tangents to a function at some
initial approximation of a root to find a better approximation. Each successive
approximation is found according to the formula :
xn+1 = xn

f (xn )
f 0 (xn )

(1)

By using Newton-Raphson we can find an approximation to our root of f (x)


within 1012 obtaining the following Matlab output:

Figure 3: Matlab output of using the Newton-Raphson method to approximate


a root of f (x)

Solution 8A(b) : Iterations


If we consider the effect of each iteration in the bisection method we can see
that the size of the interval is halved each time, so that is after N trials the size
of the interval will be;
ba
b0 a0 = N
(2)
2
Using this fact we can calculate the number of iterations necessary to reach our
required tolerance so for some general tolerance 10M we can see that it will
take N iterations where :
ba
10M
2N
2N
10M
ba
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
M
(ba)
take at least log(10
iterations to have an approximation of a root to 10M
log(2)
accuracy. When carrying out these computations it is much more favourable to
use a method with fewer iterations necessary , for example the Newton-Raphson
method. Taking the error at the nt h iteration to be en = xn + r in the NewtonRaphson scheme, where r is the actual value of the root. We can clearly see
that the errors satisfy the difference equation ;
f (xn )
f 0 (xn )
f (xn )
en+1 + r = en + r 0
f (xn )
en f 0 (xn ) f (xn )
en+1 =
f 0 (xn )
xn+1 = xn

Then by Taylors theorem ;


1
f (r) = f (xn ) + (r xn )f 0 (xn ) + (r xn )2 f 00 ()
2

(3)

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


1
0 = f (xn ) + (r xn )f 0 (xn ) + (r xn )2 f 00 ()
2
1 2 00
0
0 = f (xn ) en f (xn ) + en f ()
2
1 2 00
0
en f (xn ) f (xn ) = en f ()
2
en 2 f 00 ()
en+1 =
2f 0 (xn )

So letting Cn =

f 00 ()
2f 0 (xn )

We get that;
en+1 = Cn en 2

(4)

Solution 8A(c) : Secant Method


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

f (xn )
tn

where

tn =

f (xn ) f (xn1 )
xn xn1

(5)

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

Figure 4: Matlab output of using the Secant method to approximate a root of


f (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)
clear
x(1)=2;
x(2) = 1;
% Input initial guess
tol = 10(-12);
% Sets tolerance.
xdiff = inf;
% initializes difference between successive estimates
k=2;
% initializes counter
i=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, this
means that the rows, columns and main diagonals of this matrix sum to the
same number, say r. Defining A as the following:

a11 a12 a13


A = a21 a22 a23
(6)
a31 a32 a33

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;

a11 + a21 + a31 = r;

a12 + a22 + a32 = r;

a13 + a23 + a33 = r;

(Sum of Rows)
(Sum of Columns)
a11 + a22 + a33 = r;

a31 + a22 + a13 = r;


(Sum of Main Diagonals)
(7)

The system of linear equations:

1
1

1
Mv =
1

1
1

1
0
0
1
0
0
1
0

1
0
0
0
1
0
0
0

1
0
0
0
0
1
0
1

0
1
0
1
0
0
0
0

0
1
0
0
1
0
1
1

0
1
0
0
0
1
0
0

0
0
1
1
0
0
0
1

0
0
1
0
1
0
0
0

0
a11

0 a12


1
a13

0
a21 = 0

0
a22

1 a23

1
a31

a32
0
a33

(8)

can be shown to be equal to the equations satisfied by the magic square matrix
A. This can be done by using matrix multiplication of M and v to gain 8 linear
equations 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 3
magic squares entries with corresponding row sum.

Solution 8B(b)
Using the command rref in Matlab, we are able to reduce the matrix M to row
reduced echelon form.
By inputting the following into the command line of Matlab, we get the output
as follows:

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

1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

0 0 0 0

0 0 0 0

0 0 0 0
0

echelon form of M is:


1

2/3 2/3

2/3

1/3

1/3

2/3

2/3

1/3

1/3 1/3

4/3 1/3

1/3

2/3

2/3

4/3

1
/3

2/3

(9)

From the row reduced form of M above we can see that the rank of M is equal
to 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 us
that 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 columns
this means that there are 3 free variables. As this is so, this means the basic
variables , 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 determine the values for the rest of the variables, which for any choice of the three
variables is the only solution to (8), as each basic variable depends on the all
three of the free variables as follows:

r a31 a32 a33 = 0;


2
2
1
a11 a31 a32 + a33
3
3
3
1
2
2
a12 a31 + a32 a33
3
3
3
2
2
1
a13 + a31 a32 a33
3
3
3
2
1
4
a21 + a31 a32 a33
3
3
3
1
1
1
a22 a31 a32 a33
3
3
3
4
1
2
a23 a31 a32 + a33
3
3
3

= 0;
= 0;
= 0;
= 0;
= 0;
= 0;

From here, we want to show that the set of 3 3 magic squares , S3 , (the set
of all 3 3 matrices, of form A, which satisfy (8) ) forms a vector space. For
this it must be shown this set satisfies all properties of a vector space.
Addition of the magic square matrices satisfies A1, A2 , A3 and
A4. As matrix addition for any two matrices in R33 are associative and
commutative, so A1 and A4 hold.
To show A2 holds, consider the zero element for addition of R33 which
is:

0 0 0
0 0 0
0 0 0
It is clear to see this is a magic square as all rows, columns and diagonals
sum to zero, meaning the system of linear equations (8) is satisfied and
hence 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)  R33
such that:

a11 a12 a13


a11 a12 a13
0 0 0
A + (A) = a21 a22 a23 + a21 a22 a23 = 0 0 0
a31 a32 a33
a31 a32 a33
0 0 0

By considering (-A), we can see that the sum of rows, columns and leading
diagonals of (-A) are all equal to (a11 + a12 + a13 ) = r, where r is the
row 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 by
scalars. That is:

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

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 real
elements, then S3 is a subset of R33 . 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 holds
for all ,  R.
This property is also true by the previous argument.
For any 3 3 magic square, v , that: () v = ( v) holds for
all ,  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 of
R33 , then the associative property also holds for all elements of S3 .
For any 3 3 magic square, v , that: 1 v = v
This holds by the identity property of matrices by scalar multiplication.
That is:

1X=X
where X is a mn matrix. And so by a similar arguement for the previous
properties, this also holds for the set S3 .
And so as the above properties hold this means that the set S3 is a vector space
of the field R.
Note that the number of free variables in the system also lets us know the
determinant of the vector space of 3 3 magic squares, and that the determinant is equal to the number of these free variables. This is because all other
variables in the magic squares are determined by the choice of these free variables, and as the free variables can take any real number, the vector space must
contain all magic squares where a31 , a32 and a33 are real numbers. So our basis
must span all possible values of the free variables, and a way to ensure this is
by 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 square
with any value of the free variables to be made, and as for each choice of free

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

Solution 8B(c)
Given the matrices below, we want to show that they form a basis. This can be
achieved by considering linear independence between the three matrices. It is
also key to note that each of the matrices are magic matrices as all satisfy the
system of linear equations in (7).

1 1 1
M1 = 1 1 1 with r = 3
1 1 1

0
1 1
1 with r = 0
M2 = 1 0
1 1 0

1 1 0
1 with r = 0
M3 = 1 0
0
1 1
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 combination
of 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 multiple
of 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 row
sums, the row sum of M2 and M3 is zero, and the row sum of M1 is 3. As it is
true 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 sum
of the resultant matrix (Proof below). Then any linear combination of any two
matrices, say M2 and M3 , with row sum zero cannot create a matrix of row sum
other than zero. Hence M1 is linear independent from M2 and M3 .
Therefore as the 3 matrices are linearly independent and are all elements of
the vector space of 3 3 magic matrices, it is so that as the vector space has
dimension 3, any set of 3 linear independent 3 3 magic matrices form a basis
to the vector space.

10

Proof that summing two magic matrices also sums their row sum
Define:

a11
A = a21
a31

b11
B = b21
b31

a12
a22
a32
b12
b22
b32

a13
a23
a33

b13
b23
b33

to be to be two magic matrices of row sum, rA =


Then:

(a11 + b11 ) (a12 + b12 )


A + B = (a21 + b21 ) (a22 + b22 )
(a31 + b31 ) (a32 + b32 )

and rB = respectively.

(a13 + b13 )
(a23 + b23 )
(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 a
magic square by the same definition as for the 3 3 case, which states that the
rows, columns and main diagonals of this matrix sum to the same number, say
r. Define A as the following:

a11 a12 a13 a14


a21 a22 a23 a24

A=
(10)
a31 a32 a33 a34
a41 a42 a43 a44
And so for A to be a 44 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;

a11 + a21 + a31 + a41 = r;

a12 + a22 + a32 + a42 = r;

a13 + a23 + a33 + a43 = r;

a14 + a24 + a34 + a44 = r;

a11 + a22 + a33 + a44 = r;

a41 + a32 + a23 + a14 = r;

(Sum of Rows)

(Sum of Columns)
(Sum of Main Diagonals)
(11)
From these linear equations satisfied by A being defined as a magic square, we
can then show this is equivalent to the linear equations M v = 0,where v is the
11

column vector that is:

And M is the

1
1

1
1

r
a11

a12

a13

a14

a21

a22

a23

v=
a24
a31

a32

a33

a34

a41

a42

a43
a44

10 17 matrix:
1
0
0
0
1
0
0
0
1
0

1
0
0
0
0
1
0
0
0
0

1
0
0
0
0
0
1
0
0
0

1
0
0
0
0
0
0
1
0
1

0
1
0
0
1
0
0
0
0
0

0
1
0
0
0
1
0
0
1
0

0
1
0
0
0
0
1
0
0
1

0
1
0
0
0
0
0
1
0
0

0
0
1
0
1
0
0
0
0
0

0
0
1
0
0
1
0
0
0
1

0
0
1
0
0
0
1
0
1
0

0
0
1
0
0
0
0
1
0
0

0
0
0
1
1
0
0
0
0
1

0
0
0
1
0
1
0
0
0
0

0
0
0
1
0
0
1
0
0
0

0
0

1
0

And so the system of linear equations M v = 0 can be shown to equal to the


equations satisfied by the magic square A. By using matrix multiplication of M
and 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 + a12 + a22 + a32 + a42 = 0;

r + a13 + a23 + a33 + a43 = 0;

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


r + a11 + a21 + a31 + a41 = 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 M v = 0 are equivalent to those in (12) however each equation is rearranged slightly.
Then by using Matlabs command rref again, we are able to reduce the matrix
M to its row reduced echelon form.
By inputting the following into the command line of Matlab, we get the output
as follows:

12

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/2 1/2

1/2 3/2

1/2

1/2

3/2

0 1

0 1

1/2

1/2

1/2

1/2

1
/2

From the row reduced form of M above we can see that the rank of M is equal
to 9, as this is the number of non-zero rows of M in its row reduced echelon
form. As the rank of a matrix is also equal to its column rank, this tells us
that the matrix has 9 linearly independent columns (These are the 8 left most
columns 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 17
variables 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 , which
can take any real number. So by choosing values for the free variables this will
then determine the values for the basic variables, which for any choice of the 8
free variables is the only solution to M v = 0

13

Now we want to show that the set of 4 4 magic squares with real entries
form a vector space, showing this is very similar to that of the proof that set
of 3 3 magic squares form a vector space. So we can show the set S4 , the set
of 4 4 magic squares, is a vector space by showing it satisfies the following
properties.
Addition of the magic square matrices satisfies A1, A2 , A3 and
A4. As matrix addition for any two matrices in R44 are associative and
commutative, so A1 and A4 hold.
To show A2 holds, consider the zero element for addition of R44 which
is:

0 0 0 0
0 0 0 0

0 0 0 0
0 0 0 0
It is clear to see this is a magic square as all rows, columns and diagonals
sum to zero, meaning the system of linear equations M v = 0 is satisfied
and hence the zero element of addition is also an element of S4 . So A2
holds.
Finally, for a given element A in S4 , there exists a matrix (A)  R44
such that:



a11 a12 a13 a14
a11 a12 a13 a14
0
a21 a22 a23 a24 a21 a22 a23 a24 0


A+(A) =
a31 a32 a33 a34 +a31 a32 a33 a34 = 0
0
a41 a42 a43 a44
a41 a42 a43 a44

By considering (-A), we can see that the sum of rows, columns and leading
diagonals 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 holds
for all ,  R..
For any 4 4 magic square, v , that: () v = ( v) holds for
all ,  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 33
magic square case, this is because all the distributive, associative and identity
properties for matrices hold for all elements of S4 , as S4 is a subset of R44 .
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 know
that the dimension of the set of all 4 4 magic squares is equal to the number
of 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

0
0
0
0

0
0
0
0

0
0

0
0

Das könnte Ihnen auch gefallen