Docsity

Numerical Analysis - assignment 1 with Solutions | MAT 128A, Assignments of Mathematical Methods for Numerical Analysis and Optimization

Toggle sidebar
bg1
MAT 128 A - Numerical Analysis
Homework 1
1. a)We want to find the limit
lim
x→0f(x) = lim
x→0
1−cos(x)
x2.
We can use l’hopital’s rule twice to find the limit. Taking two derivatives of the
numerator and denominator we have
lim
x→0f(x) = lim
x→0
1−cos(x)
x2= lim
x→0
g(x)
h(x)
= lim
x→0
g00(x)
h00(x)
= lim
x→0
cos(x)
2
=1
2
b)As x→0, f(x)→1
2as O(x2). We see
1
x2(1 −cos(x)) = 1
x21−1−1
2x2+O(x4)
=1
2+O(x2)
c)Using MATLAB f(x) was computed for x= 10−1,10−2, ..., 10−10. Results
are shown in the table below.
x f(x)
10−10.4995834721
10−20.4999958333
10−30.4999999583
10−40.4999999969
10−55.0000004137
10−65.0004445029
10−70.4996003610
10−80
10−90
10−10 0
The results are close to 0.5 for xfrom 10−1to 10−4, and evaluate to 0 for
values of xless than or equal to 10−8. This is due to the computer evaluating
cos(x) = 1 for x < 10−8.
1
pf3
pf4
pf5

Partial preview of the text

Download Numerical Analysis - assignment 1 with Solutions | MAT 128A and more Assignments Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

MAT 128 A - Numerical Analysis Homework 1

  1. a)We want to find the limit

lim x→ 0

f (x) = lim x→ 0

1 − cos(x) x^2

We can use l’hopital’s rule twice to find the limit. Taking two derivatives of the numerator and denominator we have

lim x→ 0

f (x) = lim x→ 0

1 − cos(x) x^2

= lim x→ 0

g(x) h(x)

= lim x→ 0

g′′(x) h′′(x)

= lim x→ 0

cos(x) 2 =

b)As x → 0, f (x) → 12 as O (x^2 ). We see

1 x^2 (1 − cos(x)) =

x^2

x^2 + O(x^4 )

  • O(x^2 )

c)Using MATLAB f (x) was computed for x = 10−^1 , 10 −^2 , ..., 10 −^10. Results are shown in the table below.

x f (x) 10 −^1 0. 10 −^2 0. 10 −^3 0. 10 −^4 0. 10 −^5 5. 10 −^6 5. 10 −^7 0. 10 −^8 10 −^9 10 −^10

The results are close to 0.5 for x from 10−^1 to 10−^4 , and evaluate to 0 for values of x less than or equal to 10−^8. This is due to the computer evaluating cos(x) = 1 for x < 10 −^8.

d)If the computer can only represent floating point numbers with N decimal digits of accuracy, then fl(1 + x^2 ) = 1 when x^2 < 10 −N^ , where fl(y) represents the floating point representation of y. When evalutating f , we will see large errors when |x| ≤ 10 −

√ N (^) since cos(x) = 1 + O(x (^2) ).

e)We can remove the accuracy problem by rearranging f (x).

f (x) = 1 − cos(x) x^2

1 − cos(x) x^2

1 + cos(x) 1 + cos(x)

1 − cos^2 (x) x^2 (1 + cos(x))

=

sin^2 (x) x^2 (1 + cos(x))

Now when x << 1, sin(x) ≈ 0 and cos(x) ≈ 1 and the problem with floating point representation is taken care of. See the table below. There will be a problem when cos(x) = −1, i.e. when x = (2n + 1)π.

x f (x) 10 −^1 0. 10 −^2 0. 10 −^3 0. 10 −^4 0. 10 −^5 0. 10 −^6 0. 10 −^7 0. 10 −^8 0. 10 −^9 0. 10 −^10 0.

  1. a)Code to evaluate arctan

function ret = evalarctan(x,N)

% evaluates arctangent using arctan(x) = \sum_0^N \frac{(-1)^n x^{2n+1}}{2n+1} % inputs: x - point at which to evaluate arctan % N - number of terms to use in series % output: ret - approximation of artcan(x)

sum = 0;

for i=0:1:N sum = sum + (-1)^i * x^(2i+1) / (2i + 1); end

ret = sum;

a)With dt = 0. 1 s and running for 864000 time steps, we have t = 8. 640000000054126 ∗ 104 s. The absolute error is ‖t − 86400 ‖ = 0. 54126 ∗ 10 −^7 s. The relative error is ‖t− 8640086400 ‖= 6. 264 ∗ 10 −^13 s.

b)With dt = 0. 125 s and running for 691200 time steps, we have t = 86400 s. The absolute and relative error is zero.

c)The result with dt = 0.125 s has no error because 0.125 = 18 = 2−^3 can represented exactly in binary. 0 .1 has no exact binary represntation and is subject to computer rounding errors.

  1. We want to approximate a function f using a second degree polynomial interpolation. The nodes are equally spaced over the period of the function and we will use the three closest node values in our interpolation, i.e. (x, x−h, x+h). f is given by f (x) = ecos(x).

We want to find an h that guarantees that the interpolation produces values within a tolerance of 10−^6. We can use Theorem 3.3 from the text to see that for parabolic interpolation

f (x) = P (x) +

f (n+1)(ζ(x)) (n + 1)!

(x − x 0 )(x − x 1 )(x − x 2 )

= P (x) + f ′′′(ζ(x)) 6

(h^3 )

where P (x) is our interpolating polynomial and the other term is the error. Taking three derivatives of f gives

f ′′′(x) = sin(x)ecos(x)^

[

1 + 3cos(x) − sin^2 (x)

]

We need to bound f ′′′(x) and we do this by taking the absolute value and using the triangle inequality and − 1 ≤ sin(x),cos(x) ≤ 1 to see

f ′′′(x) ≤ 4 e

We want the error to be less than 10−^6 so we need

10 −^6 >

f ′′′(ζ(x))h^3

4 eh^3

This gives us our condition on h

h ≤

2 e

10 −^6

≈ 5. 51819 ∗ 10 −^6

  1. a)Here is the Neville’s Method code modified to terminate when successive diagonal enteries are less than a user entered tolerance.

function ret = neville(x,y,xi,tol)

%neville’s method %inputs : x - vector of nodes % y - vector of function values at nodes % xi - location to interpolate % tol - tolerance used to terminate algorithm %output : yi - value of interpolation at xi

N = length(x); p = zeros(N); p(1,1) = y(1);

for i= 2:N p(i,1) = y(i);

for j=2:i num = (xi-x(i))p(i-1,j-1) - (xi-x(i-j+1))p(i,j-1); den = x(i-j+1) - x(i); p(i,j) = num/den; end %added code to terminate when error tolerance acheived if (abs(p(i,i)-p(i-1,i-1)) < tol) break; end end

ret = p;

Using the values in the table provide and the modified code with a specified tolerance of 10−^6 , we can evaluate Ei(1.625) = 0.0832168217387078. This used a 7th^ degree polynomial.

b)The differences in the diagonal elements of the Neville table are an order of magnitude greater than the actual error.

i p(i,i) w/o reordering p(i,i)-p(i-1,i-1) w/o reordering error 1 2.193839343955e-01 - 1.361671378514870e- 2 1.067749777377e-02 -2.087064366217e-01 7.253929877026284e- 3 1.060070334557e-01 9.532953568198e-02 2.279023691172216e- 4 7.845171204439e-02 -2.755532141136e-02 4.765084499639236e- 5 8.386051909522e-02 5.408807050835e-03 6.437225511963934e- 6 8.317063895927e-02 -6.898801359581e-04 4.615758476175791e- 7 8.321743206827e-02 4.679310900708e-05 6.355242453265353e- 8 8.321682173870e-02 -6.103295705217e-07 2.519467480477378e- c)After reordering the input data to put nodes closer to 1.625 earlier in the ordering, we approximate Ei(1.625) = 0.08321681815685121 with a 5th^ degree