Docsity

Midterm Sample Solution for Computational Methods for Engineer | MAE 107, Exams of Mechanical Engineering

Toggle sidebar
bg1
MAE 107 – Computational Methods
Summer Session II 2009
Midterm sample – Solutions
Problem 1
function fact=factorial(n)
% Computes the factorial of n for an integer n positive
if(n<0)
error(’n must be a positive integer’)
end
fact=1;
for k=1:n,
fact=fact*k;
end
Note that the previous function, indeed computes n! for n≥0. In particular for n= 0, the for...end
loop is never started as [1:n] does not have any element.
Problem 2
Consider the matrix A=
2−1 3
−2−2−2
2−7 4
.
1. The first step of the Forward Elimination procedure aims at canceling the coefficients below the
pivot coefficient a11 = 2 in the first column of A. To do so, we perform the following row operations:
−(2) ←(2) −λ21(1) with λ21 =−1,
−(3) ←(3) −λ31(1) with λ31 = 1.
The result of this operation can be written as a decomposition for A:
A=
1 0 0
−110
1 0 1
2−1 3
0−3 1
0−6 1
| {z }
A(1)
The second step of the Forward Elimination procedure aims at canceling the coefficients below the
second pivot coefficient a22 =−3 in the second column of the modified A(1). The following row
1
pf3
pf4
pf5

Partial preview of the text

Download Midterm Sample Solution for Computational Methods for Engineer | MAE 107 and more Exams Mechanical Engineering in PDF only on Docsity!

MAE 107 – Computational Methods

Summer Session II 2009

Midterm sample – Solutions

Problem 1

function fact=factorial(n)

% Computes the factorial of n for an integer n positive

if(n<0) error(’n must be a positive integer’) end fact=1; for k=1:n, fact=fact*k; end

Note that the previous function, indeed computes n! for n ≥ 0. In particular for n = 0, the for...end loop is never started as [1:n] does not have any element.

Problem 2

Consider the matrix A =

  1. The first step of the Forward Elimination procedure aims at canceling the coefficients below the pivot coefficient a 11 = 2 in the first column of A. To do so, we perform the following row operations:

− (2) ← (2) − λ 21 (1) with λ 21 = −1, − (3) ← (3) − λ 31 (1) with λ 31 = 1.

The result of this operation can be written as a decomposition for A:

A =

A(1)

The second step of the Forward Elimination procedure aims at canceling the coefficients below the second pivot coefficient a 22 = −3 in the second column of the modified A(1). The following row

operation is performed (3) ← (3) − λ 32 (2) with λ 32 = 2. The result of this operation can be written as the following decomposition for A(1):

A(1)^ =

From these two steps we can write:

A =

The LU decomposition of A is therefore:

A = L · U, with L =

 (^) and U =

  1. Using the fundamental property of the determinant, the determinant of A is the product of the determinants of L and of U. These two matrices are diagonal, therefore their determinants are easily computed as the product of their diagonal terms. |L| = 1 and |U| = 6, therefore

|A| = 6

  1. To compute Y = A−^1 we need to solve 3 linear systems for the column vectors y(1), y(2)^ and y(3), which are the columns of Y:

Ay(1)^ =

 (^) , Ay(2)^ =

 (^) , Ay(3)^ =

Using the LU decomposition of A we can replace these three systems by three pairs of triangular systems: (^) { Lz(k)^ = b Uy(k)^ = z(k)^

for 1 ≤ k ≤ 3.

− First column y(1):  

 (^) z(1)^ =

 (^) =⇒ z(1)^ =

 (^) y(1)^ =

 (^) =⇒ y(1)^ =

2 3 3

Problem 3

f ′(1) = 5 6 = 0 therefore Newton-Raphson’s method will lead to a quadratic convergence if the initial guess is close enough from the root ˜x = 1. f ′(x) = 3x^2 + 2, so a possible fixed point iterative scheme x(n+1)^ = g(x(n)) is

g(x) = x −

f (x) f ′(x) = x −

x^3 + 2x − 3 3 x^2 + 2

Problem 4

The general form of the Taylor Series of a function F (x) in 0 up to O(x^4 ) is given by

F (x) = F (0) + xF ′(0) + x^2 2

F ′′(0) +

x^3 6

F ′′′(0) + O(x^4 ). (1)

  1. f (0) = 1. Using successive differentiation, we also find:

− f ′(x) = − (^) (1+^1 x) 2 ⇒ f ′(0) = −1, − f ′′(x) = (^) (1+^2 x) 3 ⇒ f ′′(0) = 2,

− f ′′′(x) = − (^) (1+^6 x) 4 ⇒ f ′′′(0) = −6.

Substituting in (1), we obtain the requested Taylor Series for f (x):

f (x) = 1 − x + x^2 − x^3 + O(x^4 ).

  1. Using this result

g(x) = 1 − x 1 + x

= (1 − x)

1 − x + x^2 − x^3 + O(x^4 )

= 1 − x − x(1 − x) + x^2 (1 − x) − x^3 (1 − x) + O(x^4 )

which is equivalent to g(x) = 1 − 2 x + 2x^2 − 2 x^3 + O(x^4 ).

  1. h(0) = 1. Using successive differentiation, we also find:

− h′(y) = 2 √^1 1+y ⇒ h′(0) = 12 ,

− h′′(y) = − (^) 4(1+^1 x) 3 / 2 ⇒ h′′(0) = − 14 ,

− h′′′(y) = (^) 8(1+^3 x) 5 / 2 ⇒ h′′′(0) = 38.

After substitution into (1), we find the requested Taylor Series expansion of h(y) near 0:

h(y) = 1 + y 2

y^2 8

y^3 16

  • O(y^4 ). (2)
  1. From question 2., we have obtained that 1 − x 1 + x = 1 − 2 x + 2x^2 − 2 x^3 + O(x^4 ) = 1 + y, with y = − 2 x + 2x^2 − 2 x^3 + O(x^4 ).

Using the expansion (2), obtained from

1 + y near 0, we find (noticing that O(y^4 ) = O(x^4 )),

p(x) =

1 − x 1 + x

1 + y

y 2

y^2 8

y^3 16

  • O(y^4 )

=1 +

− 2 x + 2x^2 − 2 x^3 + O(x^4 )

− 2 x + 2x^2 − 2 x^3 + O(x^4 )

− 2 x + 2x^2 − 2 x^3 + O(x^4 )

  • O(x^4 )

=1 +

− 2 x + 2x^2 − 2 x^3 + O(x^4 )

4 x^2 − 8 x^3

− 8 x^3

  • O(x^4 )

p(x) = 1 − x +

x^2 2

x^3 2

  • O(x^4 )

Problem 5

  1. a, b and c correspond respectively to the subdiagonal, main diagonal and superdiagonal of A. For n = 5, A is equal to

A =

b 1 c 1 0 0 0 a 2 b 2 c 2 0 0 0 a 3 b 3 c 3 0 0 0 a 4 b 4 c 4 0 0 0 a 5 b 5

  1. We observe that not all the coefficients of a and c are used in the matrix A and in the code. For a general n, a 1 and cn are unused: their value will not influence the result of the function.
  2. During the Forward Elimination, the function only modifies b and r:

− The Forward Elimination is designed to set all the coefficient of the subdiagonal equal to zeros. The actual operation is not performed by the function as it would be a useless additional cost: since they are thereafter equal to zeros, the coefficients of a will not be used again. The routine understands them as being equal to zero (they will not be called during the Backward Substitution) but does not perform the operation as it already knows the result. − All the elements above any cj are equal to zero. Therefore, the Forward Elimination leaves them unchanged and no operation is necessary.

  1. − Forward Elimination: A loop is performed for j varying between 2 and n. At each step of the loop, the computer performs one division to compute factor and the computation of b(j) and r(j) costs one multiplication and one subtraction. Each step of the loop therefore has a cost of 5 operations. The total cost of Forward Elimination is therefore 5(n − 1).