Numerical Analysis
Numerical Analysis
Numerical Analysis
9/28/16
9/28/16
Where,
Now we can see that the answer to the problem is the value of m that makes the function
equal to zero. Hence, we call this a roots problem.
9/28/16
9/28/16
5.4 Bisection
The first step in
bisection is to guess
two values of the
unknown (in the
present problem, m)
that give values for f
(m) with different
signs: [50, 200].
The initial estimate
of the root xr lies at
the midpoint of the
interval [50, 200].
xr=(50+200)/2=125
9/28/16
Note that the exact value of the root is 142.7376. This means
that the value of 125 calculated here has a true percent relative
error of
|t|=|( 125 - 142.7376)/142.7376|=12.43%
MEEN5313 Numerical Methods in ME
5.4 Bisection
Next we compute the product of
the function value at the lower
bound and at the midpoint:
f(50)f(125)=4.579(0.409) =
1.871>0
The process can be repeated to obtain refined estimates. For example, f (125)
f (162.5) = 0.409(0.359) = 0.147
Therefore, the root is now in the lower interval between 125 and 162.5. The
upper bound is redefined as 162.5, and the root estimate for the third
9/28/16
MEEN5313 Numerical Methods in ME
8
iteration is calculated as
5.4 Bisection
We might decide that we should terminate when the error
drops below, say, 0.5%. This strategy is flawed because the
error estimates in the example were based on knowledge of
the true root of the function, but this is never known.
One way to do this is by estimating an approximate percent
relative error as
9/28/16
Bisection example
Use bisection method to find the root of f(x)=sin(5x)+x2 near
-0.5, till a < 10-4.
MATLAB plot and fzero.
clear
f1=@(x) sin(5.*x)+x.^2;
x=-1.5:0.01:1.5; y=f1(x);
plot(x,y); grid on
xlabel('x'); ylabel('y'); title('sin(5x)+x^2')
[root_fzero,fx_fzero]=fzero(f1,-0.5);
root_fzero=-0.5637
fx_fzero=-1.1102e-16
Bisection hand
calculation:
9/28/16
Iteration xl
xu
xr
f(xl)
0.2188
8
0.2188
8
0.0671
8
0.0671
8
0.0300
f(xu)
f(xr)
0.3484
7
0.0791
6
0.0791
6
0.0067
8
0.0067
0.0791 1.0000
6
0
0.0671 0.0434
8
8
0.0067 0.0222
8
2
0.0300 0.0109
2
9
10
0.0115 0.0055
11
Second iteration:
f (xl ) f (xr ) = 2.592732
Therefore, the root lies in the first subinterval, and xr becomes the upper limit for the next iteration, xu =
176.2773.
xl = 50 f (xl) = 4.579387
xu = 176.2773
f (xu) = 0.566174
xr = 176.2773 0.566174(50 176.2773)/(4.579387 0.566174)= 162.3828
which has true and approximate relative errors of 13.76% and 8.56%, respectively. Additional iterations can be
performed to refine the estimates of the root.
9/28/16
12
13
xl
Iteration
1
2
3
4
5
6
7
9/28/16
xu
0.9
0.94986
0.97409
0.98306
0.98601
0.98695
0.98724
xr
1.1
1.1
1.1
1.1
1.1
1.1
1.1
f(xl)
f(xu)
f(xr)
a
0.94986 -0.16753 0.50446 -0.09708 0.052493
0.97409 -0.09708 0.50446 -0.03868 0.024875
0.98306 -0.03868 0.50446 -0.01308 0.009121
0.98601 -0.01308 0.50446 -0.00418 0.002998
0.98695 -0.00418 0.50446 -0.00131 0.000948
0.98724 -0.00131 0.50446 -0.00041 0.000296
0.98733 -0.00041 0.50446 -0.00013 9.22E-05
14
Open methods
Use only a single starting value or
two starting values that do not
necessarily bracket the root.
Sometimes diverge;
when the open methods converge,
they usually converge much faster
than bracketing methods.
9/28/16
15
(6.1)
17
9/28/16
18
19
6.2 NEWTON-RAPHSON
Commonly called Newtons method. The formula is
(6.6)
20
Solution:
The Newton-Raphson formula for this case is
21
22
(6.8)
(6.9)
= a small perturbation fraction.
9/28/16
23
clear
f=@(x) log(x) - 1./x;
x=0.5:0.01:3;
y=f(x);
plot(x,y)
grid on
xlabel('x')
ylabel('y')
title('y=ln(x)+1/x')
MATLAB plot
The root is located near 1.75 (initial
guess).
9/28/16
24
xr
Iteration
1.7708
1.759
1.7657
1.7618
1.764
1.7628
1.7635
1.7631
1.7633
1.7632
0.01175
-0.00673
0.003795
-0.00216
0.001223
-0.00069
0.000393
-0.00022
0.000127
-7.18E-05
1
2
9/28/16
xr
Iteration
1.7632
1.7632
0.00749
3.84E-5
25
Modified Secant:
Initial guess: 1.75
Delta: 0.01
Secant
Modified Secant
xr
Iteration
1
2
3
9/28/16
xr
Iteration
1.7637
0.00777
1.7632 -0.00025797
1.7632 8.3026E-06
1
2
a
1.7632
0.00749
1.7632 -1.2482e-05
26
MATLAB example
disp('Newton method')
tolerance = 1e-6;
a_error = 1;
x_old=100.0;
iter=0;
%univariate function
syms x
f(x) = sqrt (9.81/0.25*x) * tanh(4*sqrt(9.81*0.25/x)) -36;
df = diff(f,x);
27
28
Results
Newton method
1 131.2027 0.23782
2 141.8974 0.075369
3 142.7332 0.0058552
4 142.7376 3.1228e-05
5 142.7376 8.7898e-10
Secant method:
1 133.9019 0.1785
2 140.911 0.049741
3 142.6357 0.012092
4 142.7365 0.00070577
Procedure
initial
97.1716
96
94.3431
92
88.6863
84
77.3726
68
54.7452
search
search
search
search
search
search
search
search
search
-1.30864
-1.35638
-1.42563
-1.5272
-1.67864
-1.91001
-2.27705
-2.89551
-4.05173
102.828
104
105.657
108
111.314
116
122.627
132
145.255
-1.09143
-1.04902
-0.99043
-0.910256
-0.801928
-0.65804
-0.471206
-0.235622
0.0506855
2 141.8974 0.075369
3 142.7332 0.0058552
5 142.7376 8.23e-06
6 142.7376 5.2997e-09
Modified Secant method:
1 131.2027 0.23782
4 142.7376 3.1227e-05
5 142.7376 8.7287e-10
9/28/16
29
6.6 Polynomials
Polynomials are a special type of nonlinear algebraic equation of the general form
fn(x)=a1xn+a2xn-1++an-1x2+anx+an+1 (6.12)
where n is the order of the polynomial, and the as are constant coefficients.
30
MATLAB roots
f5(x) = x5 3.5x4 + 2.75x3 + 2.125x2 3.875x
+ 1.25
Solution. Polynomials are entered into
MATLAB by storing the coefficients as a row
vector. For example, entering the following
line stores the coefficients in the vector a:
% define the polynomial by its coefficient
x =
2.0000
-1.0000
1.0000
1.0000
0.5000
+
+
+
+
0.0000i
0.0000i
0.5000i
0.5000i
0.0000i
ans =
clear
1.0e-13 *
0.3042
0.0000
0.0067
0.0067
0.0022
x=roots(a)
polyval(a,x)
9/28/16
+
+
+
+
0.0000i
0.0000i
0.0289i
0.0289i
0.0000i
31
Chapter 7: Optimization
Objectives: how optimization can be used to determine minima and maxima of both onedimensional and multidimensional functions.
The determination of such extreme values is referred to as optimization.
9/28/16
32
Solution
This is actually the equation for the velocity. Find the root of
the above equation. Analytic solution is
9/28/16
33
(a) The initial step of the golden-section search algorithm involves choosing two
interior points according to the golden ratio. (b) The second step involves defining a
new interval that encompasses the optimum.
9/28/16
34
35
Iteration 2
d = 0.61803(2.4721 0) = 1.5279
x2 = 2.4721 1.5279 = 0.9443
The function evaluation at x2 is f(0.9943) = 1.5310. Since this value is less than the
function value at x1, the minimum is f(1.5279) = 1.7647, and it is in the interval prescribed by
x2, x1, and xu.
9/28/16
36
Note that the current minimum is highlighted for every iteration. The result is
converging on the true value of 1.7757 at x = 1.4276.
Golden-Section error
Where, xopt= x1, if f(x1)<f(x2), otherwise, xopt=x2.
9/28/16
37
38
39
Solution
x1= 1 f(x1) = 1.5829
x2= 1.5055 f(x2) = 1.7691
x3= 4 f(x3) = 3.1136
Then,
Then, f(1.4903)=-1.7714. The process can be repeated
within five iterations, the result is converging rapidly on the true value of
1.7757 at x = 1.4276.
9/28/16
40
9/28/16
41
g=9.81;v0=55;m=80;c=15;z0=100;
z=@(t) -(z0+m/c*(v0+m*g/c)*(1-exp(-c/m*t))m*g/c*t);
[x,f]=fminbnd(z,0,8)
x = 3.8317
f = -192.8609
9/28/16
42
Optimization example
Find the maxima of the following
function using golden search and
the parabolic interpolation.
clear
f = @(x) x.^(-x);
x=0.01:0.01:2;
y=f(x);
plot(x,y)
grid on
xlabel('x');
ylabel('y');
title('y=x^{1/x}')
s = 0.0001
9/28/16
43
Golden search
x2
f2
x1
f1
xu
xopt
a
Iteration xl
d
1
0.2 0.352786 -1.44421 0.447214 -1.43316
0.6 0.247214 0.352786 0.267661
2
0.2 0.294427 -1.43333 0.352786 -1.44421 0.447214 0.152786 0.352786 0.165424
3 0.294427 0.352786 -1.44421 0.388854 -1.44382 0.447214 0.094427 0.352786 0.102237
4 0.294427 0.330495 -1.44183 0.352786 -1.44421 0.388854 0.058359 0.352786 0.063186
5 0.330495 0.352786 -1.44421 0.366563 -1.44466 0.388854 0.036068 0.366563 0.037584
6 0.352786 0.366563 -1.44466 0.375078 -1.44457 0.388854 0.022291 0.366563 0.023228
7 0.352786 0.361301 -1.44458 0.366563 -1.44466 0.375078 0.013777 0.366563 0.014356
8 0.361301 0.366563 -1.44466 0.369815 -1.44466 0.375078 0.008514 0.366563 0.008872
9 0.361301 0.364553 -1.44465 0.366563 -1.44466 0.369815 0.005262 0.366563 0.005483
10 0.364553 0.366563 -1.44466 0.367805 -1.44467 0.369815 0.003252 0.367805 0.003377
11 0.366563 0.367805 -1.44467 0.368573 -1.44467 0.369815 0.00201 0.367805 0.002087
12 0.366563 0.367331 -1.44467 0.367805 -1.44467 0.368573 0.001242 0.367805 0.00129
13 0.367331 0.367805 -1.44467 0.368099 -1.44467 0.368573 0.000768 0.367805 0.000797
14 0.367331 0.367624 -1.44467 0.367805 -1.44467 0.368099 0.000474 0.367805 0.000493
15 0.367624 0.367805 -1.44467 0.367917 -1.44467 0.368099 0.000293 0.367917 0.000304
16 0.367805 0.367917 -1.44467 0.367987 -1.44467 0.368099 0.000181 0.367917 0.000188
17 0.367805 0.367875 -1.44467 0.367917 -1.44467 0.367987 0.000112 0.367875 0.000116
18 0.367805 0.367848 -1.44467 0.367875 -1.44467 0.367917 0.000069 0.367875 0.000072
44
Parabolic interpolation
Iteration x1
1
2
3
4
5
6
7
f1
x2
f2
x3
f3
x4
f4
a
0.2 -1.37973
0.4 -1.4427
0.6 -1.35866 0.385665 -1.44406
---0.2 -1.37973 0.385665 -1.44406
0.4 -1.4427 0.371376 -1.44464 -0.03848
0.2 -1.37973 0.371376 -1.44464 0.385665 -1.44406 0.369434 -1.44466 -0.00526
0.2 -1.37973 0.369434 -1.44466 0.371376 -1.44464 0.368251 -1.44467 -0.00321
0.2 -1.37973 0.368251 -1.44467 0.369434 -1.44466 0.368022 -1.44467 -0.00062
0.2 -1.37973 0.368022 -1.44467 0.368251 -1.44467 0.367917 -1.44467 -0.00028
0.2 -1.37973 0.367917 -1.44467 0.368022 -1.44467 0.367893 -1.44467 -6.7E-05
0.3679
-1.4447
45
46
Roots finding
Bracketing methods: bisection, false position
Open methods: Simple fixed-point, Newtons method, Secant
method, modified newtons method.
MATLAB: fzero, roots
Optimization:
1-D: golden-section, parabolic
MATLAB:
1-D: fminbnd;
2-D or multidimensional: fminsearch.
9/28/16
47