Вы находитесь на странице: 1из 13

NA, Lab work No 2

Luminita Leahu, FAF - 121


November, 2013

1. Root finding
Find the root of the function f (x) =
error tolerance of 104 , using:

x ex on the interval [0, 1.2] with

1. Bisection method, starting with a = 0 and b = 1.2


Code of program in MATLAB language:
err
a =
b =
c =

= 10^-4;
0;
1.2;
(a+b)/2;

% error tolerance

% compute the root

while ((b-c) > err)

% if (b-c) <= c accept c as root

funct = sqrt(c) - exp(-c);


if (sign(funct) <= 0)
a = c;
else
b = c;
end
c = (a+b)/2;

% compute function in c
% cheking sing of function
% if negative, assing c to a
% if positive, assing c to b

end
disp(c);

% display root

Results:
n
1
2
3
4
5
6
7
8
9
10
11
12
13
14

an
0
0
0.30000
0.30000
0.37500
0.41250
0.41250
0.42187
0.42187
0.42421
0.42539
0.42597
0.42626
0.426269

bn
1.20000
0.60000
0.60000
0.45000
0.45000
0.45000
0.43125
0.43125
0.42656
0.42656
0.42656
0.42656
0.42656
0.42641

cn
0.60000
0.30000
0.45000
0.37500
0.41250
0.43125
0.42187
0.42656
0.42421
0.42539
0.42597
0.42626
0.42641
0.42634

f (cn )
0.22578
-0.18309
0.03319
-0.07491
-0.19731
0.00699
-0.00629
0.000368
-0.00295
-0.00129
-0.00046
-0.00004
-0.00016
-0.00005

The root approaches value 0.426342773437500

bn cn
0.60000
0.30000
0.15000
0.07500
0.03750
0.18750
0.00937
0.00468
0.00234
0.00117
0.00058
0.00029
0.00014
0.00007

2. Newtons method, starting with x0 = 0.6


Code of program in MATLAB language:
err = 10^-4;
% error tolerance
x0 = 0.6;
% initial guess
funct1 = sqrt(x0) - exp(-x0);
% compute function(x0)
func_der = exp(-x0) + 1/(2*sqrt(x0)); % compute its derivative
x = x0 - funct1/func_der;
% root
while (abs(x-x0) > err)
x0=x;
% assing root to initial guess
funct = sqrt(x) - exp(-x);
% compute f in new guess
func_der = exp(-x) + 1/(2*sqrt(x)); % compute its derivative
xnew = x - funct/func_der;
% compute new root
x = xnew;
% assing new root to old one
end
disp(x)

% display the root

Results:
n
1
2
3
4

xn
0.6
0.41094921
0.42617277
0.42630274

fn
2.25E-1
- -2.19E-2
-1.84E-4
-1.31E-8

xn xn1
-1.89E-1
1.52E-2
1.29E-4

The root approaches value 0.426302751006863


3. Simplified Newtons method, starting with x0 = 0.6

4. Secant method, starting with x0 = 0.6 and x1 = 1


Code of program in MATLAB language:
x0 = 0.6;
x1 = 1;
err = 10^-4;

% first initial guess


% second initial guess
% error tolerance

while (abs(x1-x0) > err)


funct = sqrt(x1) - exp(-x1);
% compute f in x1
funct1 = sqrt(x0) - exp(-x0);
% compute f in x0
x_new = x1 - (funct*(x1 - x0))/(funct - funct1); % root
x0 = x1;
% assing x1 to first root
x1 = x_new;
% assing x_new to second one
end
disp(x1)

% display result

n
1
2
3
4
5
6

xn
1
0.6
0.42640286
0.42607934
0.42630276
0.42630275

fn
2.44701099
0.00014203
-0.00031691
1.74E-08
1.27E-08
-1.42E-009

xn xn1
-0.4
-0.17359
-3.23E-04
2.23e-004
-9.99E-09

The root approches value 0.426302751008356


Conclusion:
Now lets substitute the obtained roots in function, in order to see which
root is closer to real one:
Bisection x = 0.426342773437500 : function = 5.68e-5
Newton x = 0.426302751006863 : function = 3.33e-16
Secant x = 0.426302751008356 : function = 2.12e-12
So, as is seen, the Newton method approaches better 0, which means that
x = 0.426302751006863 is closer to real root. For this equation is also
better Newton mehtod because converges quicker the result , only in 4
steps, since Secant in 6 and Bisection - 14.

2. Root finding part II


Solve the equation using algorithms from previous problem
f (x) = x3 3x2 + 3x 1
for initial values: [0, 1.5], x0 = 10, x1 = 10 :

Given form
Reverse order
Nasted form

Bisection
1.000006914138794
1.000005483627319
0.99999976158142

Newton
0.999997000376625
0.999993108986103
0.999998507777507

Secant
0.999997000376625
0.999992458325684
0.999997761666261

for inital values: [0.2, 2.0], x0 = 0, x1 = 0.6 :

Given form
Reverse order
Nasted form

Bisection
1.000007820129395
1.000004673004150
0.999999523162842

Newton
0.999995652810177
0.999992693944920
0.999998454786652

Secant
0.999995652810177
0.999992100843405
0.999997682179978

for inital values: [0.6, 1.1], x0 = 1.1, x1 = 0.9 :

Given form
Reverse order
Nasted form

Bisection
1.000006294250488
1.000006294250488
1.000000572204590

Newton
1.000006252735082
1.000004883085882
1.000001760094580

Secant
1.000006252735082
1.000004883085882
1.000002640141866

Conclusion:
Looking at these results I denote that it is important in which form you
evaluate the function. Also is important the interval and initial guesses, if they
are closer to real root, the result is more precise. Newton and Secant method in
this problem, are better as in fisrt one, because approache root faster and closer
to real one. The more precise result are obtained for nasted form. ,because it
is seen that root is of multiplicity 3.

3. Fixed point functions


Compute fixed point for the following functions:
1. g(x) = 2ex with x0 = 0.8
x0 = 0.8;
for i=1:8
result = 2*exp(-x0);
disp(result);
x0 = result;
%root = 0.8526055
end

This function for small numbers of interations diverges the result, because
g 0 (0.8526055) = 0.8526055
Using active Aitkens extrapolation:

x0 = 0.8;
a = 0.8526055;
for i=1:2
result = 2*exp(-x0)
alpha = a - result;
result2 = 2*exp(-result)
alph = a - result2;
lamda = (result2 - result)/(result - x0);
result3 = result2 + (lambda/(1-lambda))*(result2 - result)
x0 = result3;
alpha = a - result3;
end

The root is generated only in 6 steps, the results are:

n
0
1
2
3
4
5
6
2. g(x) =

xn
0.80000
0.898658
0.814231
0.853163
0.852130
0.853011
0.8526055

xn
5.26E-2
-4.60E-2
3.83E-2
-5.57E-4
4.75E-4
-4.06E-4
-6.31E-8

0.9
with x0 = 0.75
1 + x4

x0 = 0.75;
for i=1:3
result = 0.9/(1+x0^4);
disp(result);
x0 = result;
%root = 0.7141900
end

This function for small numbers of interations diverges the result, because
g 0 (0.7141900) = 0.8258219
Using active Aitkens extrapolation:

x0 = 0.75;
a = 0.7141900;
for i=1:2
result = 0.9/(1+x0^4);
alpha = a - result;
result2 = 0.9/(1+result^4);
alph = a - result2;
lamda = (result2 - result)/(result - x0);
result3 = result2 + (lambda/(1-lambda))*(result2 - result);
x0 = result3;
alpha = a - result3;
end

The root is generated only in 9 steps, the results are:

n
0
1
2
3
4
5
6
7
8
9

xn
0.80000
0.683680
0.738625
0.713730
0.714571
0.713876
0.714190
0.7141901
0.7141899
0.7141900

xn
5.26E-2
3.05E-2
-2.44E-2
4.61E-4
-3.815E-4
3.14E-4
2.67E-8
-1.10E-7
2.93E-9
-4.80E-8

3. f (x) = 6.28 + sin(x) with x0 = 6


Code in MATLAB language:

x0 = 6;
for i=1:7
result = 6.28 + sin(x0);
err = result - x0;
res(i) = result;
% store values in a vector
disp(err);
x0 = result;
% root = 6.0155027
end
disp(res);
for i=3:7
lambda = (res(i) - res(i-1))/(res(i-1)-res(i-2)) % compute
esstimate = lambda/(lambda-1)*(res(i) - res(i-1)) % Aitkens error
end

Results:

n
0
1
2
3
4
5
6
7

xn
6.00000
6.00058
6.00115
6.00168
6.00220
6.00270
6.00318
6.00364

xn xn1

5.85E-4
5.62E-4
5.39E-4
5.18E-4
4.97E-4
4.78E-4
4.60E-4

0.9603
0.9604
0.9606
0.9607
0.9609
0.9610

xn
1.55E-4
1.49E-4
1.44e-4
1.38E-4
1.33E-4
1.28E-4
1.23E-4
1.19E-4

Estimate

-1.36E-4
-1.31E-4
-1.26E-4
-1.22E-4
-1.17E-4
-1.13E-4

In table I presented only 7 interations, because function converge the root


very slow, because g 0 (6.0155027) = 0.9644 . Also the difference between real
root and finded one is big enought. In order to get faster the real root lets
repeat the problem using active Aitkens extrapolation.
x0 = 6;
while (x0 < 6.0155007)
result = 6.28 + sin(x0);
result2 = 6.28 + sin(result);
lamda = (result2 - result)/(result - x0);
result3 = result2 + (lambda/(1-lambda))*(result2 - result);
x0 = result3;
disp(result);
alph = 6.0155027 - result
disp(result2);
alph = 6.0155027 - result2
disp(result3);
alph = 6.0155027 - result3
end

Results:
n
0
1
2
3
4
5
6

xn
6.00000
6.00058
6.00115
6.01471
6.01473
6.01476
6.01550

xn xn1
5.85E-4
5.62E-4
1.36E-2
2.00E-5
2.99E-5
7.40E-4

xn
1.55E-4
1.49E-4
1.44E-4
7.98E-6
7.69E-6
7.42E-6
1.99E-6

So, using Aitkens extrapolation, aproximation of real root is generated in


only 6 steps.
8

5. Bank
Loan consists of:
a : amount that is borrowed
r : interest rate (%)
n : number of years
p : payment per year
Using these datas, the formula which allows us to calculate the total amount
left to pay after n yers is:
a(1 + r)n p

(1 + r)n 1
r

Tasks:
1. The boss is pondering a loan of $ 100,000 at 6% rate with $ 10,000 yearly
payments. How long will it take to pay off the loan?
Solution:
It needs to find the number of year, n, in which the payment will be pay off.
In order to solve it directly I have to :
1. Write a formula which determines how much 6% of total amount, a, conar
)
sists (percentages =
100
2. Extract from total amount, annual payment (a - p)
3. Add to result obtained from preveous step, the result from step 1, i.e. Im
adding percentages (a - p + percentages )
4. Assing to old amount, remainded one, from preveous step .
5. Compute new percentages from new amount and repeat steps, while remained amount is greater than 0.
6. Incriment one value, n, that stores number of years. (n = n+1)
7. Thats all!
Here is the Code in MATLAB
a = 100000;
p = 10000;
r = 6;
percentage = (a*r)/100;
result = a;

n=0;
while(result>0)
result = a - p + percentage;
a = result;
percentage = (a*r)/100;
n=n+1;
end
disp(n);

% n = 16

Now lets check the initial formula, for this task. In order to do this I
substituted n = 16 , if the result will be equal to 0 , then its true.
a(1 + r)1 6 p

(1 + r)1 6 1
= 2.6901e + 003
r

So, this means that obtained number of years is rounded, and real value is
15 years and some months. In orer to obtain this exact term, it is better to use
one of performed in exercice 1 root finding algorithms. Secant and Newton ware
the better ones. Ill choose Secant method, in order to avoid need to calculate
the derivate of such a big function, if n is like power Ill need ln , so me do not
like this. (note that rate is not in percentage, but as 0.06)
x0 = 0.6;
x1 = 1;
eps = 10^-4;
while (abs(x1-x0) > eps)
funct = a*(1 + r)^x1 - p*(((1 + r)^x1 - 1)/r);
funct1 = a*(1 + r)^x0 - p*(((1 + r)^x0 - 1)/r);
x_new = x1 - (funct*(x1 - x0))/(funct - funct1);
x0 = x1;
x1 = x_new;
end
disp(x1);

It this case n = 15.7252 its like 15 years and 7 months.


Now computing the formula :
a(1 + r)1 6 p

(1 + r)1 6 1
= 0.0830
r

2. For a loan of $100,000 how large must the yearly payments be for the loan
to be paid off in 20 years at 6% interest ?
10

Solution:
For this task the Newton method can not be applied, because derivative is
constant and it generate at infinite.
x1 =1;
x0 = 0.6;
eps = 10^-4;
a = 100000;
n = 20 ;
r = 0.06;
while (abs(x1-x0) > eps)
funct = a*(1 + r)^n - x1*(((1 + r)^n - 1)/r);
funct1 = a*(1 + r)^n - x0*(((1 + r)^n - 1)/r);
x_new = x1 - (funct*(x1 - x0))/(funct - funct1);
x0 = x1;
x1 = x_new;
end
disp(x1);

The result is: $8718.5


2. For a loan of $100,000 and yearly payment of $10,000,what interest rate
is required for the loan to pe paid in 20 years?
Solution:
x1 =1;
x0 = 0.6;
eps = 10^-4;
a = 100000;
n = 20 ;
p = 10000;
while (abs(x1-x0) > eps)
funct = a*(1 + x1)^n - p*(((1 + x1)^n - 1)/x1);
funct1 = a*(1 + x0)^n - p*(((1 + x0)^n - 1)/x0);
x_new = x1 - (funct*(x1 - x0))/(funct - funct1);
x0 = x1;
x1 = x_new;
end
disp(x1);

11

The result is: 0.0775 , wich means r = 7.75%


Conclusion:
Root finding is a usefull tool, which can be used in banking! So, even you
are an economist learn NA :) !

12

Вам также может понравиться