2993 spring 2005
TRANSCRIPT
-
7/25/2019 2993 spring 2005
1/185
Numerical Methods for Engineers13.002
Introduction to Numerical Analysis fo
Fundamentals of Digital Compu Digital Computer Models
Convergence, accuracy and stabilit
Number representation
Arithmetic operations Recursion algorithms
Error Analysis Error propagation numerical stab
Error estimation
Error cancellation Condition numbers
-
7/25/2019 2993 spring 2005
2/185
Numerical Methods for Engineers13.002
Digital Computer Models
x
w(x,t)
xw(x,t)
n n
xn
m
Continuous Model
Discrete Model
Different
Differenc
System o
Linear Syste
Eigenval
Non-triv
Accuracy and Sta
-
7/25/2019 2993 spring 2005
3/185
Numerical Methods for Engineers13.002
m M
b Be E
Floating Number Representati
Examples
Convention
Decimal
Binary
Decimal
Binary
General
Max mantissa
Min mantissa
Max exponent
Min exponent
-
7/25/2019 2993 spring 2005
4/185
Numerical Methods for Engineers13.002
Arithmetic Operations
Number Representation
Absolute Error
Relative Error
Addition
Multiplic
R
A
R
Shift mantis
Result has ex
-
7/25/2019 2993 spring 2005
5/185
Numerical Methods for Engineers13.002
Recursion
Numerically evaluate square-root
Initial guess
Test
Mean of guess and its reciprocal
Recursion Algorithm
a=26;
n=10;
g=1;
sq(1)=g;for i=2:n
sq(i)= 0.5*(
end
hold off
plot([0 n],[s
hold on
plot(sq,'r')
plot(a./sq,'r
plot((sq-sqrt
grid on
-
7/25/2019 2993 spring 2005
6/185
Numerical Methods for Engineers13.002
Recursion
Horners Scheme
% Horners
% for eva
a=[ 1 2 3
n=length
z=1;
b=a(1);
% Note in
for i=1:n
b=a(i
end
p=b
Evaluate polynomial
Horners Scheme
General order n
Recurrence relation
-
7/25/2019 2993 spring 2005
7/185
Numerical Methods for Engineers13.002
Recursion
Order of Operations Matter
0 1
N=20; sum=0; sumr=0;
b=1; c=1; x=0.5;
xn=1;
% Number of significant dig=2;
ndiv=10;
for i=1:N
a1=sin(pi/2-pi/(ndiv*i))
a2=-cos(pi/(ndiv*(i+1)))
% Full matlab precision
xn=xn*x;
addr=xn+b*a1;
addr=addr+c*a2;
ar(i)=addr;
sumr=sumr+addr;
z(i)=sumr;
% additions with dig sig
add=radd(xn,b*a1,dig);
add=radd(add,c*a2,dig);
% add=radd(b*a1,c*a2,dig
% add=radd(add,xn,dig);
a(i)=add;
sum=radd(sum,add,dig);
y(i)=sum;
end
sumr
' i delta
res=[[1:1:N]' ar' z' a'
hold off
a=plot(y,'b'); set(a,'Li
hold on
a=plot(z,'r'); set(a,'Li
a=plot(abs(z-y)./z,'g');
legend([ num2str(dig) '
Result of small, but significantterm destroyed by subsequent
addition and subtraction of almost
equal, large numbers.
Remedy:
Change order of additions
-
7/25/2019 2993 spring 2005
8/185
Numerical Methods for Engineers13.002
recur.m
>> recur
b = 1; c = 1; x = 0.5;
dig=2
i delta Sum delta(approx) Sum(approx)
res =
1.0000 0.4634 0.4634 0.5000 0.5000
2.0000 0.2432 0.7065 0.2000 0.7000
3.0000 0.1226 0.8291 0.1000 0.8000
4.0000 0.0614 0.8905 0.1000 0.9000
5.0000 0.0306 0.9212 0 0.90006.0000 0.0153 0.9364 0 0.9000
7.0000 0.0076 0.9440 0 0.9000
8.0000 0.0037 0.9478 0 0.9000
9.0000 0.0018 0.9496 0 0.9000
10.0000 0.0009 0.9505 0 0.9000
11.0000 0.0004 0.9509 0 0.9000
12.0000 0.0002 0.9511 0 0.9000
13.0000 0.0001 0.9512 0 0.9000
14.0000 0.0000 0.9512 0 0.9000
15.0000 0.0000 0.9512 0 0.9000
16.0000 -0.0000 0.9512 0 0.900017.0000 -0.0000 0.9512 0 0.9000
18.0000 -0.0000 0.9512 0 0.9000
19.0000 -0.0000 0.9512 0 0.9000
20.0000 -0.0000 0.9512 0 0.9000
-
7/25/2019 2993 spring 2005
9/185
Numerical Methods for Engineers13.002
Introduction to Numerical AnEngineers
Fundamentals of Digital Compu Digital Computer Models
Convergence, accuracy and stabilit
Number representation
Arithmetic operations Recursion algorithms
Error Analysis Error propagation numerical stab
Error estimation
Error cancellation Condition numbers
-
7/25/2019 2993 spring 2005
10/185
Numerical Methods for Engineers13.002
m M
b Be E
Floating Number Representati
Examples
Convention
Decimal
Binary
Decimal
Binary
General
Max mantissa
Min mantissa
Max exponent
Min exponent
-
7/25/2019 2993 spring 2005
11/185
Numerical Methods for Engineers13.002
Error Analysis
Number Representation
Absolute Error
Relative Error
Addition
Multiplic
R
A
R
Shift mantis
Result has ex
-
7/25/2019 2993 spring 2005
12/185
Numerical Methods for Engineers13.002
Error Propagation
Spherical Bessel Functions
Differential Equation
Solutions
-
7/25/2019 2993 spring 2005
13/185
Numerical Methods for Engineers13.002
Error Propagation
Spherical Bessel Functions
Forward Recurrence
Backward Recurrence
Forward Recurrence
Millers algorithm
N ~ x+20
-
7/25/2019 2993 spring 2005
14/185
Numerical Methods for Engineers13.002
Error Propagation
Eulers MethodDifferential Equation
Example
Discretization
Finite Difference (forward)
Recurrence
Central Finite Difference
-
7/25/2019 2993 spring 2005
15/185
Numerical Methods for Engineers13.002
Error Propagation
Absolute Errors
Function of one variable
General Error Propagation Formula
-
7/25/2019 2993 spring 2005
16/185
Numerical Methods for Engineers13.002
Error Propagation
Example
Multiplication
=>
=>
=>
=>
Error Pr
Relative Errors Add for Multiplication
-
7/25/2019 2993 spring 2005
17/185
Numerical Methods for Engineers13.002
Error Propagation
Expectation of Errors
Truncation
Error Expectation
Rounding
Addition
Sta
Standard
of e
-
7/25/2019 2993 spring 2005
18/185
Numerical Methods for Engineers13.002
Error Propagation
Error Cancellation
Function of one variable
Max. error
Stand. errorE
-
7/25/2019 2993 spring 2005
19/185
Numerical Methods for Engineers13.002
Error Propagation
Condition Number
x = x(1 + )
xx
y = y(1
yy
y = f(x)
Problem
Error canc
Problem Condition Number
Well-cond
-
7/25/2019 2993 spring 2005
20/185
Numerical Methods for Engineers13.002
Error Propagation
Condition NumberProblem Condition Number
4 Significant Digits
Algorithm Condition Number
K is algorit
may be muc
to limited nu
Solution
Higher pre
Rewrite alg
A
Well-con
-
7/25/2019 2993 spring 2005
21/185
Numerical Methods for Engineers13.002
Introduction to Numerical Analysis fo
Systems of Linear Equations
Mathews Cramers Rule
Gaussian Elimination Numerical implementation
3.3-3.4
Numerical stability
Partial Pivoting Equilibration
Full Pivoting
Multiple right hand sides
Computation count
LU factorization
Error Analysis for Linear Systems
Condition Number
Special Matrices
Iterative Methods Jacobis method
Gauss-Seidel iteration
Convergence
-
7/25/2019 2993 spring 2005
22/185
Numerical Methods for Engineers13.002
Systems of Linear EquationCramers Rule
Cramers Rule, n=2
Linear System of Equations
Exa
Cramers rule inco
-
7/25/2019 2993 spring 2005
23/185
Numerical Methods for Engineers13.002
Systems of Linear EquationGaussian Elimination
Red
StLinear System of Equations
-
7/25/2019 2993 spring 2005
24/185
Numerical Methods for Engineers13.002
Systems of Linear EquationGaussian Elimination
Reduction
Step 1
i
j
-
7/25/2019 2993 spring 2005
25/185
Numerical Methods for Engineers13.002
Systems of Linear EquationGaussian Elimination
Re
StReduction
Step k
Back-S
-
7/25/2019 2993 spring 2005
26/185
Numerical Methods for Engineers13.002
Systems of Linear EquationGaussian Elimination
Pivotal Elements
Step k Partial Pivo
Row k
Row i
Required at each step!
-
7/25/2019 2993 spring 2005
27/185
Numerical Methods for Engineers13.002
Systems of Linear EquationGaussian Elimination
Pivotal Elements
Reduction
Step kPartial Pivo
New Row k
New Row i
Required at each step!
-
7/25/2019 2993 spring 2005
28/185
Numerical Methods for Engineers13.002
Systems of Linear EquationGaussian Elimination
Example, n=2
Cramers Rule - Exact
Gaussian
2-dig
1% error
100% error
n=3
a = [ [0.01 1.0]' [-1.0 0.01]']
b= [1 1]'
r=a^(-1) * b
x=[0 0];m21=a(2,1)/a(1,1);
a(2,1)=0;
a(2,2) = radd(a(2,2),-m21*a(1,2),n);
b(2) = radd(b(2),-m21*b(1),n);
x(2) = b(2)/a(2,2);
x(1) = (radd(b(1), -a(1,2)*x(2),n))/a(1,1);
x'
tbt.m
tbt.m
-
7/25/2019 2993 spring 2005
29/185
Numerical Methods for Engineers13.002
Systems of Linear EquationGaussian Elimination
Example, n=2
Cramers Rule - Exact
Partial Piv
Interc
2-dig
1% error
1% error
-
7/25/2019 2993 spring 2005
30/185
Numerical Methods for Engineers13.002
Systems of Linear EquationGaussian Elimination
Example, n=2
Cramers Rule - Exact
2-dig
Multiply E
1% error
100% error
Infinity-NoEquations must be normalized for
partial pivoting to ensure stability
ThisEquilibration is made by
normalizing the matrix to unit norm
Two-Nor
-
7/25/2019 2993 spring 2005
31/185
Numerical Methods for Engineers13.002
Systems of Linear EquationGaussian Elimination
Example, n=2
Cramers Rule - Exact
Interchan
2-digit A
Pivotin
Full Pivoting
Find largest numerical value in same row and column
Affectsordering of unknowns
-
7/25/2019 2993 spring 2005
32/185
Numerical Methods for Engineers13.002
Partial Pivoting
Equilibrate system of equations
Pivoting by Columns
Simple book-keeping
Solution vector in original order
Full Pivoting
Does not require equilibration
Pivoting by both row and columns
More complex book-keeping Solution vector re-ordered
Systems of Linear EquationGaussian Elimination
Numerical Stability
Partial Pivoting is simplest and most co
Neither method guarantees stabilit
-
7/25/2019 2993 spring 2005
33/185
Numerical Methods for Engineers13.002
Systems of Linear EquationGaussian Elimination
Example, n=2
Cramers Rule - Exact
Variable Tra
2-digit A
-
7/25/2019 2993 spring 2005
34/185
Numerical Methods for Engineers13.002
System of equations must be well co
Investigate condition number
Tricky, because it requires matrix inversi
Consistent with physics
E.g. dont couple domains that are physi
Consistent units
E.g. dont mix meter and m in unknown
Dimensionless unknowns
Normalize all unknowns consistently
Equilibration and Partial Pivoting, or
Systems of Linear EquationGaussian Elimination
How to Ensure Numerical Stab
-
7/25/2019 2993 spring 2005
35/185
Numerical Methods for Engineers13.002
Introduction to Numerical Analysis fo
Systems of Linear Equations
Cramers Rule
Gaussian Elimination Numerical implementation
3.3-3.4
Numerical stability
Partial Pivoting Equilibration
Full Pivoting
Multiple right hand sides
Computation count
LU factorization
Error Analysis for Linear Systems
Condition Number
Special Matrices
Iterative Methods Jacobis method
Gauss-Seidel iteration
Convergence
-
7/25/2019 2993 spring 2005
36/185
Numerical Methods for Engineers13.002
Systems of Linear EquationGaussian Elimination
k pn-k
Reduction
Step k
n
k
Compu
Reduc
Total Com
Re
Back
Multiple Right-hand Sides
Reduction for each right-hand side inefficient.
However, RHS may be result of iteration and unknown a priori
(e.g. Eulers method) ->LU Factorization
-
7/25/2019 2993 spring 2005
37/185
Numerical Methods for Engineers13.002
Systems of Linear EquationLU Factorization
The coefficient Matrix A is decomposed as
where is a lower triangular matrix
and is an upper triangular matrix
Then the solution is performed in two simple steps
1.
2.
Forward substitution
Back substitution
How to determine and ?
-
7/25/2019 2993 spring 2005
38/185
Numerical Methods for Engineers13.002
After reduction step i-1:
i
j
Above and on diagonal
Below diagonal
Unchanged after step i-1
Become and remain 0 in step j
Systems of Linear EquationLU Factorization
Change in re
Total chan
Total cha
-
7/25/2019 2993 spring 2005
39/185
Numerical Methods for Engineers13.002
Systems of Linear EquationLU Factorization
Matrix product
= xi
j
Below diagonal
k
k
Sum stops at diagonal
00
= x
i
j
Above diagonal
k
k
00
Lower triangular
0
0
-
7/25/2019 2993 spring 2005
40/185
Numerical Methods for Engineers13.002
Systems of Linear EquationLU Factorization
=
=
Upper triangular
Lower triangular
GE Reduction directly yields LU factorization
Comp
Lower diag
-
7/25/2019 2993 spring 2005
41/185
Numerical Methods for Engineers13.002
Systems of Linear EquationPivoting in LU Factorizatio
Before reduction, step k
Pivoting if
else
Interchange rowsi andk
Pivot element vector
Forward s
Intercha
-
7/25/2019 2993 spring 2005
42/185
Numerical Methods for Engineers13.002
Linear Systems of EquationError Analysis
Lin
How is th
depende
Small changes i
The syste
Function of one variable
Condition number
The condition number K is a
measure of the amplification of the
relative errorby the function f(x)
-
7/25/2019 2993 spring 2005
43/185
Numerical Methods for Engineers13.002
Linear Systems of EquationError Analysis
Vector and Matrix Norm
Properties
Perturbed R
Subtract or
Relative Err
Conditio
-
7/25/2019 2993 spring 2005
44/185
Numerical Methods for Engineers13.002
Linear Systems of EquationError Analysis
Vector and Matrix Norm
Properties
Perturbed C
Subtract unpe
Relative Err
Conditio
-
7/25/2019 2993 spring 2005
45/185
Numerical Methods for Engineers13.002
Ill-Conditioned System
n=4
a = [ [1.0 1.0]'
b= [1 2]'
ai=inv(a);
a_nrm=max( abs(a
abs(a
ai_nrm=max( abs(a
abs(ai
k=a_nrm*ai_nrm
r=ai * b
x=[0 0];
m21=a(2,1)/a(1,1)
a(2,1)=0;
a(2,2) = radd(a(
b(2) = radd(b(2
x(2) = b(2)/a(2
x(1) = (radd(b
x'
Ill-conditioned system
-
7/25/2019 2993 spring 2005
46/185
Numerical Methods for Engineers13.002
Well-Conditioned System
n=4
a = [ [0.0001 1.0
b= [1 2]'
ai=inv(a);
a_nrm=max( abs(a
abs(a
ai_nrm=max( abs(a
abs(a
k=a_nrm*ai_nrm
r=ai * b
x=[0 0];
m21=a(2,1)/a(1,1)
a(2,1)=0;
a(2,2) = radd(a(
b(2) = radd(b(2
x(2) = b(2)/a(2
x(1) = (radd(b
x'
Well-conditioned system
4-di
Algorithm
-
7/25/2019 2993 spring 2005
47/185
Numerical Methods for Engineers13.002
Introduction to Numerical Analysis fo
Systems of Linear Equations
Cramers Rule
Gaussian Elimination Numerical implementation
3.3-3.4
Numerical stability
Partial Pivoting Equilibration
Full Pivoting
Multiple right hand sides
Computation count
LU factorization
Error Analysis for Linear Systems
Condition Number
Special Matrices
Iterative Methods Jacobis method
Gauss-Seidel iteration
Convergence
-
7/25/2019 2993 spring 2005
48/185
Numerical Methods for Engineers13.002
Linear Systems of EquationTri-diagonal Systems
y(x,t)x i
Forced Vibration of a Stringf(x,t)
Harmonic excitation
f(x,t) = f(x) cos(t)
Differential Equation
Boundary Conditions
Discrete Differen
Matrix F
Tridiagonal Ma
Finite Diffe
Symmetric, positive
-
7/25/2019 2993 spring 2005
49/185
Numerical Methods for Engineers13.002
Linear Systems of EquationTri-diagonal Systems
General Tri-diagonal Systems
LU Factorization
-
7/25/2019 2993 spring 2005
50/185
Numerical Methods for Engineers13.002
Linear Systems of EquationTri-diagonal Systems
LU FactorizationRedu
Forward
Back Su
LU Factorization:
Forward substitution:Back substitution:
Total:
-
7/25/2019 2993 spring 2005
51/185
Numerical Methods for Engineers13.002
Linear Systems of EquationSpecial Matrices
General, Banded Coefficient Matrix
0
0
p
q
p super-diagonals
q sub-diagonals
w = p+q+1 bandwidth
b is half-band
Banded Symme
-
7/25/2019 2993 spring 2005
52/185
Numerical Methods for Engineers13.002
0
0
q
0
p
Linear Systems of EquationSpecial Matrices
Banded Coefficient Matrix
Gaussian EliminationNo Pivoting
= =
-
7/25/2019 2993 spring 2005
53/185
Numerical Methods for Engineers13.002
0
0
q
=
Banded Coefficient Matrix
Gaussian EliminationWith Pivoting
q
Linear Systems of EquationSpecial Matrices
-
7/25/2019 2993 spring 2005
54/185
Numerical Methods for Engineers13.002
0
0
q
q
p
Banded Coefficient Matrix
Compact Storage
Linear Systems of EquationSpecial Matrices
0
n2
Diagon
n(p
i
j
i
j - i
-
7/25/2019 2993 spring 2005
55/185
Numerical Methods for Engineers13.002
Linear Systems of EquationSpecial Matrices
Sparse and Banded Coefficient
Skyline Systems
..
Skyline storage applicable when no pivoting is needed, e
symmetric, and positive definite matrices: FEM and FD m
solvers are usually based on Choleski factorization
Skyline
Storage
Pointers 1 4 9 11 16 2
0
0
-
7/25/2019 2993 spring 2005
56/185
Numerical Methods for Engineers13.002
Linear Systems of EquationSpecial Matrices
Symmetric, Positive Definite Coefficient MaNo pivoting needed
Choleski Factorization
where
-
7/25/2019 2993 spring 2005
57/185
Numerical Methods for Engineers13.002
Introduction to Numerical Analysis fo
Systems of Linear Equations
Cramers Rule
Gaussian Elimination Numerical implementation
3.3-3.4
Numerical stability
Partial Pivoting Equilibration
Full Pivoting
Multiple right hand sides
Computation count
LU factorization
Error Analysis for Linear Systems
Condition Number
Special Matrices
Iterative Methods Jacobis method
Gauss-Seidel iteration
Convergence
-
7/25/2019 2993 spring 2005
58/185
Numerical Methods for Engineers13.002
Linear Systems of EquationIterative Methods
x
x
x
xx
xx
x x
x
0
0
0
0
0
0
Sparse, Full-bandwidth Systems
00
0
0
0
0
0
0
0
Rewrite Equ
Iterative, Recursi
Gauss-Seidels
Jacobis M
-
7/25/2019 2993 spring 2005
59/185
Numerical Methods for Engineers13.002
Linear Systems of EquationIterative Methods
Convergence
Iteration Matrix form
Decompose Coefficient Matrix
with
/
/
Jacobis
Iteration M
Convergen
Sufficient Conve
-
7/25/2019 2993 spring 2005
60/185
Numerical Methods for Engineers13.002
Linear Systems of EquationIterative Methods
Sufficient Convergence Condition
Sufficient Convergence Condition
Jacobis Method
Diagonal Dominance
Stop Criter
-
7/25/2019 2993 spring 2005
61/185
Numerical Methods for Engineers13.002
vib_string.m
n=99;
L=1.0;
h=L/(n+1);k=2*pi;
kh=k*h
x=[h:h:L-h]';
a=zeros(n,n);
f=zeros(n,1);
o=1
a(1,1) =kh^2 - 2;
a(1,2)=o;
for i=2:n-1a(i,i)=a(1,1);
a(i,i-1) = o;
a(i,i+1) = o;
end
a(n,n)=a(1,1);
a(n,n-1)=o;
nf=round((n+1)/3);
nw=round((n+1)/6);
nw=min(min(nw,nf-1),n-nf);
figure(1)hold off
nw1=nf-nw;
nw2=nf+nw;
f(nw1:nw2) = h^2*hanning(nw2-nw1+1);
subplot(2,1,1); plot(x,f,'r');
% exact solution
y=inv(a)*f;
subplot(2,1,2); plot(x,y,'b');
% Iterative solution using Jacobi
b=-a;
c=zeros(n,1);
for i=1:n
b(i,i)=0;
for j=1:n
b(i,j)=b(i,j)/a(i,i);
c(i)=f(i)/a(i,i);
end
end
nj=100;
xj=f;
xgs=f;
figure(2)
nc=6
col=['r' 'g' 'b' 'c' 'm' 'y']
hold off
for j=1:nj
xj=b*xj+c;
xgs(1)=b(1,2:n)*xgs(2:n) + c(
for i=2:n-1
xgs(i)=b(i,1:i-1)*xgs(1:i
endxgs(n)= b(n,1:n-1)*xgs(1:n-1)
cc=col(mod(j-1,nc)+1);
subplot(2,1,1); plot(x,xj,cc)
subplot(2,1,2); plot(x,xgs,cc
hold on
end
Off-diagonal values
-
7/25/2019 2993 spring 2005
62/185
Numerical Methods for Engineers13.002
vib_string.mo=1.0
Exact Solution It
-
7/25/2019 2993 spring 2005
63/185
Numerical Methods for Engineers13.002
vib_string.mo = 0.5
Exact Solution It
-
7/25/2019 2993 spring 2005
64/185
Numerical Methods for Engineers13.002
Introduction to Numerical Analysis fo
Roots of Non-linear Equations
Herons formula
Stop criteria
General method
Convergence
Examples
Newton-Raphsons Method
Convergence Speed
Examples
Secant Method
Convergence and efficiency
Examples
Multiple roots
Bisection
-
7/25/2019 2993 spring 2005
65/185
Numerical Methods for Engineers13.002
Roots of Nonlinear Equatio
Example Square root
Herons Principle
a=2;
n=6;
g=2;
% Number of Digits
dig=5;
sq(1)=g;
for i=2:n
sq(i)= 0.5*radd
end
' i v
[ [1:n]' sq']
hold off
plot([0 n],[sqrt
hold on
plot(sq,'r')
plot(a./sq,'r-.
plot((sq-sqrt(a)
grid on
i value
1.0000 2.0000
2.0000 1.5000
3.0000 1.4167
4.0000 1.4143
5.0000 1.4143
6.0000 1.4143
Guess root
Mean is better guess
Iteration Formula
( )/2
( )/2
-
7/25/2019 2993 spring 2005
66/185
Numerical Methods for Engineers13.002
Roots of Nonlinear EquatioStop-criteria
Unrealistic stop-criteria
Realistic stop-criteria
Machine
Accuracy
x
f(x)
flat f(x)
f(x)
steep f(x)
Cannot require
Use combinatio
-
7/25/2019 2993 spring 2005
67/185
Numerical Methods for Engineers13.002
Roots of Nonlinear EquatioGeneral Method
Non-linear Equation
Goal: Converging series
Rewrite Problem
Example
Iteration
% f(x) = x^3 - a
% g(x) = x + C*(x
a=2;
n=10;
g=1.0;
C=-0.1;
sq(1)=g;
for i=2:n
sq(i)= sq(i
end
hold off
plot([0 n],[
hold on
plot(sq,'r')
plot( (sq-a^
grid on
Exam
-
7/25/2019 2993 spring 2005
68/185
Numerical Methods for Engineers13.002
Roots of Nonlinear EquatioGeneral Method
Convergence
x
y
Define
Apply
Co
Conve
-
7/25/2019 2993 spring 2005
69/185
Numerical Methods for Engineers13.002
Roots of Nonlinear EquatioGeneral Method
y
y
Mean-value Theorem
x
x x
1
10
y=g(x)
y
Convergence
Convergence
-
7/25/2019 2993 spring 2005
70/185
Numerical Methods for Engineers13.002
Roots of Nonlinear EquatioGeneral Method
Example: Cube root
Rewrite
Convergence
Converges more rapidly for small
n=10;
g=1.0;
C=-0.21;sq(1)=g;
for i=2:n
sq(i)= sq(i-1)
end
hold off
f=plot([0 n],[a^
set(f,'LineWidth
hold on
f=plot(sq,'r')
set(f,'LineWidth
f=plot( (sq-a^(1
set(f,'LineWidth
legend('Exact','
f=title(['a = '
set(f,'FontSize'
grid on
-
7/25/2019 2993 spring 2005
71/185
Numerical Methods for Engineers13.002
Roots of Nonlinear EquatioGeneral Method
Converging, but how close?
General Convergence Rule
Absolute error
-
7/25/2019 2993 spring 2005
72/185
Numerical Methods for Engineers13.002
Roots of Nonlinear EquatioNewton-Raphson Method
Newton-Raphson Iteration
Fast Convergence
Convergence Criteria
Non-linear Equation
f(x)
-
7/25/2019 2993 spring 2005
73/185
Numerical Methods for Engineers13.002
Roots of Nonlinear EquatioNewton-Raphson Method
Example Square Root
Newton-Raphson
Same as Herons formula
a=26;
n=10;
g=1;
sq(1)=g;for i=2:n
sq(i)= 0.5*(
end
hold off
plot([0 n],[s
hold on
plot(sq,'r')
plot(a./sq,'r
plot((sq-sqrt
grid on
-
7/25/2019 2993 spring 2005
74/185
Numerical Methods for Engineers13.002
Roots of Nonlinear EquatioNewton-Raphson Method
a=10;
n=10;g=0.19;
sq(1)=g;
for i=2:n
sq(i)=sq(i-1) -
end
hold off
plot([0 n],[1/a 1
hold on
plot(sq,'r')
plot((sq-1/a)*a,'
grid on
legend('Exact','I
title(['x = 1/' n
Newton-Raphson
Approximate Guess
-
7/25/2019 2993 spring 2005
75/185
Numerical Methods for Engineers13.002
Roots of Nonlinear EquatioNewton-Raphson Method
Convergence Speed
Taylor Expansion
Second Order Expansion
Relative Error
General Convergence Rate
Conv
-
7/25/2019 2993 spring 2005
76/185
Numerical Methods for Engineers13.002
Roots of Nonlinear EquatioSecant Method
1. In Newton-Raphson we have to evaluate 2 functions
2. may not be given in closed, analytical form, i.e. it m
result of a numerical algorithm
Approximate Derivative
Secant Method Iteration
Only 1 function call per iteration:
f(x)
-
7/25/2019 2993 spring 2005
77/185
Numerical Methods for Engineers13.002
Roots of Nonlinear EquatioSecant Method
Convergence Speed
Absolute Error
Taylor Series 2nd order
Relative Error
Error E
Error improvement fo
Newton-Raphson
Secant Method
Exponents called
-
7/25/2019 2993 spring 2005
78/185
Numerical Methods for Engineers13.002
Roots of Nonlinear EquatioMultiple Roots
f(x)
Newton-Raphson
=>
Convergence
Slower convergence the higherthe order of the root
p-order Root
-
7/25/2019 2993 spring 2005
79/185
Numerical Methods for Engineers13.002
Roots of Nonlinear EquatioBisection
f(x)
Algorithm
n = n+1
yes
no Less efficient tha
Secant methods
interval with root
value. Then follow
method for accur
-
7/25/2019 2993 spring 2005
80/185
Numerical Methods for Engineers13.002
Roots of Nonlinear EquatioBisection
Algorithm
n = n+1
yes
no
% Root finding by bi
f=inline(' a*x -1','
a=2
figure(1); clf; hold
x=[0 1.5]; eps=1e-3;
err=max(abs(x(1)-x(2
while (err>eps & f(x
xo=x; x=[xo(1) 0
if ( f(x(1),a)*f
x=[0.5*(xo(1
end
x
err=max(abs(x(1)-
b=plot(x,f(x,a),
grid on;
end
-
7/25/2019 2993 spring 2005
81/185
Numerical Methods for Engineers13.002
Introduction to Numerical Analysis fo
Interpolation 4.1-4
Lagrange interpolation 4.3
Triangular families 4.4
Newtons iteration method 4.4
Equidistant Interpolation 4.4
Numerical Differentiation 6.1-6
Numerical Integration 7.1-7
Error of numerical integration
Mathe
-
7/25/2019 2993 spring 2005
82/185
Numerical Methods for Engineers13.002
Numerical Interpolation
Purpose of numerical Interpolatio
1. Compute intermediate values of a samp
2. Numerical differentiation foundation fo
Difference and Finite Element methods
3. Numerical Integration
Given:
Find for
-
7/25/2019 2993 spring 2005
83/185
Numerical Methods for Engineers13.002
Numerical InterpolationPolynomial Interpolation
x
f(x)
Interpolation function
Interpolation
Polynomia
Coefficients: Linea
-
7/25/2019 2993 spring 2005
84/185
Numerical Methods for Engineers13.002
Numerical InterpolationPolynomial Interpolation
Linear Interpolation
Examples
x
f(x)f(x)
Quadratic
-
7/25/2019 2993 spring 2005
85/185
Numerical Methods for Engineers13.002
Numerical InterpolationPolynomial Interpolation
Taylor Series
Remainder
Requirement
x
f(x)
f(x)
p(x)
Ill-conditioned for large n
Polynomial is uniq
we calculate the
-
7/25/2019 2993 spring 2005
86/185
Numerical Methods for Engineers13.002
Numerical InterpolationLagrange Polynomials
f(x)
1
k-3
Difficult tDifficult t
Divisions
Importan
-
7/25/2019 2993 spring 2005
87/185
Numerical Methods for Engineers13.002
Numerical InterpolationTriangular Families of Polynom
Ordered Polynimials
where
Special form c
for interpo
Coefficie
foun
-
7/25/2019 2993 spring 2005
88/185
Numerical Methods for Engineers13.002
Numerical InterpolationTriangular Families of Polynom
Polynomial EvaluationHorners Scheme
Remainder Interpolation Error
-
7/25/2019 2993 spring 2005
89/185
Numerical Methods for Engineers13.002
Numerical InterpolationNewtons Iteration Formul
Standard triangular family of polynomials
Divided Differences
Newtons
-
7/25/2019 2993 spring 2005
90/185
Numerical Methods for Engineers13.002
Numerical InterpolationNewtons Iteration Formul
x
f(x) f(x)
01 2 3 4
-
7/25/2019 2993 spring 2005
91/185
Numerical Methods for Engineers13.002
Numerical InterpolationEquidistant Newton Interpola
Divided
Stepsiz
Equidistant Sampling
-
7/25/2019 2993 spring 2005
92/185
Numerical Methods for Engineers13.002
Numerical InterpolationNewtons Iteration Formul
function[a] = interp_test(n)
%n=2
h=1/n
xi=[0:h:1]
f=sqrt(1-xi.*xi) .* (1 - 2*xi +5*(xi.*xi));
%f=1-2*xi+5*(xi.*xi)-4*(xi.*xi.*xi);
c=newton_coef(h,f)
m=101
x=[0:1/(m-1):1];
fx=sqrt(1-x.*x) .* (1 - 2*x +5*(x.*x));
%fx=1-2*x+5*(x.*x)-4*(x.*x.*x);
y=newton(x,xi,c);
hold off; b=plot(x,fx,'b'); set(b,'LineWidth',2);hold on; b=plot(xi,f,'.r') ; set(b,'MarkerSize',30);
b=plot(x,y,'g'); set(b,'LineWidth',2);
yl=lagrange(x,xi,f);
b=plot(x,yl,'xm'); set(b,'Markersize',5);
b=legend('Exact','Samples','Newton','Lagrange')
b=title(['n = ' num2str(n)]); set(b,'FontSize',16);
function[y] = newton(x,xi,c)
% Computes Newton polynomial
% with coefficients cn=length(c)-1
m=length(x)
y=c(n+1)*ones(1,m);
for i=n-1:-1:0
cc=c(i+1);
xx=xi(i+1);
y=cc+y.*(x-xx);
end
function[c] = newton_coef(h,f)
% Computes Newton Coefficients
% for equidistant sampling hn=length(f)-1
c=f; c_old=f; fac=1;
for i=1:n
fac=i*h;
for j=i:n
c(j+1)=(c_old(j+1)-c_old(j))/fac;
end
c_old=c;
end
-
7/25/2019 2993 spring 2005
93/185
Numerical Methods for Engineers13.002
Introduction to Numerical Analysis fo
Interpolation 4.1-4
Lagrange interpolation 4.3
Triangular families 4.4
Newtons iteration method 4.4
Equidistant Interpolation 4.4
Numerical Differentiation 6.1-6
Numerical Integration 7.1-7
Error of numerical integration
Mathe
-
7/25/2019 2993 spring 2005
94/185
Numerical Methods for Engineers13.002
Numerical Differentiation
Taylor Series
First order
f(x)
-
7/25/2019 2993 spring 2005
95/185
Numerical Methods for Engineers13.002
f(x)
Numerical Differentiation
Second order
Second Derivatives
n=3 Central Difference
Forward Differencen=2
-
7/25/2019 2993 spring 2005
96/185
Numerical Methods for Engineers13.002
Numerical Integration
f(x)
a
Lagrange Interpolation
Equidistant Sampling
Integration Weights (Cotes Numbers)
Properties
-
7/25/2019 2993 spring 2005
97/185
Numerical Methods for Engineers13.002
Numerical Integration
Trapezoidal Rule
Simpsons Rule
f(x)
f(x)
-
7/25/2019 2993 spring 2005
98/185
Numerical Methods for Engineers13.002
Numerical IntegrationError Analysis
x
f(x)
h h
Trapezoidal Rule
Local Absolute Error
Global Error
S
Local ErrorN Intervals
-
7/25/2019 2993 spring 2005
99/185
Numerical Methods for Engineers13.002
Introduction to Numerical Analysis fo
Ordinary Differential Equations
Initial Value Problems
Eulers Method
Taylor Series Methods
Error analysis
Runge-Kutta Methods
Systems of differential equations
Boundary Value Problems
Shooting method
Direct Finite Difference methods
M
-
7/25/2019 2993 spring 2005
100/185
Numerical Methods for Engineers13.002
Ordinary Differential Equation
Initial Value Problems
Differential Equation
non-linear in y
Non-Linear Differential Equation
Linear Differential Equation
Linear differential equations can often be solv
Non-linear equations require numerical soluti
y
a
-
7/25/2019 2993 spring 2005
101/185
Numerical Methods for Engineers13.002
Eulers Method
Differential Equation
Example
Discretization
Finite Difference (forward)
Recurrence
Ordinary Differential Equati
Initial Value Problems
-
7/25/2019 2993 spring 2005
102/185
Numerical Methods for Engineers13.002
Initial Value Problems
Taylor Series Methods
Taylor Series
Partial Derivatives
Derivatives
+
Truncate ser
Initial Value Problem
Choose
Recursio
w
Loca
Discr
-
7/25/2019 2993 spring 2005
103/185
Numerical Methods for Engineers13.002
Initial Value Problems
Taylor Series Methods
Example
Eulers Method
General Taylor Series Method Example
Err
-
7/25/2019 2993 spring 2005
104/185
Numerical Methods for Engineers13.002
Initial Value Problems
Taylor Series Methods
Error Analysis De
Error Estimates and Convergence
Eulers Method
-
7/25/2019 2993 spring 2005
105/185
Numerical Methods for Engineers13.002
Initial Value Problems
Taylor Series Methods
Error AnalysisExample Eulers Method
Exact solution
Derivative Bounds
Error Bound
-
7/25/2019 2993 spring 2005
106/185
Numerical Methods for Engineers13.002
Initial Value ProblemsRunge-Kutta Methods
Taylor Series Recursion
Runge-Kutta Recursion
Matcha,b, to match Taylor series amap.
Substitute k
Match 2nd ord
-
7/25/2019 2993 spring 2005
107/185
Numerical Methods for Engineers13.002
Initial Value ProblemsRunge-Kutta Methods
Initial Value Problem
2nd Order Runge-Kutta
4th Order Runge-Kutta
y
mid-poi
Predic
-
7/25/2019 2993 spring 2005
108/185
Numerical Methods for Engineers13.002
Eulers Method
Recurrence
Initial Value Problems
Runge-Kutta Methodsh=1.0;
x=[0:0.1*h:10];
y0=0;
y=0.5*x.^2+y0;
figure(1); hold offa=plot(x,y,'b'); set
% Euler's method, fo
xt=[0:h:10]; N=lengt
yt=zeros(N,1); yt(1)
for n=2:N
yt(n)=yt(n-1)+h*
end
hold on; a=plot(xt,y
% Runge Kutta
fxy='x'; f=inline(fx
[xrk,yrk]=ode45(f,xt
a=plot(xrk,yrk,'.g')
a=title(['dy/dx = '
set(a,'FontSize',16)
b=legend('Exact',['E
'Runge-Kutta (Matlab
4th Order Runge-Kutta
Matlab ode45 automatically ensures convergence
Matlab inefficient for large problems > Convergence Analysis
-
7/25/2019 2993 spring 2005
109/185
Numerical Methods for Engineers13.002
Introduction to Numerical Analysis fo
Ordinary Differential Equations
Initial Value Problems
Eulers Method
Taylor Series Methods
Error analysis
Runge-Kutta Methods
Systems of differential equations
Boundary Value Problems
Shooting method
Direct Finite Difference methods
M
-
7/25/2019 2993 spring 2005
110/185
Numerical Methods for Engineers13.002
Initial Value Problems
Higher Order Differential Equ
Differential Equation
Initial
Conditions
Convert to 1s
Matr
Solved using e.g
-
7/25/2019 2993 spring 2005
111/185
Numerical Methods for Engineers13.002
Boundary Value Problems
Shooting Method
Differential Equation
y
a
Boundary
Conditions
Shooting Method
Initial value Problem
Solve by Runge-Kutta
Shooting Iteration
-
7/25/2019 2993 spring 2005
112/185
Numerical Methods for Engineers13.002
Boundary Value Problems
Direct Finite Difference Metho
Differential Equation
y
a
Boundary
Conditions
Discretization
Finite Differences
-
7/25/2019 2993 spring 2005
113/185
Numerical Methods for Engineers13.002
Boundary Value Problems
Direct Finite Difference MethoBoundary value Problem
Finite Differences
Substitute Finite Differences
Difference Equations
N-1 equations, N-1 unknowns
Matrix
Linear Differen
Solve using standard l
-1
-
7/25/2019 2993 spring 2005
114/185
Numerical Methods for Engineers13.002
Boundary Value ProblemsFinite Difference Methods
Forced Vibration of a Stringf(x,t)
Harmonic excitation
f(x,t) = f(x) cos(t)
Differential Equation
Boundary Conditions
Discrete Differenc
Matrix F
Tridiagonal Mat
Finite Diffe
Symmetric, positive d
y(x,t)x i
-
7/25/2019 2993 spring 2005
115/185
Numerical Methods for Engineers13.002
Boundary Value ProblemsFinite Difference Methods
Boundary Conditions with Derivatives
Difference Equations
Centra
N-1
General Bou
Add extra point - N
Finite DiffereO(h )4
O(h )2
Central Difference
Backward Difference
-
7/25/2019 2993 spring 2005
116/185
Numerical Methods for Engineers13.002
Introduction to Numerical Analysis fo
Minimization Problems
Least Square Approximation
Normal Equation
Parameter Estimation Curve fitting
Optimization Methods
Simulated Annealing
Traveling salesman problem
Genetic Algorithms
M
-
7/25/2019 2993 spring 2005
117/185
Numerical Methods for Engineers13.002
Minimization Problems
Data Modeling Curve Fitting
Mini
N
Objective: Find c that minimizes error
-
7/25/2019 2993 spring 2005
118/185
Numerical Methods for Engineers13.002
Least Square Approximatio
m
n
m
m
n
n model parameters
m measurements
Linear Measurement Model Overd
Lea
Minim
-
7/25/2019 2993 spring 2005
119/185
Numerical Methods for Engineers13.002
Least Square Approximatio
A
Theorem
)
Proof
Nor
Symmetr
singular
linea
-
7/25/2019 2993 spring 2005
120/185
-
7/25/2019 2993 spring 2005
121/185
Numerical Methods for Engineers13.002
% Quadratic data model
fxy='a*x.^2+b'
f=inline(fxy,'x','a','b');
x=[0:0.01:1]; x=reshape([x' x']',1,2*length(x));
n=length(x); y=zeros(n,1);
a=5; b=3;
% Generate noisy data
amp=0.05*(max(f(x,a,b))-min(f(x,a,b)));
for i=1:n
y(i) =f(x(i),a,b)+random('norm',0,amp);
end
figure(1); clf; hold off; p=plot(x,y,'.r');
set(p,'MarkerSize',10)
% Non-linear, quadrati model
A=ones(n,2); A(:,1)=f(x,1,0)'; bb=y;
%Normal matrix
C=A'*A; c=A'*bb;
z=inv(C)*c
% Residuals
r=bb-A*z; rn=sqrt(r'*r)/n
hold on; p=plot(x,f(x,z(1),z(2)),'b'); set(p,'LineWidth',2)
% Linear model
A(:,1)=x';
C=A'*A; c=A'*bb;
z=inv(C)*c
% Residuals
r=bb-A*z; rn=sqrt(r'*r)/n
hold on; p=plot(x,z(1)*x+z(2),'g'); set(p,'LineWidth',2)
p=legend('Data','Non-linear','Linear'); set(p,'FontSize',14);
Least Square ApproximatioCurve Fitting
curve.m
-
7/25/2019 2993 spring 2005
122/185
Numerical Methods for Engineers13.002
Optimization ProblemsNon-linear Models
Minimim
x
E(c)
Measured
Non
i
Global
Minimum
Local
Minimum
Non-linear models often have multiple, local minima. A locally
approximation may therefore find a local minimum instead of th
-
7/25/2019 2993 spring 2005
123/185
-
7/25/2019 2993 spring 2005
124/185
Numerical Methods for Engineers13.002
Simulated AnnealingExample: Traveling Salesman Pr
Objective:
Visit N cities acorder, in the sh
Metr
1. Configura
can vary2. Rearrang
any two c
3. Cost func
number o
4. Annealin
with coo
for e.g. 1
method).
Penalty for crossing Mississippi
Cost function: Distance Traveled
East: = 1
West: = -1
Adapted by MIT OCW.
-
7/25/2019 2993 spring 2005
125/185
Numerical Methods for Engineers13.002
Simulated AnnealingExample: Traveling Salesman Pr
% Travelling salesman problem
% Create random city distribution
n=20; x=random('unif',-1,1,n,1); y=random('unif',-1,1,n,1);
gam=1; mu=sign(x);% End up where you start. Add starting point to end
x=[x' x(1)]'; y=[y' y(1)]'; mu=[mu' mu(1)]';
figure(1); hold off; g=plot(x,y,'.r'); set(g,'MarkerSize',20);
c0=cost(x,y,mu,gam); k=1; % Boltzman constant
nt=50; nr=200; % nt: temp steps. nr: city switches each T
cp=zeros(nr,nt);
iran=inline('round(random(d,1.5001,n+0.4999))','d','n');
for i=1:nt
T=1.0 -(i-1)/nt
for j=1:nr
% switch two random cities
ic1=iran('unif',n); ic2=iran('unif',n);
xs=x(ic1); ys=y(ic1); ms=mu(ic1);
x(ic1)=x(ic2); y(ic1)=y(ic2); mu(ic1)=mu(ic2);
x(ic2)=xs; y(ic2)=ys; mu(ic2)=ms;
p=random('unif',0,1); c=cost(x,y,mu,gam);
if (c < c0 | p < exp(-(c-c0)/(k*T))) % accept
c0=c;
else % reject and switch back
xs=x(ic1); ys=y(ic1); ms=mu(ic1);
x(ic1)=x(ic2); y(ic1)=y(ic2); mu(ic1)=mu(ic2);
x(ic2)=xs; y(ic2)=ys; mu(ic2)=ms;
end
cp(j,i)=c0;
end
figure(2); plot(reshape(cp,nt*nr,1)); drawnow;
figure(1); hold off; g=plot(x,y,'.r'); set(g,'MarkerSize',20);
hold on; plot(x,y,'b');
g=plot(x(1),y(1),'.g'); set(g,'MarkerSize',30);
p=plot([0 0],[-1 1],'r--'); set(g,'LineWidth',2); drawnow;
end
salesman.m function n=length(
c=0;
for i=1:nc =c+s
+ g
end
-
7/25/2019 2993 spring 2005
126/185
-
7/25/2019 2993 spring 2005
127/185
-
7/25/2019 2993 spring 2005
128/185
-
7/25/2019 2993 spring 2005
129/185
-
7/25/2019 2993 spring 2005
130/185
-
7/25/2019 2993 spring 2005
131/185
-
7/25/2019 2993 spring 2005
132/185
-
7/25/2019 2993 spring 2005
133/185
-
7/25/2019 2993 spring 2005
134/185
-
7/25/2019 2993 spring 2005
135/185
-
7/25/2019 2993 spring 2005
136/185
-
7/25/2019 2993 spring 2005
137/185
-
7/25/2019 2993 spring 2005
138/185
-
7/25/2019 2993 spring 2005
139/185
-
7/25/2019 2993 spring 2005
140/185
-
7/25/2019 2993 spring 2005
141/185
-
7/25/2019 2993 spring 2005
142/185
-
7/25/2019 2993 spring 2005
143/185
-
7/25/2019 2993 spring 2005
144/185
-
7/25/2019 2993 spring 2005
145/185
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
x
y
Plot of x*sin(x) vs. x
-
7/25/2019 2993 spring 2005
146/185
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
x
y
y1 = x sin(x)
y2 = sin(x) _._
-
7/25/2019 2993 spring 2005
147/185
-
7/25/2019 2993 spring 2005
148/185
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
x
y
y2 = sin(x) _._
y1 = x sin(x)
-
7/25/2019 2993 spring 2005
149/185
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
0.2
0.4
0.6
0.8
1
x
y
Plot of sin(x) vs. x
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
0.2
0.4
0.6
0.8
1
x
y
Plot of x*sin(x) vs. x
-
7/25/2019 2993 spring 2005
150/185
-
7/25/2019 2993 spring 2005
151/185
-
7/25/2019 2993 spring 2005
152/185
-
7/25/2019 2993 spring 2005
153/185
-
7/25/2019 2993 spring 2005
154/185
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 11
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
time (sec)
ty(t)=sin(alpha*t)
simple plot by jleonard 2/23/97
-
7/25/2019 2993 spring 2005
155/185
-
7/25/2019 2993 spring 2005
156/185
-
7/25/2019 2993 spring 2005
157/185
90 60 30 0 30 60 9080
70
60
50
40
30
20
10
0f = 120 kHz D = 0.037 m beamwidth = +9.899 deg
theta (degrees)
Normalizedsourcelevel(dB)
-
7/25/2019 2993 spring 2005
158/185
Issued:
February
3rd,
2005
13.002 IntroductiontoNumericalMethods forEngineers
In-classprogrammingexercises
1. Letabeapositiverealnumber,andletthesequenceofrealnumbersxi begivenby
1 ax0 =1, xi+1 =
2(xi
+xi
),
fori=0, 1, 2, 3, . . ..
Thevaluexi willconvergeto
aasiWriteaprogramthatreadsinthevalueofa
interactivelyandusesthisalgorithmtocomputethesquarerootofa.
Test your program as you vary the maximum number of iterations of the algorithm isincreased from1, 2, 3, . . .anddeterminehowmanysignificantdigitsofprecisionthatyouobtain
for
each.
How
many
iterations
are
necessary
to
reach
the
machine
precision
of
matlab?
2. Writeaprogramtoevaluateebytheseries:
1 1 1 1e=1+1+ + + + +. . .
2! 3! 4! 5!
Testyourprogramasyouincreasethenumberoftermsintheseries. Determinehowmanysignificantdigitsofprecisionthatyouobtain inyouranswerasa functionofthenumberoftermsintheseries. Howmanytermsarenecessarytoreachmachineprecision?
3. Considerthefunctionx sin(x)1.
(a.) Howmanyrootsdoesthisfunctionhaveintheinterval[0, ]?
(b.) Write a matlab program to find the root(s) using Newton-Raphson iteration withappropriatestartingvalues.
(c.)
Make
a
graph
of
relative
error
vs.
iteration
step
for
all
roots.
(d.)
How
many
iterations
are
needed
to
reach
an
error
of
less
than
108?
-
7/25/2019 2993 spring 2005
159/185
13.002J/10.002J
Introduction
to
Numerical
Analysis
for
Engineers
Problem
Set
1
Issued:
February
3,
2005
Due:
February
10,
2005
Do
the
following
problems
from
Mathews
and
Fink:
1.2.4(a)and(b)(onpage23)
1.2.5(a)and(b)
1.2.13(b)(onpage24)
1.3.1(b)and(c)(onpage37)
1.3.12(onpage39)
1.3.13
(a)
Programmingexercise1(onpage39)
Programmingexercise2
-
7/25/2019 2993 spring 2005
160/185
13.002
PS #1 Solution
1.2.4. (a)
(1.0110101)2 = (2^0)+(2^-2)+(2^-3)+(2^-5)+(2^-7)= 1.41406250000000
1.2.4. (b)
(11.0010010001)2 = (2^1)+(2^0)+(2^-3)+(2^-6)+(2^-10)= 3.14160156250000
1.2.5. (a)
2 (1.0110101) = 1.41421356237309 1.41406250000000=2
= 1.510623730900385e-004 (absolute error)
1.510623730900385e-004= 1.068172283940988e-004 (relative error)
1.41421356237309
1.2.5. (b)
(11.0010010001)2 = 3.14159265358979- 3.14160156250000
= -8.908910209992627e-006 (absolute error)
8.908910209992627e-006= 2.835794194964366e-006 (relative error)
3.14159265358979
1.2.13. (b)
1 1 1( + ) + =10 3 5
21((0.1101) 23 + (0.1011) ) = (0.1110) 212 2 2
22(0.1110) 21 + (0.1101) = (0.1010) 202 2 2
19 (0.1010) 20
302
0.63333333333333- 0.6250000000000= 0.0083333333333 (absolute error)
0.0083333333333= 0.01315... (relative error)
0.63333333333333
-
7/25/2019 2993 spring 2005
161/185
1.3.1.(b)
98350-98000 = 350 (absolute error)
350= 0.00355871886121 (relative error) (2 significant digits)
98350
1.3.1.(c)
0.000068-0.00006= 8e-006 (absolute error)
8e-006= 0.117647058 (relative error) (no significant digits)
0.000068
1.3.12
b2
b2
b + 4ac b + 4ac 2c =x1new =
b2
b22a b + 4ac b + 4ac
b2
b2
b 4ac b 4ac 2c =x2new =
b2
b22a b 4ac b 4ac
1.3.13. (a)
2x 1, 000.001x +1 = 0
b2
b + 4acx1 = = 1000
2a
2cx2 = = 0.001
b2
b 4ac
-
7/25/2019 2993 spring 2005
162/185
Programming Exercise 1
% script M-file findroots.m
a=input(Enter the value of "a" from ax^2+bx+c=0 :);
b=input(Enter the value of "b" from ax^2+bx+c=0 :);
c=input(Enter the value of "c" from ax^2+bx+c=0 :);if b>= 0;
sign=1;
else sign=-1;
end;
q=-0.5*(b+sign*sqrt((b^2)-4*a*c));
x1=q/a;
x2=c/q;
xx1=num2str(x1);
xx2=num2str(x2);
disp([X1 is equal to , xx1])
disp([X2 is equal to ,xx2])
Programming Exercise 2
x(1)=1/2;r(1)=0.994; p(1)=1; p(2)=0.497; q(1)=1; q(2)=0.497;
for n=2:11
x(n)=(1/2)*x(n-1);
end
for n=2:11
r(n)=(1/2)*(r(n-1));
end
for n=3:11
p(n)=(3/2)*p(n-1)-(1/2)*p(n-2);
end
for n=3:11q(n)=(5/2)*q(n-1)-q(n-2);
end
h=1:11;
figure(1)
plot(h, x(h)-r(h),bd,h, x(h)-p(h),r+,h, x(h)-q(h),g)
grid on
legend(r(n),p(n),q(n))
fprintf(n x(n) r(n) p(n) q(n)\n)
for i = h
fprintf(%2d %+10.8f %+10.8f %+10.8f %+10.8f\n, i, x(i), p(i), r(i), q(i))
endfprintf( n x(n)-r(n) x(n)-p(n) x(n)-q(n)\n)
for i = h
fprintf(%2d %+10.8f %+10.8f %+10.8f\n, i, x(i)-r(i), x(i)-p(i), x(i)-q(i))
end
-
7/25/2019 2993 spring 2005
163/185
n x(n)
1 +0.50000000
2 +0.25000000
3 +0.12500000
4 +0.06250000
5 +0.03125000
6 +0.01562500
7 +0.00781250
8 +0.00390625
9 +0.00195313
10 +0.00097656
11 +0.00048828
n x(n)-r(n)
1 -0.49400000
2 -0.24700000
3 -0.12350000
4 -0.06175000
5 -0.03087500
6 -0.015437507 -0.00771875
8 -0.00385938
9 -0.00192969
10 -0.00096484
11 -0.00048242
>>
r(n) p(n)
+1.00000000 +0.99400000
+0.49700000 +0.49700000
+0.24550000 +0.24850000
+0.11975000 +0.12425000
+0.05687500 +0.06212500
+0.02543750 +0.03106250
+0.00971875 +0.01553125
+0.00185938 +0.00776563
-0.00207031 +0.00388281
-0.00403516 +0.00194141
-0.00501758 +0.00097070
q(n)
+1.00000000
+0.49700000
+0.24250000
+0.10925000
+0.03062500
-0.03268750
-0.11234375
-0.24817188
-0.50808594
-1.02204297
-2.04702148
x(n)-p(n)
-0.50000000
-0.24700000
-0.12050000
-0.05725000
-0.02562500
-0.00981250-0.00190625
+0.00204687
+0.00402344
+0.00501172
+0.00550586
x(n)-q(n)
-0.50000000
-0.24700000
-0.11750000
-0.04675000
+0.00062500
+0.04831250+0.12015625
+0.25207813
+0.51003906
+1.02301953
+2.04750977
-
7/25/2019 2993 spring 2005
164/185
-
7/25/2019 2993 spring 2005
165/185
-
7/25/2019 2993 spring 2005
166/185
-
7/25/2019 2993 spring 2005
167/185
-
7/25/2019 2993 spring 2005
168/185
-
7/25/2019 2993 spring 2005
169/185
-
7/25/2019 2993 spring 2005
170/185
-
7/25/2019 2993 spring 2005
171/185
-
7/25/2019 2993 spring 2005
172/185
Amendment to Problem Set 4
The previous equation has been changed to
be:
x1
2
3
1 0
2 e
1 1 0
1 1
2 1
x1
x2 = 2
x3
= 6
01.
1
e
e
e
=
xx
1
1
1
=
x1x
2
x3
1e1
1
=
1
1
3=
-
7/25/2019 2993 spring 2005
173/185
=
x3
=
2
2.
clear
clc
alfa=100;
A=[exp(-alfa) 1 0; -1 exp(-alfa) -1; 1 -2
exp(-alfa)];
%A=[1 exp(-alfa) 0; exp(-alfa) -1 -1; -2 1
exp(-alfa)];oA=A;
B=[1; exp(-alfa); exp(-alfa)];
oB=B;
x2
x1
x3
0 1 0
1 0
2 0
1
0
=
0
1
1
2
1
=
=
x1
x2
-
7/25/2019 2993 spring 2005
174/185
[mm,n]=size(A);
A=[A B];L=zeros(mm,n);
for i=1:mm-1
for j=i+1:mm
m(j,i)=A(j,i)/A(i,i);
L(j,i)=m(j,i);for k=i:n+1
A(j,k)=A(j,k)-m(j,i)*A(i,k);
end
end
end
U=A(:,1:n);
L=L+eye(mm,n);
B=A(:,n+1);
x=zeros(n,1);
for j=mm:-1:1x(j)=(B(j)-A(j,j+1:n)*x(j+1:n))/A(j,j);
end
x
-
7/25/2019 2993 spring 2005
175/185
oB-oA*x
3.
=0
1
=2
2
= 63
x
x
x
=3
AxB =
0 The solutions are exactly correct.
=5
1
= 9866.02
x
x
x3
= 9934.1
=
9933.1
AxB 0 Solutions are very good
approximation.
=10
-
7/25/2019 2993 spring 2005
176/185
1 =2
= 9999.02
=
2
3
x
x
x
AxB 0 Solutions are very goodapproximation
=
20
=21
=12
= 23
x
x
x
AxB
0 Solutions are very goodapproximation
=
40
=0x
x
x
1
=12
=03
-
7/25/2019 2993 spring 2005
177/185
0
AxB
=
0
2
Big errors exist. Without
pivoting, its not stable now.
4.
clearclc
alfa=40;
%A=[1 exp(-alfa) 0; exp(-alfa) -1 -1; -2 1
exp(-alfa)];A=[exp(-alfa) 1 0; -1 exp(-alfa) -1; 1 -2
exp(-alfa)]; %original one.
oA=A;
B=[1; exp(-alfa); exp(-alfa)];
oB=B;
[mm,n]=size(A);
A=[A B];
L=zeros(mm,n);
-
7/25/2019 2993 spring 2005
178/185
for i=1:mm-1
%pivoting begins from here.
[Y I]=max(abs(A(i:mm,i)));
temp_store1=A(I,:);
temp_store2=B(I);
A(I,:)=A(i,:);
B(I)=B(i);A(i,:)=temp_store1;
B(i)=temp_store2;
%pivoting ends from here.
for j=i+1:mm
m(j,i)=A(j,i)/A(i,i);
L(j,i)=m(j,i);
for k=i:n+1
A(j,k)=A(j,k)-m(j,i)*A(i,k);
end
endend
U=A(:,1:n);
L=L+eye(mm,n);
-
7/25/2019 2993 spring 2005
179/185
x
B=A(:,n+1);
x=zeros(n,1);for j=mm:-1:1
x(j)=(B(j)-A(j,j+1:n)*x(j+1:n))/A(j,j);
end
oB-oA*x
5. switch x1 and x2
-
7/25/2019 2993 spring 2005
180/185
-
7/25/2019 2993 spring 2005
181/185
-
7/25/2019 2993 spring 2005
182/185
-
7/25/2019 2993 spring 2005
183/185
-
7/25/2019 2993 spring 2005
184/185
13.002
Introduction to Numerical Methods for Engineers
Take Home Exam
Issued: Thursday, Mar. 10, 2005
Due: Friday, Mar. 18, 2005
Problem 1.
Problem 2.
-
7/25/2019 2993 spring 2005
185/185
13.002J - Introduction to Numerical Analysis for EngineersClass Survey - Spring 2005
Name:Course:E-mail:WhatprogrammingsubjectshaveyoutakenatMIT?
1.00
6.001
10.001
Self-Assessment Quiz (no grade)