0% found this document useful (0 votes)
18 views

W2 Tute2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

W2 Tute2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

DEPARTMENT OF MATHEMATICS AND STATISTICS

FACULTY OF HUMANITIES AND SCIENCES

MA3223 – Numerical Methods

Homework 2 – Convergence, Algorithms, Bisection Method - SOLUTIONS

1. The sequence {𝐹 } described by 𝐹 = 1 , 𝐹 = 1, and 𝐹 = 𝐹 + 𝐹 if 𝑛 > 0, is called the Fibonacci


sequence. Its terms occur naturally in many botanical species, particularly those with petals or scales arranged in
the form of a logarithmic spiral.

a. Consider the sequence {𝑥 }, where 𝑥 = . Assuming that lim 𝑥 = 𝑥 exists, show that 𝑥 = .

This number is called the golden ratio.
√ √
b. Show that the Fibonacci sequence also satisfies the equation 𝐹 ≡ 𝐹 = −

Solution:
a. Since 𝑥 = , we have 𝑥 = = =1+ =1+

Considering the limit as n becomes large, lim 𝑥 = lim 1 + =1+


→ →

But lim 𝑥 = lim 𝑥 =𝑥


→ →

So, we have 𝑥 = 1 + → 𝑥 −𝑥−1=0

( )± ( ) ∙ ∙( ) ±√
Solving for 𝑥: 𝑥 = =

The Fibonacci sequence has all positive terms so all by definition of 𝑥 , 𝑥 ≥ 0 for all 𝑛 ≥ 0.

So, 𝑥 = .

√ √
b. Let 𝐹 = − .

√ √
𝑘 = 0: 𝐹 = − = [1 − 1] = 0
√ √

√ √ √ √ √
𝑘 = 1: 𝐹 = − = = =1
√ √ √

√ √ √ ( √ ) √
𝑘 = 2: 𝐹 = − = = =1
√ √ √

√ √
For ease of computing, let 𝑢 = and 𝑣 = . Then you can write 𝐹 = [𝑢 − 𝑣 ]

Consider
1 1
𝐹 +𝐹 = [𝑢 −𝑣 ]+ [𝑢 − 𝑣 ]
√5 √5
1
= [𝑢 +𝑢 −𝑣 −𝑣 ]
√5
1 1 1 1 1
= 𝑢 + −𝑣 +
√5 𝑢 𝑢 𝑣 𝑣
1 1 1 1 1
= 𝑢 + −𝑣 +
√5 𝑢 𝑢 𝑣 𝑣
1
= [𝑢 −𝑣 ]
√5
=𝐹

So, we have that 𝐹 ≡ 𝐹 for all 𝑛 > 0.

2. Find the rates of convergence of the following sequence as 𝑛 → ∞.


a. lim sin = 0 (Please correct the typo in the original: Should be sin instead of .)

b. lim sin =0

Solutions: Use the definition of rate of convergence

a. We know that sin 𝑥 ≤ 𝑥 for 0 ≤ 𝑥 ≤ 1 ⟹ sin ≤ for 𝑛 > 0

Then, sin − 0 = sin ≤ for 𝑛 > 0

So, sin = 𝑂 . That is, sin converges to 0 with rate of convergence 𝑂

b. We know that sin 𝑥 ≤ 𝑥 for 0 ≤ 𝑥 ≤ 1 ⟹ sin ≤ for 𝑛 > 0

Then, sin − 0 = sin ≤ for 𝑛 > 0

So, sin =𝑂 . That is, sin converges to 0 with rate of convergence 𝑂

3. Find the rates of convergence of the following sequence as ℎ → 0.


( )
a. lim =1

( )
b. lim =0

Solutions: Use the definition of rate of convergence


( ) ( )( ) ( )( )
a. Consider the Maclaurin series: 𝑓(𝑥) = 𝑓(0) + 𝑓 (0)𝑥 + 𝑥 + 𝑥 + ⋯+ 𝑥 +⋯
! ! !

So, sin 𝑥 = sin 0 + cos 0 𝑥 − 𝑥 − 𝑥 + 𝑥 + 𝑥 +⋯=𝑥− + −⋯

( ) ⋯
Consider −1 = −1 = 1− + −⋯ −1 = − +⋯

( )
So, = 1 + 𝑂(ℎ )

b. cos 𝑥 = cos 0 − sin 0 𝑥 − 𝑥 + 𝑥 + 𝑥 − 𝑥 −⋯= 1− + −⋯

So, 1 − cos 𝑥 = − +⋯

( ) ⋯
Consider −0 = = − +⋯

( )
So, = 𝑂(ℎ)

4. Construct an algorithm that has as input an integer 𝑛 ≥ 1, numbers 𝑥 , 𝑥 , … , 𝑥 , and a number 𝑥 and that
produces as output the product (𝑥 − 𝑥 )(𝑥 − 𝑥 ) ∙∙∙ (𝑥 − 𝑥 ).
Solution:
INPUT: 𝑛, 𝑥, 𝑥 , 𝑥 , … , 𝑥
OUTPUT: 𝑝𝑟𝑜𝑑

STEP 1: Set 𝑃𝑟𝑜𝑑 = 𝑥 − 𝑥 ;


𝑖 = 1;
STEP 2: While 𝑝𝑟𝑜𝑑 ≠ 0 and 𝑖 ≤ 𝑛
Set 𝑝𝑟𝑜𝑑 = 𝑝𝑟𝑜𝑑 ∙ (𝑥 − 𝑥 );
𝑖 = 𝑖 + 1;
STEP3: OUTPUT (𝑝𝑟𝑜𝑑);
STOP.

Example Octave code:


5. a. How many multiplications and additions are required to determine a sum of the form:

𝑎𝑏

b. Modify this sum expression to an equivalent form that reduces the number of computations.
Solution:

a. For each 𝑖 value,


For the inner summation ∑ 𝑎 𝑏 , we are multiplying 𝑎 by each 𝑏 for 𝑗 = 1: 𝑖. So, we have 𝑖
multiplications. Then, we are adding each 𝑎 𝑏 value for 𝑗 = 1: 𝑖. So, we have (𝑖 − 1) additions.

Example for the inner summation:


For 𝑖 = 1, we have just 1 multiplication and no additions (𝑎 𝑏 ).
For 𝑖 = 2, we have 2 multiplications and 1 addition (𝑎 𝑏 + 𝑎 𝑏 ).
For 𝑖 = 3, we have 3 multiplications and 2 additions (𝑎 𝑏 + 𝑎 𝑏 + 𝑎 𝑏 )

Since 𝑖 = 1: 𝑛,
( )
we have the total number of multiplications = ∑ (𝑖) =

( )
Similarly, for the inner sum, we need total number of additions = ∑ (𝑖 − 1) =
Then we need another (n-1) additions to add these sums together.
( ) ( ) ( ) ( )( )
So, total number of additions = +𝑛−1 = =

Example:
∗ ∗
For 𝑛 = 1, we have just = 1 multiplication and = 0 additions. (𝑎 𝑏 )
∗ ∗
For 𝑛 = 2, we have = 3 multiplications and = 2 addition. (𝑎 𝑏 + 𝑎 𝑏 + 𝑎 𝑏 )
∗ ∗
For 𝑛 = 3, we have = 6 multiplications and = 5 additions (𝑎 𝑏 + 𝑎 𝑏 + 𝑎 𝑏 + 𝑎 𝑏 + 𝑎 𝑏 + 𝑎 𝑏 )

b. The modified form:

𝑎𝑏 = 𝑎 𝑏

For each 𝑖, ∑ 𝑏 will have (𝑖 − 1) additions. 𝑎 ∑ 𝑏 will add on just 1 multiplication to the term
(∑ 𝑏 ).
So, total number of multiplications = ∑ (1) = 𝑛
( )( )
Total number of additions = (∑ (𝑖 − 1)) + (𝑛 − 1) =

6. Assume that,
1 − 2𝑥 2𝑥 − 4𝑥 4𝑥 − 8𝑥 1 + 2𝑥
+ + +⋯= for 𝑥 < 1, and let 𝑥 = 0.025.
1−𝑥+𝑥 1−𝑥 +𝑥 1−𝑥 +𝑥 1+𝑥+𝑥

Write and execute an algorithm that determines the number of terms needed on the left side of the equation so
that the left side differs from the right side by less than 10 .
7. Let 𝑓(𝑥) = (𝑥 + 2)(𝑥 + 1 ) 𝑥(𝑥 − 1 ) (𝑥 − 2). To which zero of 𝑓 does the Bisection method converge when
applied on the following intervals?
a. [−1.5, 2.5]
b. [−0.5, 2.4]
c. [−0.5, 3]
d. [−3, −0.5]
Solution:
a. 0
b. 0
c. 2
d. -2

Use algorithm to see which zero it converges to. The possible zeroes are -2, -1, 0, 1, or 2.

8. a. Sketch the graphs of 𝑦 = 𝑒 − 2 and 𝑦 = cos (𝑒 − 2).


b. Use the Bisection method to find an approximation to within 10 to a value in [0.5, 1.5] with
𝑒 − 2 = cos (𝑒 − 2).

Solution:

Graphs of 𝑦 = 𝑒 − 2 and 𝑦 = cos (𝑒 − 2) Graph of 𝑦 = cos(𝑒 − 2) −𝑒 + 2


Iteration 1: a = 0.500000, p = 1.000000, b = 1.500000
Iteration 2: a = 1.000000, p = 1.250000, b = 1.500000
Iteration 3: a = 1.000000, p = 1.125000, b = 1.250000
Iteration 4: a = 1.000000, p = 1.062500, b = 1.125000
Iteration 5: a = 1.000000, p = 1.031250, b = 1.062500
Iteration 6: a = 1.000000, p = 1.015625, b = 1.031250
Iteration 7: a = 1.000000, p = 1.007812, b = 1.015625
Iteration 8: a = 1.000000, p = 1.003906, b = 1.007812
Iteration 9: a = 1.003906, p = 1.005859, b = 1.007812
Iteration 10: a = 1.005859, p = 1.006836, b = 1.007812
Iteration 11: a = 1.006836, p = 1.007324, b = 1.007812
Iteration 12: a = 1.007324, p = 1.007568, b = 1.007812
Iteration 13: a = 1.007568, p = 1.007690, b = 1.007812
Iteration 14: a = 1.007568, p = 1.007629, b = 1.007690
Iteration 15: a = 1.007568, p = 1.007599, b = 1.007629
Iteration 16: a = 1.007599, p = 1.007614, b = 1.007629
Iteration 17: a = 1.007614, p = 1.007622, b = 1.007629

Approximate root: 1.0076

9. Let 𝑓(𝑥) = (𝑥 − 1) , 𝑝 = 1, and 𝑝 = 1 + . Show that |𝑓(𝑝 )| < 10 whenever 𝑛 > 1 but that |𝑝 − 𝑝 | <
10 requires that 𝑛 > 1000.

Solution:
For 𝑛 > 1, |𝑓(𝑝 )| = 1+ −1 = = ≤ = < 10

1 1
|𝑝 − 𝑝 | = 1 − 1 − = < 10 ⟹ 𝑛 > 10 = 1000
𝑛 𝑛

10. A trough of length L has a cross section in the shape of a semicircle with radius r. When filled with water to
within a distance h of the top, the volume V of water is given by:

Suppose 𝐿 = 10 𝑓𝑡, 𝑟 = 1 𝑓𝑡, and 𝑉 = 12.4 𝑓𝑡 . Find the depth of water in the trough to within 0.01 𝑓𝑡.

Solu on:
Solve for the equa on 𝑉 − 𝐿 0.5𝜋𝑟 − 𝑟 𝑎𝑟𝑐𝑠𝑖𝑛 ℎ 𝑟 − ℎ(𝑟 − ℎ ) = 0 for h.
Itera on 1: a = 0.000000, p = 0.500000, b = 1.000000
Itera on 2: a = 0.000000, p = 0.250000, b = 0.500000
Itera on 3: a = 0.000000, p = 0.125000, b = 0.250000
Itera on 4: a = 0.125000, p = 0.187500, b = 0.250000
Itera on 5: a = 0.125000, p = 0.156250, b = 0.187500
Itera on 6: a = 0.156250, p = 0.171875, b = 0.187500
Itera on 7: a = 0.156250, p = 0.164062, b = 0.171875
Itera on 8: a = 0.164062, p = 0.167969, b = 0.171875

Approximate root: 0.1660

Depth of the water = 𝑟 − ℎ = 1 − 0.1660 = 0.8340

11. Create an octave func on to find the root of a func on given the interval the root exists in.

Bisec on Method: Save this script as bisec on_method.m

function root = bisection_method(f, a, b, tol, max_iterations)


% func: The function whose root is to be found.
% a: Lower bound of the interval.
% b: Upper bound of the interval.
% tol: Tolerance level (the desired accuracy of the root).
% max_iterations: Maximum number of iterations allowed.

% Check if the function values at a and b have opposite signs


if f(a) * f(b) >= 0
error('The function values at "a" and "b" must have opposite signs for the bisection method to work.');
end

% Perform the bisection iterations


for i = 1:max_iterations
% Calculate the midpoint of the interval
p = (a + b) / 2;

% Check if the midpoint is the root (within the tolerance)


if abs(f(p)) < tol
root = p;
% The return statement simply makes it easier to exit a function from a deeply nested loop or conditional statement.
return;
end

% Display iteration information


fprintf('Iteration %d: a = %f, p = %f, b = %f\n', i, a, p, b);

% Update the interval for the next iteration


if f(a) * f(p) < 0
b = p;
else
a = p;
end
end

% If max_iterations is reached and the root is not found within the tolerance
warning('Maximum number of iterations reached. The root may not be found within the desired tolerance.');
root = p;

endfunction
To use this func on, you can run the following script:

% Define the function for which we want to find the root.


func = @(x) (x+2)*((x+1 )^2)*x*((x-1)^3)*(x-2);
a = 0; % Lower bound of the interval.
b = 1; % Upper bound of the interval.
tol = 10^-2; % Tolerance (stop when the interval becomes smaller than tol).
max_iter = 100; % Maximum number of iterations.

root = bisection_method(func, a, b, tol, max_iter);


disp("Approximate root: ");
disp(root);

You might also like