ME211 Midterm 1 (10/8/2004)

NAME: ______

This is an open-book test. There are 4 problems in total. You have 50 min to finish the exam. Clearly mark your final answers. To ensure consideration for partial scores, write down intermediate steps where necessary.

Good luck!

Problem Score 1 2 3 4 Total

1 1. Use any root finding method you have learned to find the zero (between 0.5 and 1) of y  x 3  3x 2 1 to two significant digits. State the name of the method you have used. (10 points)



2 2. Read the following Matlab program and answer the questions.

function I = anonym(f,a,b,n) h=(b-a)/n; S=feval(f,a); for j=1:2:n-1 x(j)=a+h*j; S=S+4*feval(f,x(j)); end for j=2:2:n-2 x(j)=a+h*j; S=S+2*feval(f,x(j)); end S=S+feval(f,b); I=h*S/3

(a) What is the purpose of this function anonym? (2 points) (b) If the file myfunc.m contains the following statements

function f=myfunc(x) f=exp(-x^2);

What is f after the following statement is executed in the command window? (6 points)

f=anonym(‘myfunc’,0,1,2)

(c) Is the following statement the proper way of using anonym? Why? (2 points)

f1=anonym(‘myfunc’,0,1,3)

3 3. The following Matlab function for Euler’s method has some errors. Identify and correct them. (10 points)

function [x,y]=Euler(f,a,b,y0,n) %solve y’=f(x,y) with initial condition y(x=a)=y0 %using n steps of Euler’s method; step size h=(b-a)/n h=(b-a)/n; x=(a:h:b); y(a)=y0; for j=1:n+1 y(j)=y(j-1)+h*feval(f,x(j-1),y(j-1)); end

4 4. (a) Write down the third-order Taylor series of ln(1+h) (up to h3) and also its remainder. (5 points)

(b) For small oscillations, the period of a pendulum can be described as l T  2  where g=9.8 m/s2 is the local acceleration of gravity and l is the length g of the pendulum. What relative error would be introduced to T if the measurement of l has a relative error of 1% (say l 1 0.01m.) (5 points) 



5 ME211 Midterm (10/26/2005)

NAME: ______

This is an open-book test. There are 4 problems in total. You have 50 min to finish the exam. Clearly mark your final answers. To ensure consideration for partial scores, write down intermediate steps where necessary. The average score in this exam is 30.5/48. Good luck!

Problem Score 1 2 3 4 Total

6 2 5. Use Newton’s method to solve x 1  0. Starting from x 0  0.2 , show the results of the first 2 iterations. (10 points)

Solution:  

y  f (x)  x 2 1; y' f '(x)  2x.

2 x2  0.2 , y0  0.2 1  0.96; y'0  2 0.2  0.4

So

y0  0.96 x1  x0   0.2   2.6 y'0 0.4 2 y1  2.6 1  5.76;

y'1  2 2.6  5.2 Then

y1 5.76 x2  x1   2.6   1.492 y'1 5.2

7 2. Use Gauss-Seidel iteration algorism to solve the following system of linear equations,

 5x1  x2  7  x1  4x2  x3  12 .   x2  2x3  8 (a) In matrix notation rewrite the above equations in a form suitable for iteration, i.e., x=Cx+D. What are C and D? (7 points)

x10  1  (b) Starting from the initial value x20  1,  x30  1 write down the result after one iteration (round to 3 significant digits). (7 points)

Solution:

(a) Rewrite the above equations in this form:

5x1   x2  7  4x2   x1  x3 12  2x3   x2  8  1  0  0 7 x (n)   5 x (n1)  1   1 5  (n)  1 1  (n1)     x2    0  x2   3 (n)  4 4 (n1) x  x  4  3   1  3    0  0    2 

So, 1 7 x (1)   x (0)   1.20 1 5 2 5 1 1 x (1)   x (0)  x (0)  3  2.45 2 4 1 4 3 1 x (1)   x (1)  4  2.78 3 2 2

8 3. Write a Matlab function for numerical integration using the two-point Gaussian b quadrature formula. Assume the integral has the form  f ( x ) d x . (10 points) a Solution: 1 n The basic form of Gaussian quadrature is: f (x)  ci f (xi ) . For a general 1   i1 interval[a,b], we have to transform the integral in [a,b] to[1,1], that is :

b 1 f (x)dx  f (At  B)d(At  B) , a 1 where x  At  B  b  a A  a  A  B  2  b  a B  b  A  B  2 So, if we set n=2, b 1 b  a b  a 1 b  a b  a 1 b  a f (x)dx  A f (At  B)dt  [ f (  ( )  )  f (  ( )  )] a 1 2 2 3 2 2 3 2

Matlab function:

function I=Gauss_2p(f,a,b) A=(b-a)/2; B=(b+a)/2; I=A*(feval(f,-A/sqrt(3)+B)+feval(f,A/sqrt(3)+B));

9 4. Solve the following ODE y' x y, 0 x1, y(1) 2. Notice here the ‘initial’ condition is given at x=1, not x=0. a. Can you still use the following Matlab function for Euler’s method, discussed in the class and attached below, to solve this problem? If yes, write the Matlab line to call this function for the above problem, assuming the derivative is saved in a function named f_midterm and 10 steps will be used. (4 points) b. What is y(0) if it is solved by midpoint method with 1 step? (10 points) [If you can’t do the problem with the initial condition at x=1, do the above problem with the initial condition y(0)=-1 and find y(1).]

Matlab function for Euler’s method function [x,y]=Euler(f,x0,x1,y0,n) h=(x1-x0)/n; y(1)=y0+h*feval(f,x0,y0); x=(x0+h:h:x1); for i=2:n y(i)=y(i-1)+h*feval(f,x(i-1),y(i-1)); end x=[x0 x]; y=[y0 y];

Solution: a. Yes, we can.

x0=1;x1=0;n=10;y0=-2; [x,y]=Euler(‘f_midterm’,x0,x1,y0,n);

b. Midpoint method: h  1

k1  hf (1,2)  1 (1 2)  1; h k 1 1 k  hf (1 ,2  1 )  1 (1  2  )  1; 2 2 2 2 2

y(0)  y(1)  k2  2 1  1

If you have questions or there are mistakes in my solution, please inform me as soon as possible. Thanks By Rui Yan

10 Scratch paper

11