Академический Документы
Профессиональный Документы
Культура Документы
1. Root finding
Find the root of the function f (x) =
error tolerance of 104 , using:
= 10^-4;
0;
1.2;
(a+b)/2;
% error tolerance
% 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
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
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
% 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
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
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
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.
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
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
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
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
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
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);
(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);
11
12