Docsity

Solved Assignment for Computational Methods for Engineer | MAE 107, Assignments of Mechanical Engineering

Toggle sidebar
bg1
MAE 107 – Computational Methods
Summer Session II 2009
Homework # 1
due on Wednesday, August 12 in class
4 problems – 20 points
Guidelines: Please turn in a neat homework that gives all the formulae that you have used and all the necessary
information for the grader to understand your solution. Also, if the problem requires you to write a MATLAB
code, please turn in, along with the written explanation of your solution, (i) a copy of the code you used, carefully
documented with comments and using proper indentation, and (ii) the requested ouputs and graphs.
Problem 1 – 4 points
1. Compute manually the eigenvalues of the matrix A=
0−1−1
−2 1 −2
1−1 2
. (Remember that λis an eigen-
value of Aif |A−λI|=0 with Ithe identity matrix – Hint: one of the eigenvalue is equal to 1). For each of
the eigenvalues compute an associated eigenvector.
Solution: We first find the eigenvalues
0=|A−λI|=
−λ−1−1
−2 1−λ−2
1−1 2−λ
= (−λ)−3λ+λ2+2[λ−3] + [3−λ] = −λ3+3λ2+λ−3
Which is the same as 0 =λ3−3λ2−λ+3= (λ+1)(λ−1) (λ−3)⇒λ=−1,1,3
We can now find associated eigenvectors
λ=−1:
Ax=xλ⇒−y−z=−x
−2x+y−2z=−y
x−y+2z=−z⇒
x−y−z=0
−2x+2y−2z=0
x−y+3z=0⇒
x=1
y=1
z=0⇒x=
1
1
0
λ=1:
Ax=xλ⇒−y−z=x
−2x+y−2z=y
x−y+2z=z⇒−x−y−z=0
−2x−2z=0
x−y+z=0⇒
x=1
y=0
z=−1⇒x=
1
0
−1
λ=3:
Ax=xλ⇒−y−z=3x
−2x+y−2z=3y
x−y+2z=3z⇒−3x−y−z=0
−2x−2y−2z=0
x−y−z=0⇒
x=0
y=1
z=−1⇒x=
0
1
−1
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Solved Assignment for Computational Methods for Engineer | MAE 107 and more Assignments Mechanical Engineering in PDF only on Docsity!

MAE 107 – Computational Methods

Summer Session II 2009

Homework # 1

due on Wednesday, August 12 in class

4 problems – 20 points

Guidelines : Please turn in a neat homework that gives all the formulae that you have used and all the necessary information for the grader to understand your solution. Also, if the problem requires you to write a MATLAB code, please turn in, along with the written explanation of your solution, (i) a copy of the code you used, carefully documented with comments and using proper indentation, and (ii) the requested ouputs and graphs.

Problem 1 – 4 points

  1. Compute manually the eigenvalues of the matrix A =

. (Remember that λ is an eigen-

value of A if | A − λ I | = 0 with I the identity matrix – Hint: one of the eigenvalue is equal to 1). For each of the eigenvalues compute an associated eigenvector.

Solution: We first find the eigenvalues

0 = | A − λ I | =

−λ − 1 − 1 − 2 1 − λ − 2 1 − 1 2 − λ

= (−λ)

[

− 3 λ + λ^2

]

  • 2 [λ − 3 ] + [ 3 − λ] = −λ^3 + 3 λ^2 + λ − 3

Which is the same as 0 = λ^3 − 3 λ^2 − λ + 3 = (λ + 1 )(λ − 1 )(λ − 3 ) ⇒ λ = − 1 , 1 , 3 We can now find associated eigenvectors λ = − 1 :

A x = x λ ⇒

− y − z = − x − 2 x + y − 2 z = − y x − y + 2 z = − z

x − y − z = 0 − 2 x + 2 y − 2 z = 0 x − y + 3 z = 0

x = 1 y = 1 z = 0

⇒ x =

λ = 1 :

A x = x λ ⇒

− y − z = x − 2 x + y − 2 z = y x − y + 2 z = z

− x − y − z = 0 − 2 x − 2 z = 0 x − y + z = 0

x = 1 y = 0 z = − 1

⇒ x =

λ = 3 :

A x = x λ ⇒

− y − z = 3 x − 2 x + y − 2 z = 3 y x − y + 2 z = 3 z

− 3 x − y − z = 0 − 2 x − 2 y − 2 z = 0 x − y − z = 0

x = 0 y = 1 z = − 1

⇒ x =

  1. In MATLAB, write a short script file that defines the matrix A as a 3 × 3 array and computes the eigenvalues and eigenvectors of A using the eig function of MATLAB. Compare with your response to the previous question (remember that if x is an eigenvector, then α x is also an eigenvector for any α 6 = 0).

Solution: Note that MATLAB code will be identified with the font: MATLAB code will have this font!

% Eigenvalue/vector script for MAE 107 HW problem 1. A = [0 -1 -1 ; -2 1 -2 ; 1 -1 2] [V,D]=eig(A)

Witht the output

A =

V =

D =

We note here that eigenvectors are not unique, hence the difference between the columns of the MATLAB solution and the analytic solutions found above. It is, however, important to note that the eigenvector x satisfies the equation A x = λ x where x 6 = 0 which holds true for the analytic and numerical soltuions.

Problem 2 – 7 points

(adapted from Chapra – Exercise 3.13)

The “divide and average” method, an old-time method for approximating the square root of any positive number a , can be formulated as

xn + 1 = xn + a / xn 2

  1. Write a first function divide.m that uses a for ... end loop structure to implement this algorithm. The function should take as arguments the value of a (positive real number), the initial guess x 0 and the maximum

(^00 1 2 3 4 5 6 7 8 9 )

200

400

600

800

1000

1200

1400

1600

Convergence of Square Root Estimation Algorithma = 8465 and x0 = 1600 Actual value sqrt(a) = 92.

Number of iterations (n)

Estimated Value

(^00 1 2 3 4 5 6 7 8 9 )

2

4

6

8

10

12

14

16

18 a = 8465 and x0 = 1600^ True Relative Error

Numer of iterations (n)

True Relative Error

Figure 1: Conversion alaysis for

% Points for data arrays x = zeros(1,N+1); e = zeros(1,N+1);

% Initialize the first point to be our initial guess with the associated % true relative error x(1,1) = x0; e(1,1) = abs(sqrt(a) - x0)/sqrt(a);

% For loop to iterate N steps with the divide algorithm % Iterated data fields of values (x_temp) and error (e_temp) are stored in % the vectors x and e respectivly for plotting for steps = 1:N [x_temp, e_temp] = divide(a,x0,steps); x(1,steps+1) = x_temp; e(1,steps+1) = e_temp; end

% Plots of convergence and error figure(1) subplot(2,1,1) plot([0:N],x,’k’) title({[’Convergence of Square Root Estimation Algorithm’];[’a = ’, num2str(a), ’ and x0 = ’,n xlabel(’Number of iterations (n)’)

ylabel(’Estimated Value’) subplot(2,1,2) plot([0:N],e,’r’) title({[’True Relative Error’];[’a = ’, num2str(a), ’ and x0 = ’,num2str(x0)]}) xlabel(’Number of iterations (n)’) ylabel(’True Relative Error’)

  1. Prove analytically, and confirm it with your function divide.m, that all the xn have the same sign as x 0 , and therefore that the choice of x 0 determines which root will be picked by the algorithm.

Solution: Proof (by induction):

− Assume x 0 > 0 (note a > 0!) − Assume that xi > 0 for i > 0, and prove that it implies that xi + 1 > 0 xi + 1 = xi +^ xia 2 ⇒^2 xi +^1 =^ xi^ +^

a xi xi > 0 by assumtion and ⇒ a xi > 0 because a > 0 Therefore 2 xi + 1 > 0 ⇒ xi + 1 > 0

− x 0 > 0, and xi > 0 ⇒ xi + 1 > 0 for all i > 0, therefore xi > 0 for all values of i ≥ 0

Note that the same proof holds for x 0 < 0. Therefore, xi has the same sign as x 0.

We can prove this with the algorithm by noting:

% check divide.m sign script % Initialization value N = 10; a = 8465; x0 = 1600;

[xp,e] = divide(a,x0,N) [xn,e] = divide(a,-x0,N)

And noting the sign of the results for x: i.e. xn is negative and xp is positive relative to the sign of x 0!

xp =

e =

x_prev = x0; x0 = (1/2)*(x0 + (a/x0)); a_r_e = abs((x0 - x_prev)/x0); counter = counter + 1; if (a_r_e > 1e-10) break end end

% Display the number of iterations disp([’Needed ’, num2str(counter), ’ iterations for convergence’])

  1. Use this function divide2.m with a = 2, and x 0 = 2. How many iterations are necessary? What about with x 0 = 20? Comment on the efficiency of this method to compute the square-root of a number.

Solution:

[x0,a_r_e,counter] = divide2(2,2) Needed 5 iterations for convergence

x0 =

a_r_e =

1.1278e-

counter =

5

[x0, a_r_e, counter]=divide2(2,20) Needed 9 iterations for convergence

x0 =

a_r_e =

4.7103e-

counter =

9

You can observe that this method is very efficient: with only five iterations, the value of

2 was found with more than ten significant digits accurate. The number of iterations is reduced if the initial guess is close from the correct value.

Problem 3 – 3 points

(from Chapra – Exercise 4.8)

  1. Evaluate the polynomial y = x^3 − 7 x^2 + 8 x − 0 .35 at x = 1 .37 exactly and using 3-digit arithmetic with chopping. Evaluate the absolute and relative errors.

(3-digit arithmetic with chopping: after each elementary operation is performed, the result is approximated by the nearest number with 3 significant digits. Example: a = 1 .71 and b = 9 .76; if computed exactly, ab = 16 .6896. In 3-digit arithmetic with chopping, ab is approximated as 16.7.) Solution:

y = ( x ) ( x ) ( x ) − 7 ( x ) ( x ) + 8 ( x ) ( x ) − 0. 35 = ( 1. 88 ) ( 1. 37 ) − 7 ( 1. 88 ) + 8 ( 1. 37 ) − 0. 35 = 2. 58 − 13. 2 + 11. 0 − 0. 35 = − 10. 6 + 11. 0 − 0. 35 = 0. 40 − 0. 35 = 0. 05

Absolute error = | 0. 043053 − 0. 05 | = 0. 006947 Relative error = 0.^0430530. 043053 −^0. 05 = 0 .1614 or 16.14%

  1. Repeat the calculation using 3-digit arithmetic but, this time, express y as y = (( x − 7 ) x + 8 ) x − 0 .35. Evaluate the absolute and relative errors and compare with the values you obtained at the previous question. Comment. Solution:
  1. Use this function to compute SN with N = 10 p^ with p = 2, 3, 4, 5, 6 and 7. Show that SN → π as N goes to infinity.

3.14 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7

3.154^ Convergence of^ π−Estimation Algorithm

Number of iterations 10x

Estimated Value

Figure 2: Convergence π estimation algorithm

  1. Plot ε N as a function of N for the same values of N as in the previous question. Use logarithmic axes (don’t forget to label the axes of your plot). Show graphically that ε N decreases like 1/ N when N is large enough (this means that for large enough N , ε N ∼ A / N with A a constant). Solution:

The variation of the true relative error with N is shown on Figure 3 in logarithmic axes. The curve is observed to be a straight line. Since both axes are logarithmic, this means that log ε ∼ α log N + β or equivalently ε ∼ AN α. Comparing with the curve corresponding to 1/ N , we observe that they are parallel and therefore have the same slope α = −1. This shows that ε ∼ A / N and the error decreases like 1/ N as N gets large.

101 102 103 104 105 106 107

10 −

10 −

10 −

10 −

10 −

10 −

10 −

10 −

Number of iterations

True relative error

True relative error (log scale)

Error 1/N

Figure 3: True Relative Error for π estimation algorithm