Numerical Methods For Civil Engineers (MATLAB)
Numerical Methods For Civil Engineers (MATLAB)
Lecture 1 -
MATLAB
Introduction
Instructor: Mongkol JIRAVACHARADET
Institute of Engineering
Textbook
Applied Numerical Methods
With MATLAB for Engineers
and Scientists
STEVEN C. CHAPRA
Topics Covered
• Introduction to Matlab • Interpolation
• Curve Fitting
Conduct of Course
Homework/Projects/Quizzes 30 %
Midterm Exam 30 %
Final Exam 40 %
Grading Policy
Final Score Grade
100 - 90 A
89 - 85 B+
84 - 80 B
79 - 75 C+
74 - 70 C
69 - 65 D+
64 - 60 D
59 - 0 F
WARNINGS !!!
1) Participation expected, check by quizzes
MATLAB
The Language of Technical Computing
M ATLAB เปนโปรแกรมที่รวมการคํานวณทาง
คณิตศาสตร การแสดงผล และภาษาที่มีประสิทธิภาพ
เพื่อสรางสภาวะแวดลอมที่ยืดหยุนสําหรับการคํานวณ
ทางเทคนิค ดวยสถาปตยกรรมแบบเปด ทําใหเปนการงาย
ที่จะใช MATLAB ในการสํารวจขอมูล สรางอัลกอริธึม หรือ
สรางเครื่องมือคํานวณตามที่ผูใชตองการ
www.mathworks.com
MATLAB = MATrix LABoratory
- Math and computation
- Algorithm development
http://www.math.utah.edu/lab/ms/matlab/matlab.html
http://www.math.mtu.edu/~msgocken/intro/intro.html
MATLAB 1
- Getting started
- Basic Arithmetic
- Built-in Functions
- Built-in Variables
Development Environment: set of tools and facilities that help you use MATLAB
functions and files. Many of these tools are graphical user interfaces. It includes the
MATLAB desktop and Command Window, a command history, an editor and
dedugger, and browsers for viewing help, the workspace, files, and the search path.
Graphics: MATLAB has extensive facilities 2-D and 3-D data visualization,
animation, and presentation graphics.
The MATLAB Application Program Interface (API): allows you to write C and
Fortran programs that interact with MATLAB.
Getting Started
MATLAB Desktop
Getting Started
Command Window
Editor/Debugger
Basic Arithmetic
Calculator functions work as you'd expect:
>>(1+4)*3
ans =
15
+ and - are addition, / is division, * is multiplication, ^ is an exponent.
>> 5*5*5
>> 5^(-2.5)
>> 3*(23+14.7-4/6)/3.5
Built-in Functions
>> x = 5;
>> y = sqrt(59);
>> z = log(y) + x^0.25
z =
3.5341
The commas allow more than one command
on a line:
>> t = 5;
>> t = t + 2
t =
7
Variable Meaning
Arrays Operations
>> A = [ 3 5 7 9 11 ] >> A / B
>> A(3) >> A ./ B
>> length(A) >> A .^ 2
>> clear(A) >> odd = 1:2:11
>> B = [ 2 4 6 8 10 ] >> even = 2:2:12
>> A + B >> natural = 1:6
>> A - B >> angle = 0:pi/10:pi;
>> A * B >> sin(angle)
>> A .* B
Generate matrices using built-in functions
Subscripts
For example, A(4,2) is the number in the fourth row and second column.
>> A(4,2)
ans =
15
Submatrix
>> A(1,:)
>> A(:,2)
>> A(3:4,1:2)
Juxtaposition
>> B = [9 8 2 5 ; 4 5 6 7 ; 2 1 3 4]
>> [A B]
>> size(ans)
>> [A ; B]
The following table gives data for the distance travel along five truck routes and the
corresponding time required to traveled each route. Use the data to compute the
average speed required to drive each route. Find the route that has the highest
average speed.
1 2 3 4 5
Distance (miles) 560 440 490 530 370
Time (hrs) 10.3 8.2 9.1 10.1 7.5
Solution:
>>d = [560, 440, 490, 530, 370]
>>t = [10.3, 8.2, 9.1, 10.1, 7.5]
>>speed = d./t
speed =
54.3689 53.6585 53.8462 52.4752 49.3333
>>[highest_speed, route] = max(speed)
highest_speed = route =
54.3689 1
Numerical Methods for Civil Engineers
Lecture 2 - MATLAB 2
Mongkol JIRAVACHARADET
Input/Output Commands
Command Description
disp (A) Displays the contents, but not the name, of the
Array A.
disp (‘text’) Displays the text string enclosed within single quotes.
x = input(‘text’) Displays the text in quotes, waits for user input from
the keyboard, and stores the value in x.
x = input(‘text’,’s’) Displays the text in quotes, waits for user input from
the keyboard, and stores the input as a string in x.
INPUT AND OUTPUT
function s = inputAbuse
% inputAbuse Use input messages to compute sum of 3 variables
Text Output
Since disp requires only one argument, the message and variable
must be combined into a single string.
Syntax Description
fprintf(‘format’,A,...) Displays the elements of the array A, and any
addition array arguments, according to the format
specified in the string ‘format’
‘format’ structure %[-][number1.number2]C, where
number1 specifies the minimum field width,
number2 specifies the number of digits to the
right of the decimal point, and C contains control
codes and format codes. Items in brackets are
optional. [-] specifies left justified.
fprintf(format)
fprintf(format,variables)
fprintf(fid,format,variables)
fprintf (Continue…)
\\ Backslash.
fprintf(format,variables)
>>r = [2.25:20:42.25];
2.25 14.137
>>circum = 2*pi*r;
22.25 139.8
>>y = [r;circum];
42.25 265.46
>>fprintf(‘%5.2f %11.5g\n’,y)
>> x = ...
>> fout = fopen(‘myfile.dat’,’wt’);
>> fprintf(fout,’ k x(k)\n’);
>> for k = 1:length(x)
>> fprintf(fout,’%4d %5.2f\n’,k,x(k));
>> end
>> fclose(fout)
Creating a Plot
The plot function has different forms, depending on the input arguments.
If y is a vector, plot(y) produces a piecewise linear graph of the elements of y
versus the index of the elements of y.
If you specify two vectors as arguments, plot(x,y) produces a graph of y versus
x.
For example, these statements use the colon operator to create a vector of x values
ranging from zero to 2π, compute the sine of these values, and plot the result.
>> x = 0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y)
Now label the axes and add a title. The characters \pi create the symbol π.
Multiple x-y pair arguments create multiple graphs with a single call to plot.
MATLAB automatically cycles through a predefined (but user settable) list of colors to
allow discrimination among sets of data.
For example, these statements plot three related functions of x, each curve in a
separate distinguishing color.
>> y2 = sin(x-.25);
>> y3 = sin(x-.5);
>> plot(x,y,x,y2,x,y3)
>> legend('sin(x)','sin(x-.25)','sin(x-.5)')
It is possible to specify color, line styles, and markers (such as plus signs or
circles) when you plot your data using the plot command.
>> plot(x,y,'color_style_marker')
color_style_marker is a string containing from one to four characters
(enclosed in single quotation marks) constructed from a color, a line style, and
a marker type:
• Color strings are 'c', 'm', 'y', 'r', 'g', 'b', 'w', and 'k'.
These correspond to cyan, magenta, yellow, red, green, blue, white, and black.
• Linestyle strings are '-' for solid, '--' for dashed, ':' for dotted, '-.' for dash-dot.
Omit the linestyle for no line.
• Marker types are '+', 'o', '*', and 'x' and the filled marker types are :
's' for square, 'd' for diamond, '^' for up triangle, 'v' for down triangle,
'>' for right triangle, '<' for left triangle, 'p' for pentagram, 'h' for hexagram,
and none for no marker.
>> x1 = 0:pi/100:2*pi;
>> x2 = 0:pi/10:2*pi;
>> plot(x1,sin(x1),'r:',x2,sin(x2),'r+')
Adding Plots to an Existing Graph
The hold command enables you to add plots to an existing graph. When you type
hold on
MATLAB does not replace the existing graph when you issue another plotting
command; it adds the new data to the current graph, rescaling the axes if
necessary.
For example, these statements first create a contour plot of the peaks function, then
superimpose a pseudocolor plot of the same function.
[x,y,z] = peaks;
contour(x,y,z,20,'k')
hold on
pcolor(x,y,z)
shading interp
hold off
The hold on command causes the pcolor
plot to be combined with the contour
plot in one figure.
Numerical Methods for Civil Engineers
Lecture 3 - MATLAB 3
Programming with MATLAB :
- Script m-files
- Function m-files
- Flow Control
Mongkol JIRAVACHARADET
Script Files
You can perform operations in MATLAB in two ways:
1. Enter commands directly in the Command Window.
2. Run a script M-file which contains all the commands – one at a time.
M-file is just a plain text file ended with extension “.m”
To open the built-in editor, select menu: File > New > M-file
A Script to Plot Functions
trigplot.m
t = linspace(0,2*pi);
y1 = sin(t);
y2 = cos(t);
y3 = y1.*y2;
plot(t,y1,’-’,t,y2,’.’,t,y3,’—’);
>> trigplot
Replace the plot statement with
plot(t,y1,’-’,t,y2,’:’,t,y3,’—’);
axis([0 2*pi -1.5 1.5])
legend(‘sin(t)’,’cos(t)’,’sin(t)*cos(t)’);
legend(‘sin(\theta)’,’cos(\theta)’,
’sin(\theta)*cos(\theta)’);
xlabel(‘\theta (radius)’,’FontName’,’Times’,
’FontSize’,14)
>> x = linspace(0,2*pi);
>> y1 = sin(x);
>> y2 = cos(x);
>> y3 = y1.*y2;
>> plot(x,y1,'-',x,y2,':',x,y3,'--');
>> axis([0 2*pi -1.5 1.5])
>>
legend('sin(\theta)','cos(\theta)','sin(\theta)*cos(\theta)')
>> xlabel('\theta (radius)','FontName','Times','FontSize',14)
>> title('Plot of simple trigonometric functions',...
'FontName','Times','FontSize',12)
Function m-Files
input parameters
Command Window
>>
function
output parameters
>> pyt(3,4)
FLOW CONTROL
MATLAB has several flow control constructs:
• if
• switch & case
• for
• while
• continue
• break
if Conditional Control
The if statement evaluates a logical expression and executes a group of statements
when the expression is true.
The optional elseif and else keywords provide for the execution of alternate groups
of statements.
An end keyword, which matches the if, terminates the last group of statements.
Condition:
if condition
Equal A == B
expression
Not equal A ~= B
elseif condition
Greater A>B
expression
Smaller A<B
else Greater or equal A >= B
expression Smaller or equal A <= B
end AND &
OR |
Example 1:
if a < 0
disp(‘a is negative’);
end
Example 2:
if a < 0, disp(‘a is negative’); end
Example 3:
if x >= y
c = x^2 - y;
elseif y/x > 2.0
c = log(y/x);
else
c = x + y;
end
switch & case
The switch statement executes groups of statements based on the value of a variable or
expression.
The keywords case and otherwise delineate the groups.
Only the first matching case is executed. There must always be an end to match the switch.
switch expression
case value1 switch sign(x)
block of statements case -1
disp(‘x is negative’);
case value2
case 0
block of statements disp(‘x is exactly zero’);
. case 1
. disp(‘x is positive’);
. otherwise
disp(‘sign test fail’);
otherwise
end
block of statements
end
for
The for loop repeats a group of statements a fixed,
predetermined number of times.
A matching end delineates the statements.
>> P
while
The while loop repeats a group of statements
an indefinite number of times under control of a logical condition.
>> x=1;
while condition >> while 1+x > 1
expressions x = x/2;
end
end >> x
LOOP
Break Loops break
return
break
Return Loops return
a = 0; fa = -Inf;
b = 3; fb = Inf; fb
while b-a > eps*b
x = (a+b)/2;
fx = x^3-2*x-5;
if sign(fx) == sign(fa)
a = x; fa = fx; x
else a b
b = x; fb = fx;
end
end fa
x
MATLAB targets the one that is designated the “current figure” (the last figure used
or clicked in).
To make an existing figure window the current figure, you can click the mouse while
the pointer is in that window or you can type
figure(n)
where n is the number in the figure title bar. The results of subsequent graphics
commands are displayed in this window.
To open a new figure window and make it the current figure, type
figure
>> t = 0:pi/10:2*pi;
>> [X,Y,Z] = cylinder(4*cos(t));
>> subplot(2,2,1); mesh(X)
>> subplot(2,2,2); mesh(Y)
>> subplot(2,2,3); mesh(Z)
>> subplot(2,2,4); mesh(X,Y,Z)
3-D Plot
First, create 1D vectors describing the grids in the x- and y-directions:
>> x = (0:2*pi/20:2*pi)';
>> y = (0:4*pi/40:4*pi)';
Next, ``spread'' these grids into two dimensions using meshgrid:
>> [X,Y] = meshgrid(x,y);
>> whos
Evaluate a function z = f(x,y) of two variables on the rectangular grid:
>> z = cos(X).*cos(2*Y);
Plotting commands:
>> mesh(x,y,z)
>> surf(x,y,z)
>> contour(x,y,z)
Numerical Methods for Civil Engineers
Lecture 4
Approximations and Errors
- Significant Figures - Truncation Errors
- Accuracy & Precision - Taylor Series
- Error Definitions - Order Notation
- Round-off Errors
Mongkol JIRAVACHARADET
π = 3.141592653589793238462643 . . .
UNCERTAINTY
ACCURACY
BIAS
Computes value
close to true value
Error Definitions
Approximation Error
Iterative approach for better and better approximations
x2 x3 xn
Approximate e0.5 by using the series e = 1+ x +
x
+ +L +
2! 3! n!
1st estimate: ex = 1
2nd estimate: ex = 1 + x e0.5 = 1 + 0.5 = 1.5
1.648721 − 1.5
true error: εt = × 100% = 9.02%
1.648721
1.5 − 1
approximate error: εa = × 100% = 33.3%
1.5
1 1 39.3
2 1.5 9.02 33.3
3 1.625 1.44 7.69
4 1.645833333 0.175 1.27
5 1.648437500 0.0172 0.158
6 1.648697917 0.00142 0.0158
After six terms are included, the approximate error falls below εa = 0.05%,
and the computation is terminated.
>> plot(Term,et,'o-‘,
Term(2:6),ea,'+-')
ROUND-OFF ERRORS
Precision : Computers retain only a fixed number of significant figures
All computations in MATLAB are done in double precision
1 1 = 20 0000 0001
2 2 = 21 0000 0010
4 4 = 22 0000 0100
8 8 = 23 0000 1000
9 8+1 = 23+20 0000 1001
10 8+2 = 23+21 0000 1010
27 16 + 8 + 2 + 1 = 24+23+23+21 0001 1011
>> dec2bin(27)
>> type dec2bin
Bits & Bytes
=0 =1
BYTE = Group of 8 bits
= 0001 1011
= 27
MAX BYTE NUMBER = 1111 1111 = 27+26+25+24+23+22+21+20 = 255
Integer Representation
1 0 0 0 0 0 1 0 1 1 0 1 0 0 1 1
Sign Number
Sign Mantissa
Sign
denormal
under under
overflow usable range usable range overflow
flow flow
dv ∆v v(ti +1 ) − v(ti )
≅ =
dt ∆t ti +1 − ti
dv
Velocity
Continuous
dt Discrete
∆v
∆t
Time
ti ti+1
dv c
= g − d v2
dt m
where v = vertical velocity (m/s), t = time (s),
g = gravity acceleration ( ≅ 9.81 m/s2)
cd = drag coefficient (kg/m)
m = jumper’s mass (kg)
Analytical solution by solving differential equation,
gm gcd ex − e− x
v(t ) = tanh t tanh( x) = x − x
cd m e +e
Example: Compute velocity of a free fall bungee jumper with a mass of 70 kg.
Use a drag coefficient of 0.25 kg/m.
9.81(70) 9.81(0.25)
v (t ) = tanh t = 52.41 tanh(0.1872t )
0.25 70
>> t=0:2:12;
>> v=52.41*tanh(0.1872*t);
>> [t’ v’] Analytical Solution for the bungee jumper problem
60
Terminal velocity
ans =
0 0 50
2.0000 18.7541
4.0000 33.2506 Velocity, m/s 40
6.0000 42.3829 30
8.0000 47.4160
10.0000 49.9874 20
12.0000 51.2501
10
>> plot(t,v) 0
0 2 4 6 8 10 12
Time, s
True slope
dv ∆v v (t i +1 ) − v (t i )
≅ = ∆v dv/dt
dt ∆t t i +1 − t i Approx. slope
∆v v (t i +1 ) − v (ti )
=
Substitute into dv/dt = g – (cd/m)v2 to give v(ti) ∆t t i +1 − t i
v (t i +1 ) − v (t i ) c
= g − d v (t i )2
t i +1 − t i m
ti ti+1 t
Rearrange equation to yield
∆t
c
v (t i +1 ) = v ( t i ) + g − d v (t i )2 (t i +1 − t i )
m
Euler’
Euler’s method:
dv i
v ( t i +1 ) = v (t i ) + ∆t New value = old value x step size
dt
Example 2: Numerical Solution to the Bungee Jumper Problem
Perform the same computation as previous example but use
the Euler’s method. Employ a step size ∆t = 2 sec.
c
v (t i +1 ) = v ( t i ) + g − d v (t i )2 (t i +1 − t i )
m
0.25 2
v ( 2) = 0 + 9.81 − (0) × 2 = 19.62 m/s
70
>> hold on
>> v2 = [0 19.62 36.49 46.6 50.71 51.96 52.3]
>> plot(t,v2,'+-')
40
Analytical solution
30
20
10
0
0 2 4 6 8 10 12
Time, s
TAYLOR SERIES
Predict function value of one point in term of function value
and its derivatives at other points
f(x)
f(xi) Zero-order approx.
f(xi+1) ≅ f(xi)
First o
rder
Known Se f(xi+1) ≅ f(xi) + f’(xi)h
co
nd
Tr or Slope @ xi
ue de
r
xi xi+1 x
h
N th-order Approximations
f ′′( x i ) 2 f ′′′( x i ) 3 f ( n ) ( xi ) n
f ( x i +1 ) ≅ f ( xi ) + f ′( x i )h + h + h +L+ h
2! 3! n!
Taylor series expansion:
f ′′( x i ) 2 f ′′′( x i ) 3 f ( n ) ( xi ) n
f ( x i +1 ) = f ( xi ) + f ′( x i )h + h + h +L+ h + Rn
2! 3! n!
f ( n +1) (ξ ) n +1
Remainder: Rn = h where xi < ξ < xi+1
(n + 1)!
Truncation error of order n + 1
f ( n +1) (ξ ) n +1
Rn = h = O(h n +1 ) = Truncation error of order hn+1
(n + 1)!
xi ξ xi+1
f(x) =Pn(x) + O(hn+1)
x2 xn
Example: e = 1+ x + +L + +L
x
2! n!
2
x
e x = 1 + x + + O( x3 ) → error of order x3
2!
x2 xn
e = 1 + x + + L + + O( x n +1 ) → error of order x n +1
x
2! n!
Exponential: f ( x) = e x
f ′′( xi ) 2 f ( n ) ( xi ) n
from f ( xi +1 ) = f ( xi ) + f ′( xi ) h + h + ... + h
2! n!
Set: xi+1 = x and xi = 0 so h = xi+1 - xi = x
f ( x) = e x , f ′( x) = e x , ..., f ( n) ( x) = e x
So
f (0) = e0 = 1, f ′(0) = 1, ..., f ( n) ( x) = 1
x 2 x3 xn
f ( x) = e = 1 + x + + + ... +
x
2! 3! n!
Taylor Series Expansions for Some Common Functions
x3 x5 x 2 n −1
sin( x) = x − + − L − (−1) n
for all x
3! 5! (2n − 1)!
x2 x4 n x
2n
cos( x) = 1 − + − L + (−1) for all x
2! 4! (2n)!
x 2 x3 xn
e = 1+ x + + + L +
x
for all x
2! 3! n!
x 2 x3 x 4 n x
n
ln(1 + x) = x − + − − L − (−1) -1 ≤ x ≤ 1
2! 3! 4! n!
x3 x5 x2n
arctan( x) = x − + − L − (−1) n
-1 ≤ x ≤ 1
3! 5! (2n − 1)!
p ( p − 1) 2 p ( p − 1)( p − 2) 3
(1 + x) p = 1 + px + x + x + L for x < 1
2! 3!
-0.5
-1
-1.5
-6 -4 -2 0 2 4 6
>> x = -2*pi:pi/30:2*pi;
>> y = sin(x);plot(x,y)
>> axis([-2*pi 2*pi -1.5 1.5])
>> hold on
>> y1 = x; plot(x,y1)
>> y2 = x-x.^3/6; plot(x,y2)
>> y3 = x-x.^3/6+x.^5/120; plot(x,y3)
>> legend('sin(x)','n = 1','n = 2','n = 3')
Example: Approximation of function cos(x) by Taylor Series Expansion
Solution:
0.5 − 0.707106781
εt = × 100% = 41.4%
0.5
0 0.707106781 41.4
1 0.521986659 4.40
2 0.497754491 0.449
3 0.499869147 2.62 x 10-2
4 0.500007551 1.51 x 10-3
5 0.500000304 6.08 x 10-5
6 0.499999988 2.44 x 10-6
Example: Evaluating the Series for sin(x)
x3 x5 x 2 n −1
sin( x) = x − + − L − (−1) n
3! 5! (2n − 1)!
− x2
Recursion: Tk = Tk − 2 , k = {1, 3, 5,...}
k (k − 1)
for k=3:2:(2*nmax-1)
term = -term*x*x/(k*(k-1));
ssum = ssum + term;
fprintf(‘%3d %11.3e %12.8f\n’,k,term,ssum);
if abs(term/ssum)<tol, break; end
end
fprintf(‘\nTruncation error after %d terms is %g\n\n’,
(k+1)/2,abs(ssum-sin(x)));
>> sinser(pi/6);
Numerical Methods for Civil Engineers
Lecture 5
Roots of Equations
- Graphical Methods
- Fixed-
Fixed-Point Iteration
- Bisection Method
- Newton’s Method
- Secant Method
fx = - Roots of Polynomials
Mongkol JIRAVACHARADET
f(x)
− b ± b 2 − 4ac
x=
2a
x
0 x1 x2
gm −
ct
dv c
=g− v v (t ) = 1 − e m
dt m c
if v = 0 at t = 0
gm/c
v(t)
t
Problem: From the velocity equation of the turtle, find the value of the drag
coefficient c such that a turtle of mass m = 5 can attain a prescribed velocity,
v = 10, at a set period of time, t = 9. Use g = 10.
gm − 50
ct 9c
−
f (c ) = 1 − e m − v (t ) f (c ) = 1 − e 5 − 10
c
c
So our task of solving the problem reduces to merely finding the value of c,
such that f( c ) =0, for the given values of v, m and t.
Graphical method
x
Graphical method : NOT precise
ZOOM
ZOOM
Double root?
Bracketing Method
f (x) + + + + - - - + +
+++++++++ ++++
----------
x1 x2 x
Possible ways in an interval
Part (a) and (c) : f(xl) and f(xu) same sign
Exceptions:
Bracketing Algorithm
Given: f (x), xmin, xmax, n
dx = (xmax-xmin)/n % Size of bracket interval
xa = xmin % Initialize left side of test bracket
i = 0 % Initialize counter
while i < n
i = i + 1
xb = xa + dx
if f (xa) and f (xb) have different sign
save [ xa , xb ] for further root finding
end
xa = xb
end
Detecting Sign Change : f (xa) & f (xb)
1st Solution: f (xa) × f (xb) < 0 ?
>> format long e
>> fa = 1e-120; fb = -2e-300;
>> fa * fb < realmin
ans = 0
x f(x)
4 34.115 20
8 17.653
12 6.067
0.00 16
16 -2.269 0
4 8 12 20 x
20 -8.401
-10
6.607
12 16 2.269
6.607
Interpolation: x = 12 + (16 − 12) = 14.977 f (14.977) = -0.3691
6.607 − (−2.269)
1.582
2nd Interpolation: x = 14 + (15 − 14) = 14.793 f (14.793) = -0.013
1.582 − (−0.4133)
§ Graphical method
f(x)
f(x) = 0
§ False-position method
x=?
§ Bisection method
§ Fixed-point iteration
§ Newton-Raphson method
§ Secant method
Fixed-Point Iteration
1) Rewrite original equation f(x) = 0 into another form x = g(x).
x2 + 3
Example: x − 2x + 3 = 0
2
⇒ x =
2
sin x = 0 ⇒ x = sin x + x
2) Select initial value x0
xi +1 − xi
εa = ×100%
xi +1
2 0.3679 171.8
0.6
3 0.6922 46.9
4 0.5005 38.3
5 0.6062 17.4 0.4
6 0.5454 11.2
7 0.5796 5.90 0.2
8 0.5601 3.48
9 0.5711 1.93 0
10 0.5649 1.11 0 1 2 3 4 5 6 7 8 9 10 11 12
Number of Iteration
>> 0
>> exp(-ans)
Two-Curve Graphical Method
1 1
0.8 f ( x) = e − x
0.6 f ( x) = e− x − x 0.8
0.4
f(x) = exp(-x) - x
Convergence
1
y2 = g(x) y1 = x
0.8
0.6922
0.6062
0.6
y 0.5005
0.4
0.3679
0.2
0
0 0.2 0.4 0.6 0.8 1
x
Divergence
1
0.8
y1 = x
y2 = g(x)
0.6
y
0.4
0.2
x0
0
0 0.2 0.4 0.6 0.8 1
x
Bisection Method
Repeatedly halve interval while bracketing root
f (x) +
+
xb
xa xm x
-
-
1) Choose interval [ xa , xb ] which has sign-change
2) Compute midpoint of interval xm = ( xa + xb ) / 2
3) Select subinterval which has sign-change and repeat 2)
Example: Use bisection method find the root of equation
(1 − e −0.147 x ) − 40
667.38 True value of the root:
f ( x) = 14.7802
x
Estimate root at midpoint: f(x)
40
12 + 16
xr = = 14
2
14.7802 − 14 Initial interval
εt = × 100% = 5.279% 20
14.7802 [ 12 , 16 ]
15 − 14
Next estimation: xr = (14+16)/2 = 15 εa = ×100% = 6.667%
14
< εs = 0.5%
Bisection Algorithm
initialize: a = . . . , b = . . .
for k = 1, 2, . . . less susceptible to
xm = a + (b-a)/2 roundoff error than
if sign(f (xm)) = sign(f (a)) (a+b)/2
a = xm
else
b = xm
end
if converged, stop
end
slope = f’(x)
f(xi)
Root
xi+1 xi x
f ( xi )
xi − xi +1 =
f ′( xi )
f(x)
f(x1)
x2
x3 x1 x
f(x2)
Failure of Newton’s Method
Case 1: Inflection point in vicinity of root
f(x)
x2 x1 x3
x
f(x)
x
Failure of Newton’s Method
Case 3: Jump away for several roots
f(x)
f(x)
x
Example: Apply Newton’s Method to x - x1/3 - 2 = 0
newton.m
function x = newton(x0, n)
if nargin<2, n=5; end % Default number of iterations
x = x0; % Initial guess
fprintf(‘ k f(x) dfdx x(k+1)\n’);
for k = 1:n
f = x - x.^(1/3) - 2; % Function value
dfdx = 1 - (1/3)*x.^(-2/3); % Derivative value
x = x - f/dfdx;
fprintf(‘%3d %12.3e %12.3e %18.15f\n’, k-1, f, dfdx, x);
end
Secant Method
for function whose derivatives are difficult to evaluate
Derivative approximated by backward finite difference
f ( xi −1 ) − f ( xi ) xi +1 = xi −
f ( xi )
f ′ ( xi ) ≅
xi −1 − xi f ′( xi )
f ( xi ) ( xi −1 − xi )
f(xi)
xi +1 = xi −
f ( xi −1 ) − f ( xi )
f(xi-1)
x
xi-1 xi
Example: Use secant method to estimate root of e-x - x = 0
Initial estimate x-1 = 0 and x0 = 1.0
True root = 0.56714329. . .
First iteration:
x-1 = 0 f (x-1) = 1.00000
x0 = 1 f (x0) = -0.63212
−0.63212(0 − 1)
x1 = 1 − = 0.61270 ε t = 8.0%
1 − ( −0.63212)
Second iteration:
x0 = 1 f (x0) = -0.63212
x1 = 0.61270 f (x1) = -0.07081
−0.07081(1 − 0.61270)
x2 = 0.61270 − = 0.56384 ε t = 0.58%
−0.63212 − (−0.07081)
Third iteration:
x1 = 0.61270 f (x1) = -0.07081
x2 = 0.56384 f (x2) = 0.00518
0.00518(0.61270 − 0.56384)
x3 = 0.56384 − = 0.56717
−0.07081 − ( −0.00518)
ε t = 0.0048%
Roots of Polynomials
f n ( x ) = a0 + a1 x + a2 x 2 + L + an x n
where n = order of polynomial
a’s = constant coefficients
Roots of polynomials:
(1) nth-order equation has n real or complex roots
(2) If n is odd, at least one root is real.
d2y dy
a2 2 + a1 + a0 y = F (t )
dt dt
d2y dy
General Solution: a2 2 + a1 + a0 y = 0
dt dt
f 2 ( x) = x 2 − 10 x + 25
r1 10 ± 102 − 4(25) 5 Repeated real roots
= =
r2 2 5
f3 ( x) = x 2 − 17 x + 72.5
Complex roots
r1 17 ± 17 2 − 4(72.5) 8.5 + 0.5i
= =
r2 2 8.5 − 0.5i
Roots of Polynomials
3
2.5
1.5
y = f(x)
0.5
-0.5
distinct repeated complex
real roots real roots roots
-1
0 2 4 6 8 10
x
MATLAB’s roots Function
Compute eigenvalues of companion matrix
4th-order polynomial: c5 λ 4 + c4 λ 3 + c3λ 2 + c2λ + c1 = 0
−c2 / c1 −c3 / c1 −c4 / c1 −c5 / c1
1 0 0 0
Companion
A=
Matrix 0 1 0 0
0 0 1 0
Eigenvalue problem Aυ = λυ
f1 ( x) = x 2 − 3 x + 2 → root([1 -3 2])
f 2 ( x) = x 2 − 10 x + 25 → root([1 -10 25])
f3 ( x) = x 2 − 17 x + 72.5 → root([1 -17 72.5])
Numerical Methods for Civil Engineers
Lecture 6
System of Linear Algebraic Equations
- Basic Concepts
- Gaussian Elimination
- Backward Substitution
- LU Decomposition
Mongkol JIRAVACHARADET
n unknowns
Example: c 1 + 3c 2 − 5c 3 = 7
2c 1 − 8c 2 + 3c 3 = −2
5c 1 + 7c 2 − 4c 3 = 9
1 3 − 5 c 1 7 1 3 − 5 7
2 − 8 3 c = − 2 2 − 8 3 − 2
2
5 7 − 4 c 3 9 5 7 − 4 9
Symbolic Notation A x = b
Solution : Ax = b MATLAB :
A-1A x = A-1b - Use matrix inversion,
m=n
Solving Ax = b for n unknowns from n equations
m>n
Overdetermined systems
(e.g., least-squares problems)
m<n
Underdetermined systems
(e.g., optimization)
Matrix Rank
rank(A) = number of linearly independent columns in A
>> A = [3 -4 1;6 10 2;9 -7 3];
>> rank(A)
ans =
M M M M
am1 am 2 amn bm
x 1 2 3
Experiment data: Practical Model: y = α x + β
y 2 1 0.5
Data → Model:
3
α + β = 2
2 2α + β = 1
1 3α + β = 0.5
0
Matrix Form:
1 1 2
-1 2 1 α 1
β =
-2 3 1 0.5
0 1 2 3 4
Consistency test: [α β ] = [− 1 3 ]
T T
2 Exact solution
1
-1
-2
0 1 2 3 4
Nonsingular Case
Ax =b x = A-1 b
§ This solution is therefore unique. Also, if b = 0, it follows that the unique
-1
solution to Ax = 0 is x = A 0 = 0.
The most basic systematic scheme for solving system of linear equations.
The procedure consisted of two steps:
elimination backward
Backward substitution
x3 = b3′′ / a33
′′
x2 = (b2′ − a23
′ x 3 ) / a22
′
Substitutions
bn
xn =
ann
n
1
xi = (bi − ∑ aij x j ), i = n − 1, n − 2, L, 2, 1
aii j = i +1
− 3 x1 + 2 x2 − x3 = −1 − 3 2 − 1 − 1 R1
6 x1 − 6 x2 + 7 x3 = −7 ⇒ 6 − 6 7 − 7 R2
3 x1 − 4 x2 + 4 x3 = −6 3 − 4 4 − 6 R3
− 3 2 − 1 − 1 Pivot row
0 − 2 5 − 9
R2 + 2×R1, R3+R1: ⇒
0 − 2 3 − 7
− 3 2 − 1 − 1
R3 - R2: ⇒
0 −2 5 − 9
0 0 −2 2
− 3 2 − 1 − 1 − 3 x1 + 2 x 2 − x 3 = −1
0 −2 5 − 9 ⇒ − 2 x 2 + 5 x3 = −9
0 0 −2 2 − 2 x3 = 2
Backward substitution:
1
− 3 x1 + 2 x2 − x3 = −1 ⇒ x1 = ( −1 − 2 x2 + x3 ) = 2
−3
1
− 2x 2 + 5 x3 = −9 ⇒ x2 = ( −9 − 5 x3 ) = 2
−2
2
− 2x3 = 2 ⇒ x3 = = −1
−2
PIVOTING
Exchange row to avoid zero pivot element
2 4 −2 −2 − 4 2 4 −2 −2 − 4
0 0 5 −2 7 0 3 5 −4 5
0 3 5 −5 1 0 3 5 −5 1
0 3 5 −4 5 0 0 5 −2 7
R3=R3-R2
2 4 −2 −2 − 4 2 4 −2 −2 − 4
0 3 5 −4 5 0 3 5 −4 5
0 0 0 −1 −4 0 0 5 −2 7
0 0 5 −2 7 0 0 0 −1 4
Solving Systems with the Backslash Operator
MATLAB
Ax =b x = A-1 b x =A\b
x =
1.0000
2.0000
3.0000
4.0000
LU Decomposition
Gaussian elimination:
1 − 2 3
(2)
0 − 1 6
R2 – 2×R1
0 2 − 10
For recording
Gaussian elimination (con’t): Lower triangular matrix:
1 − 2 3 1 0 0
R3 – 0×R1 (2) − 1 6
⇒ L = 2 1 0
R3 + 2×R1 (0 ) (−2) 2 0 − 2 1
1 0 0 1 − 2 3 1 − 2 3
LU = 2 1 0 0 − 1 6 = 2 − 5 12 = A
0 − 2 1 0 0 2 0 2 − 10
Ly = b : Forward substitution
Ax = b → LUx = b
Ux = y : Backward substitution
For Gauss elimination with pivoting:
1 0 0 1 − 2 3 1 − 2 3
0 0 1 2 − 5 12 = 0 2 − 10
0 1 0 0 2 − 10 2 − 5 12
Ly = b’
PAx = Pb ≡ b’ → LUx = b’
Ux = y
Gauss elimination as LU decomposition
1 0 0 1 0 0
0 = − 2 1 0
Lower triangular matrix, L = f21 1
f31 f32 1 − 1 1 1
Check LU = A or not ?
Using LU to solve equations (con’t)
Ly = b
Ax = b → LUx = b
− 3 2 − 1 x1 − 1 Ux = y
6 − 6 7 x = − 7
2
3 − 4 4 x2 − 6
Ly = b
1 0 0 y1 b1 1 0 0 y 1 − 1
f 0 y = b − 2 1 0 y = − 7
21 1 2 2 2
f31 f32 1 y 3 b3 − 1 1 1 y 3 − 6
y1 = b1 y1 = −1
y 2 = b2 − y 1 f21 y 2 = −7 − ( −1)(−2) = −9
Ux = y
a11 a12 a13 x1 y1 − 3 2 − 1 x1 − 1
0 a′ ′ x2 = y 2
a23 0 − 2 5 x = − 9
22 2
0 0 ′′ x3 y 3
a33 0 0 − 2 x3 2
y3 2
x3 = x3 = = −1
′′
a33 −2
′
y 2 − x3 a23 − 9 − ( −1)(5)
x2 = x2 = =2
′
a22 −2
y 1 − x 2 a12 − x3 a13 − 1 − 2 × 2 − ( −1)( −1)
x1 = x1 = =2
a11 −3
MATLAB Function: lu
− 3 2 − 1 x1 − 1
6 − 6 7 x = − 7
2
3 − 4 4 x2 − 6
LU Decomposition:
Solve equations:
>> y = L\b
>> x = U\y
Various LU decompositions
Crout’
Crout’s reduction (U has ones on the diagonal)
l11 0 0 1 u12 u13 a11 a12 a13
l 0 × 0 1 u23 = a21 a22 a23
21 l 22
l 31 l 32 l 33 0 0 1 a31 a32 a33
Doolittle’
Doolittle’s method (L has ones on the diagonal)
1 0 0 u11 u12 u13 a11 a12 a13
l 0 × 0 u 22 u23 = a21 a22 a23
21 1
l 31 l 32 1 0 0 u33 a31 a32 a33
Cholesky’
Cholesky’s method (The diagonal terms are the same value for
the L and U matrices)
l11 0 0 u11 u12 u13 a11 a12 a13
l 0 × 0 u 22 u 23 = a21 a22 a23
21 l 22
l 31 l 32 l 33 0 0 u33 a31 a32 a33
i −1
uii = aii − ∑ uki2
k =1
i −1
aij − ∑ uki ukj
uij = k =1
for j = i + 1, K, n
uii
2 4 27 4 8 27
Thus, Cholesky yields U = 0 4 27 ⇒ U T U = 8 32 216 = A
0 0 6 27 216 1,494
MATLAB Function: chol
>> U = chol(A)
4 8 54
A = 8 32 216
54 216 1,494
Cholesky Decomposition:
Test:
>> U’*U
90o
0.866 m
[ΣFx = 0] H2 = 0 F1
F3
@ node 1 :
F2
V2 V3
[ΣFx = 0] F1cos30o – F3cos60o = 0
1.5 m 0.5 m
[ΣFy = 0] F1sin30o + F3sin60o = 1000
@ node 3 :
@ node 2 :
[ΣFx = 0] – F2 + F3cos60o = 0
[ΣFx = 0] – F1cos30o + F2 = 0
[ΣFy = 0] F3 = 750/sin60o = 866 N
[ΣFy = 0] F1sin30o = 250
F2 = 866cos60o = 433 N
F1 = 250/sin30o = 500 N
F1sin30o + F3sin60o = 1000 sin 30° 0 sin 60° F1 1,000
− cos 30° 1 0 F2 = 0
– F1cos30o + F2 = 0
0 − 1 cos 60° F3 0
– F2 + F3cos60o = 0
>> b=[1000;0;0]
>> F=A\b
F =
500.0000
433.0127
866.0254
Numerical Methods for Civil Engineers
Mongkol JIRAVACHARADET
SURANAREE INSTITUTE OF ENGINEERING
UNIVERSITY OF TECHNOLOGY SCHOOL OF CIVIL ENGINEERING
LINEAR REGRESSION
We want to find the curve that will fit the data.
y Observation: [ xi yi ]
Model: y = α x + β
Error: ei = yi – α xi – β
x
Criteria for a “Best” Fit
Find the BEST line which minimize the sum of error for all data
ei = y i − yˆ
Where
ŷ = α x i + β However, the errors can
cancel one another and
still be wrong.
ERROR Definition
ei = y i − yˆ
But, the error minimization is going to have problems.
The solution is the minimization of the sum of squares.
S = ∑ (ei )
2
0 = ∑ y i − ∑ β − ∑ αx i
0 = ∑ y i x i − ∑ βx i − ∑ αx i2
nβ + α ∑ x i = ∑ y i
β ∑ x i + α ∑ x i2 = ∑ y i x i
1
∑ xi yi − ∑ xi ∑ yi S xy
α= n α=
1 S xx
∑ xi2 − ( ∑ xi )
2
β = y −α x
where y and x are the mean of y and x
1
Define: S xy = Σxi yi − Σxi Σyi
n Approximated y for any x is
1
S xx = Σxi2 − ( Σxi ) ŷ = α x + β
2
n
1
S yy = Σyi2 − ( i)
Σ
2
y
n
Example: Fit a straight line to x and y values
xi yi xi2 xi yi yi2
n =7
1 0.5 1 0.5 0.25
2 2.5 4 5.0 6.25 28
3 2.0 9 6.0 4 x = =4
4 4.0 16 16.0 16 7
5 3.5 25 17.5 12.25
6 6.0 36 36.0 36 24
7 5.5 49 38.5 30.25 y = = 3 . 4286
7
Σ 28 24 140 119.5 105
(119.5) − (28)(24) / 7
α= = 0.8393 Least-square fit:
(140) − (28) 2 / 7
y = 0 . 8393 x + 0 . 0714
β = 3.4286 − 0.8393(4) = 0.0714
S r = ∑ e = ∑ ( yi − β − α xi ) = ( S xx S yy − S xy2 ) / S xx
n n
2 2
i
i =1 i =1
n
St = ∑ ( yi − y ) = S yy
2
Sum of the square around the mean:
i =1
Sr
Standard errors of the estimation: sy / x =
n−2
St
Standard deviation: sy =
n−2
Linear regression
sy > sy/x
sy sy/x
y
St − S r S xy2
Coefficient of determination r2 = =
St S xx S yy
r2 คืออัตราสวนการแปรเปลี่ยนคา y ที่เกิดจากการเปลี่ยนคา x
xi yi x i2 xi yi y i2
S xx = 140 − 282 / 7 = 28
1 0.5 1 0.5 0.25
2 2.5 4 5.0 6.25 S yy = 105 − 242 / 7 = 22.7
3 2.0 9 6.0 4
4 4.0 16 16.0 16
5 3.5 25 17.5 12.25 S xy = 119.5 − 28 × 24 / 7 = 23.5
6 6.0 36 36.0 36
7 5.5 49 38.5 30.25 Sr = (28 × 22.7 − 23.52 ) / 28
Σ 28 24 140 119.5 105 = 2.977
yˆ i ± ∆
yˆ i
y y
x xi x
For CI 95%, you can be 95% confident that the two curved
confidence bands enclose the true best-fit linear regression line,
leaving a 5% chance that the true line is outside those boundaries.
A 100 (1 - α) % confidence interval for yi is given by
Confidence interval 95% → α = 0.05
1 ( xi − x ) 2
yˆ i ± tα / 2 s y / x +
n S xx
T-Distribution
Probability density function of
t 0.025 t 0.025 the t distribution:
t 0.005 t 0.005
(1 + x 2 /ν ) − (ν +1) / 2
f ( x) =
t
B(0.5, 0.5ν ) ν
95% where B is the beta function and
99% ν is a positive integer
shape parameter.
ν = df
Degree of freedom
Critical Values of t
Confidence Interval
80% 90% 95% 98% 99% 99.8%
df 0.10 0.05 0.025 0.01 0.005 0.001
Confidence Interval
80% 90% 95% 98% 99% 99.8%
df 0.10 0.05 0.025 0.01 0.005 0.001
Confidence Interval
80% 90% 95% 98% 99% 99.8%
df 0.10 0.05 0.025 0.01 0.005 0.001
y = a0 + a1x + a2 x2
∂S r
= −2 ∑ x i ( y i − a 0 − a 1 x i − a 2 x i2 )
∂a 1
∂S r
= −2 ∑ x i2 ( y i − a 0 − a 1 x i − a 2 x i2 )
∂a 2
Normal equations:
( )
n a 0 + (∑ x i )a 1 + ∑ x i2 a 2 = ∑ y i
(∑ x i )a 0 + (∑ x i2 )a + (∑ x )a
1 i
3
2 = ∑ x iy i
(∑ x )a + (∑ x
i
2
0 i
3
)a + (∑ x )a
1 i
4
2 = ∑ x i2 y i
( yi − y )
2
xi yi ( yi - a0 - a1xi - a2xi2)2
0 2.1 544.44 0.14332
1 7.7 314.47 1.00286
2 13.6 140.03 1.08158
3 27.2 3.12 0.80491
4 40.9 239.22 0.61951
5 61.1 1272.11 0.09439
2513.39 − 3.74657
r= = 0.99851 = 0.99925
2513.39
>> x = [0 1 2 3 4 5];
>> y = [2.1 7.7 13.6 27.2 40.9 61.1];
>> c = polyfit(x, y, 2)
>> [c, s] = polyfit(x, y, 2)
>> st = sum((y - mean(y)).^2)
>> sr = sum((y - polyval(c, x)).^2)
>> r = sqrt((st - sr) / st)
MATLAB polyval Function
Evaluate polynomial at the points defined by the input vector
>> y = polyval(c, x)
where x = Input vector
y = Value of polynomial evaluated at x
c = vector of coefficient in descending order
Polynomial Interpolation
70
60
50
40
y
30
20
10
0
0 1 2 3 4 5
x
>> y2 = polyval(c,x)
>> plot(x, y, ’o’, x, y2)
Error Bounds
>> plot(x,y,'o',x,y2,'g-',x,y2+2*delta,'r:',x,y2-2*delta,'r:')
xi yi
1 0.5
2 2.5
3 2.0
4 4.0
5 3.5
6 6.0
7 5.5
>> plot(x,y,'o',x,y2,'g-',x,y2+2*delta,'r:',x,y2-2*delta,'r:')
Multiple Linear Regression
Example:
x1 x2 y 6 16.5 14 c0 54
16.5
76.25 48 c1 = 243.5
0 0 5
2 1 10 14 48 54 c2 100
2.5 2 9
1 3 0
c0 = 5
4 6 3 c1 = 4
7 2 27
c2 = −3
Multivariate Fit in MATLAB
c0 + c1x11 + c2x12 + . . . + cpx1p = y1
c0 + c1x21 + c2x22 + . . . + cpx2p = y2
.
.
.
c0 + c1xm1 + c2xm2 + . . . + cpxmp = ym
Overdetermined system of equations: A c = y
x11 x12 L x1 p 1 c0 y1
x x22 L x2 p 1 c y
A= , c = , and y = 2
21 1
M M O M M M M
xm1 xm 2 L xmp 1 p
c ym
Fit norm >> c = (A’*A)\(A’*y)
Fit QR >> c = A\y
Example:
x1 x2 y
0 0 5
2 1 10
2.5 2 9
1 3 0
4 6 3
7 2 27
Mongkol JIRAVACHARADET
SURANAREE INSTITUTE OF ENGINEERING
UNIVERSITY OF TECHNOLOGY SCHOOL OF CIVIL ENGINEERING
LINEAR REGRESSION
We want to find the curve that will fit the data.
y Observation: [ xi yi ]
Model: y = α x + β
Error: ei = yi – α xi – β
x
Criteria for a “Best” Fit
Find the BEST line which minimize the sum of error for all data
ei = y i − yˆ
Where
ŷ = α x i + β However, the errors can
cancel one another and
still be wrong.
ERROR Definition
ei = y i − yˆ
But, the error minimization is going to have problems.
The solution is the minimization of the sum of squares.
S = ∑ (ei )
2
0 = ∑ y i − ∑ β − ∑ αx i
0 = ∑ y i x i − ∑ βx i − ∑ αx i2
nβ + α ∑ x i = ∑ y i
β ∑ x i + α ∑ x i2 = ∑ y i x i
1
∑ xi yi − ∑ xi ∑ yi S xy
α= n α=
1 S xx
∑ xi2 − ( ∑ xi )
2
β = y −α x
where y and x are the mean of y and x
1
Define: S xy = Σxi yi − Σxi Σyi
n Approximated y for any x is
1
S xx = Σxi2 − ( Σxi ) ŷ = α x + β
2
n
1
S yy = Σyi2 − ( i)
Σ
2
y
n
Example: Fit a straight line to x and y values
xi yi xi2 xi yi yi2
n =7
1 0.5 1 0.5 0.25
2 2.5 4 5.0 6.25 28
3 2.0 9 6.0 4 x = =4
4 4.0 16 16.0 16 7
5 3.5 25 17.5 12.25
6 6.0 36 36.0 36 24
7 5.5 49 38.5 30.25 y = = 3 . 4286
7
Σ 28 24 140 119.5 105
(119.5) − (28)(24) / 7
α= = 0.8393 Least-square fit:
(140) − (28) 2 / 7
y = 0 . 8393 x + 0 . 0714
β = 3.4286 − 0.8393(4) = 0.0714
S r = ∑ e = ∑ ( yi − β − α xi ) = ( S xx S yy − S xy2 ) / S xx
n n
2 2
i
i =1 i =1
n
St = ∑ ( yi − y ) = S yy
2
Sum of the square around the mean:
i =1
Sr
Standard errors of the estimation: sy / x =
n−2
St
Standard deviation: sy =
n−2
Linear regression
sy > sy/x
sy sy/x
y
St − S r S xy2
Coefficient of determination r2 = =
St S xx S yy
r2 คืออัตราสวนการแปรเปลี่ยนคา y ที่เกิดจากการเปลี่ยนคา x
xi yi x i2 xi yi y i2
S xx = 140 − 282 / 7 = 28
1 0.5 1 0.5 0.25
2 2.5 4 5.0 6.25 S yy = 105 − 242 / 7 = 22.7
3 2.0 9 6.0 4
4 4.0 16 16.0 16
5 3.5 25 17.5 12.25 S xy = 119.5 − 28 × 24 / 7 = 23.5
6 6.0 36 36.0 36
7 5.5 49 38.5 30.25 Sr = (28 × 22.7 − 23.52 ) / 28
Σ 28 24 140 119.5 105 = 2.977
yˆ i ± ∆
yˆ i
y y
x xi x
For CI 95%, you can be 95% confident that the two curved
confidence bands enclose the true best-fit linear regression line,
leaving a 5% chance that the true line is outside those boundaries.
A 100 (1 - α) % confidence interval for yi is given by
Confidence interval 95% → α = 0.05
1 ( xi − x ) 2
yˆ i ± tα / 2 s y / x +
n S xx
T-Distribution
Probability density function of
t 0.025 t 0.025 the t distribution:
t 0.005 t 0.005
(1 + x 2 /ν ) − (ν +1) / 2
f ( x) =
t
B(0.5, 0.5ν ) ν
95% where B is the beta function and
99% ν is a positive integer
shape parameter.
ν = df
Degree of freedom
Critical Values of t
Confidence Interval
80% 90% 95% 98% 99% 99.8%
df 0.10 0.05 0.025 0.01 0.005 0.001
Confidence Interval
80% 90% 95% 98% 99% 99.8%
df 0.10 0.05 0.025 0.01 0.005 0.001
Confidence Interval
80% 90% 95% 98% 99% 99.8%
df 0.10 0.05 0.025 0.01 0.005 0.001
y = a0 + a1x + a2 x2
∂S r
= −2 ∑ x i ( y i − a 0 − a 1 x i − a 2 x i2 )
∂a 1
∂S r
= −2 ∑ x i2 ( y i − a 0 − a 1 x i − a 2 x i2 )
∂a 2
Normal equations:
( )
n a 0 + (∑ x i )a 1 + ∑ x i2 a 2 = ∑ y i
(∑ x i )a 0 + (∑ x i2 )a + (∑ x )a
1 i
3
2 = ∑ x iy i
(∑ x )a + (∑ x
i
2
0 i
3
)a + (∑ x )a
1 i
4
2 = ∑ x i2 y i
( yi − y )
2
xi yi ( yi - a0 - a1xi - a2xi2)2
0 2.1 544.44 0.14332
1 7.7 314.47 1.00286
2 13.6 140.03 1.08158
3 27.2 3.12 0.80491
4 40.9 239.22 0.61951
5 61.1 1272.11 0.09439
2513.39 − 3.74657
r= = 0.99851 = 0.99925
2513.39
>> x = [0 1 2 3 4 5];
>> y = [2.1 7.7 13.6 27.2 40.9 61.1];
>> c = polyfit(x, y, 2)
>> [c, s] = polyfit(x, y, 2)
>> st = sum((y - mean(y)).^2)
>> sr = sum((y - polyval(c, x)).^2)
>> r = sqrt((st - sr) / st)
MATLAB polyval Function
Evaluate polynomial at the points defined by the input vector
>> y = polyval(c, x)
where x = Input vector
y = Value of polynomial evaluated at x
c = vector of coefficient in descending order
Polynomial Interpolation
70
60
50
40
y
30
20
10
0
0 1 2 3 4 5
x
>> y2 = polyval(c,x)
>> plot(x, y, ’o’, x, y2)
Error Bounds
>> plot(x,y,'o',x,y2,'g-',x,y2+2*delta,'r:',x,y2-2*delta,'r:')
xi yi
1 0.5
2 2.5
3 2.0
4 4.0
5 3.5
6 6.0
7 5.5
>> plot(x,y,'o',x,y2,'g-',x,y2+2*delta,'r:',x,y2-2*delta,'r:')
Multiple Linear Regression
Example:
x1 x2 y 6 16.5 14 c0 54
16.5
76.25 48 c1 = 243.5
0 0 5
2 1 10 14 48 54 c2 100
2.5 2 9
1 3 0
c0 = 5
4 6 3 c1 = 4
7 2 27
c2 = −3
Multivariate Fit in MATLAB
c0 + c1x11 + c2x12 + . . . + cpx1p = y1
c0 + c1x21 + c2x22 + . . . + cpx2p = y2
.
.
.
c0 + c1xm1 + c2xm2 + . . . + cpxmp = ym
Overdetermined system of equations: A c = y
x11 x12 L x1 p 1 c0 y1
x x22 L x2 p 1 c y
A= , c = , and y = 2
21 1
M M O M M M M
xm1 xm 2 L xmp 1 p
c ym
Fit norm >> c = (A’*A)\(A’*y)
Fit QR >> c = A\y
Example:
x1 x2 y
0 0 5
2 1 10
2.5 2 9
1 3 0
4 6 3
7 2 27
Lecture 8 Interpolation
Linear Interpolation
Quadratic Interpolation
Polynomial Interpolation
Piecewise Polynomial Interpolation
Mongkol JIRAVACHARADET
SURANAREE INSTITUTE OF ENGINEERING
UNIVERSITY OF TECHNOLOGY SCHOOL OF CIVIL ENGINEERING
Visual Interpolation
60
40 80
20 100
km/h
0 120
y
known data
What is the
corresponding value
of y for this x ?
BASIC IDEAS
From the known data ( xi , yi ), interpolate yˆ = F ( xˆ ) for xˆ ≠ x i
x1 y1 y F(x)
M M
xi y i interpolate ŷ
xˆ → yˆ
x i +1 y i +1
M M
x
xn yn x̂
Determining coefficient a1, a2, . . . , an of basis function F(x)
F(x) = a1 + a2 x + a3 x2 + . . . + an xn-1
Interpolation v.s. Curve Fitting
y known data
curve fit
interpolation
x
Curve fitting: fit function & data not exactly agree
Interpolation: function passes exactly through known data
x1 x2 x
Linear Interpolation
Interpolated Point
y1
Using similar triangles:
F ( xˆ ) = yˆ yˆ − y 0 y − y0
= 1
xˆ − x0 x1 − x 0
y0
x0 x̂ x1
y1 − y 0
F ( xˆ ) = yˆ = y 0 + ( xˆ − x0 )
x1 − x0
Quadratic Interpolation
Second-order polynomial interpolation using 3 data points
1.7918 − 0
f1(2) = 0 + ( 2 − 1) = 0.3584 → ε t = 48 .3%
6 −1
Linear interpolation from x1 = 1 to x2 = 4
1.3863 − 0
f1(2) = 0 + ( 2 − 1) = 0.4621 → ε t = 33 .3%
4 −1
Quadratic interpolation from x1 = 1 to x2 = 4 and x3 = 6
1.3863 − 0
b0 = 0 , b1 = = 0.4621
4 −1
1.7918 − 1.3863
− 0.4621
b3 = 6 − 4 = −0.05187
6 −1
1 2 3 4 5 6 7
Polynomial Interpolation
Finding Pn-1(x) of degree n-1 that passes through n known data pairs
Linear = 1 : 2 pt.
Pn-1(x) = c1xn-1 + c2xn-2 + . . . + cn-1x + cn Quadratic = 2 : 3 pt.
Cubic = 3 : 4 pt.
y = c1x2 + c2x + c3
that pass through (x, y) support points (-2, -2), (-1, 1), and (2, -1)
MATLAB:
>> x = [-2 -1 2]’;
>> A = [x.^2 x ones(size(x))];
or use the built-in vander function
>> A = vander([-2 -1 2]);
>> y = [-2 1 -1]’;
>> c = A\y
c =
-0.9167
0.2500
2.1667
>> polyval(c,1)
ans =
1.5000
Also, we can use the polyval function to plot the result as in
-2
-4
-6
-8
-3 -2 -1 0 1 2 3
Polynomials Wiggle
2nd-order
3rd-order
y
4th-order
5th-order
xi 1 2 3 4 5 6 7 8 9 10
yi 3.5 3.0 2.5 2.0 1.5 -2.4 -2.8 -3.2 -3.6 -4.0
MATLAB’s Command Lines to Demonstrate Polynomials Wiggle
>> x = [1 2 3 4 5 6 7 8 9 10];
>> y = [3.5 3.0 2.5 2.0 1.5 -2.4 -2.8 -3.2 -3.6 -4.0];
>> x0 = 1:0.1:10;
>> y2 = polyval(polyfit(x(4:6), y(4:6), 2), x0);
>> y3 = polyval(polyfit(x(4:7), y(4:7), 3), x0);
>> y4 = polyval(polyfit(x(3:7), y(3:7), 4), x0);
>> y5 = polyval(polyfit(x(3:8), y(3:8), 5), x0);
>> axis([0 10 -5 5])
>> plot(x, y, ‘o’)
>> hold on
>> plot(x0, y2)
>> plot(x0, y3)
>> plot(x0, y4)
>> plot(x0, y5)
x
Piecewise-linear interpolation
Piecewise-quadratic interpolation
Piecewise-cubic interpolation
f’(x) and f’’(x) continuous at breakpoint = cubic spline
MATLAB’s Built-in Interpolation Functions
Funnction Description
x 0 1 2 3 4 5
y 15 10 9 6 2 0
>> x = 0:5; 15
ans =
4 5
Plot Graph:
- Basic Ideas
- Symbolic vs.
vs. Numerical Integration
- Trapezoid Rule
- Simpson’
Simpson’s Rule
- MATLAB quad and quad8
quad8 Functions
Mongkol JIRAVACHARADET
SURAN AR EE INSTITUTE OF ENGINEERING
UNIVERSITY OF TECHNOLOGY SCHOOL OF CIVIL ENGINEERING
BASIC IDEAS
b
I = ∫ f ( x ) dx = Area under curve f ( x )
a
Node
f (x)
Trapezoidal
region
x
a b
Approximated by piecewise-linear:
Area = Sum of trapezoidal regions
Symbolic Math Toolbox
Symbolic Math Toolboxes incorporate symbolic computation into the numeric
environment of MATLAB.
Symbolic Integration with MATLAB
Example : I = ∫ ( x 3 − c ) dx
a
>> syms x a b c
>> I = int(x^3-c,x,a,b)
I =
1/4*b^4-c*b-1/4*a^4+c*a
What is Integration?
The integral is equivalent to the area under the curve.
f(x)
I = ∫ f ( x ) dx
a I
x
a b
Examples of how integration is used to evaluate areas
Newton-Cotes Formulas
Replace a complicated function with a polynomial that is easy to integrate
b b
I = ∫ f ( x ) dx ≅ ∫ fn ( x ) dx
a a
f(x) f(x)
x x
a b a b
f (a ) + f ( b )
b
f(a) I = ∫ f1( x ) dx = (b − a )
a
2
x
a b
width = b – a = h
f (a ) + f ( b )
average height =
2
f(x) f5
f4
f3
f2
f1
x1 x2 x3 x4 x5
x5 x2 x3 x4 x5
∫ f ( x ) dx = ∫ f ( x ) dx + ∫ f ( x ) dx + ∫ f ( x ) dx
x1 x1 x2 x3
+ ∫ f ( x ) dx
x4
f(x)
There are n segment with equal width:
b–a
h =
n
x2 x3 xn
I = ∫ f ( x ) dx + ∫ f ( x ) dx + L + ∫ f ( x ) dx
x1 x2 x n −1
f ( x1 ) + f ( x 2 ) f ( x2 ) + f ( x 3 )
I =h +h +L
2 2
f ( x n−1 ) + f ( x n )
x1 x2 x3 x4 x5 x6 +h
2
x1 = a b–a xn = b
h=
n
f +f f +f f +f h n −1
I = h 1 2 + h 2 3 + L + h n −1 n I = f1 + 2 ∑ fi + fn
2 2 2 2 i =2
f(x)
f4 f5
MEAN
f2 f3
f6
f1
x1 x2 x3 x4 x5 x6
n −1
b–a
f
1
+ 2 ∑
i =2
f i + f n
Substitute h = I = (b − a )
n 2n
width average height
Example 9-1 Use Trapezoidal rule to numerically integrate
f ( x ) = 0.2 + 25 x − 200 x 2 + 675 x 3 − 900 x 4 + 400 x 5
from a = 0 to b = 0.8. Note that the exact value of the integral can be determined
analytically to be 1.640533.
I = 3076/1875 = 1.6405
f (a ) + f (b )
I = (b − a)
2
0.2 + 0.232
= (0.8 − 0) = 0.1728 Error
2
1.6405 − 0.1728
εt = × 100% = 89 .5% Integral estimate
1.6405 0 0.8
2 Trapeziods: n = 2 (h = 0.4) :
n −1
f1 + 2 ∑ fi + fn
I = (b − a) i =2
2n
1.6405 − 1.0688
εt = × 100 % = 34.9%
1.6405
MATLAB’s trapz function
Trapezoidal numerical integration
n I εt
2 1.0688 34.9
3 1.3639 16.5
4 1.4848 9.49
5 1.5399 6.13
6 1.5703 4.28
Simpson’s Rules
Using higher-order polynomials to connect the points
f(x) f(x)
x x
Simpson’s 1/3 rule Simpson’s 3/8 rule
Quadratic connecting 3 points Cubic connecting 4 points
h x −x
I≅ [ f ( x0 ) + 4 f ( x1 ) + f ( x2 )] , h = 2 0
3 2
Example 9-3 Use Simpson’s 1/3 rule to numerically integrate
f ( x ) = 0.2 + 25 x − 200 x 2 + 675 x 3 − 900 x 4 + 400 x 5
from a = 0 to b = 0.8. Note that the exact value of the integral can be determined
analytically to be 1.640533.
Solution: n = 2 (h = 0.4) :
0.4
I = (0.2 + 4( 2.456) + 0.232) = 1.3675
3
1.6405 − 1.3675
εt = × 100 % = 16.6%
1.6405
I=
h
[f0 + 4f1 + f2 ] + h [f2 + 4f3 + f4 ] + L + h [fn−2 + 4fn −1 + fn ]
3 3 3
1 4 1 1 4 1
1 4 1 1 4 1 1 4 1
4 2
4
1 2
4 1
2 4
4 2
a b
(b − a) n −1 n −2
I=
3n
0
f ( x ) + 4 ∑ i j =∑
f ( x ) + 2 f ( x j ) + f ( x n
)
i =1,3,5 2, 4, 6
Example 9-4 Use composite Simpson’s 1/3 rule with n = 4 to numerically integrate
f ( x ) = 0.2 + 25 x − 200 x 2 + 675 x 3 − 900 x 4 + 400 x 5
from a = 0 to b = 0.8. Note that the exact value of the integral can be determined
analytically to be 1.640533.
Solution: n = 4 (h = 0.2) :
f(0) = 0.2 f(0.2) = 1.288 f(0.4) = 2.456 f(0.6) = 3.464 f(0.8) = 0.232
(b − a ) n −1 n−2
I=
3n
0
f ( x ) + 4 ∑ i j =∑
f ( x ) + 2 f ( x j ) + f ( x n
)
i =1,3,5 2, 4,6
0.8
I = ( 0.2 + 4(1.288 + 3.464) + 2( 2.456) + 0.232) = 1.624
3( 4)
1.6405 − 1.624
εt = × 100 % = 1.04%
1.6405
x
x0 x1 x2 x3
a b
(b − a)
or… I = [f ( x0 ) + 3 f ( x1) + 3 f ( x2 ) + f ( x3 )]
8
Truncation Errors
Interval
Method Et width Et
1 3 (b − a )3
Trapezoid h f ′′(ξ ) h =b−a f ′′(ξ )
12 12
1 5 (4) b−a (b − a )5 (4)
Simpson’s 1/3 h f (ξ ) h= f (ξ )
90 2 2880
f(x)
x
1/3 rule 3/8 rule
Example 9-5 Use Simpson’s 1/3 + 3/8 rule with n = 5 to numerically integrate
f ( x ) = 0.2 + 25 x − 200 x 2 + 675 x 3 − 900 x 4 + 400 x 5
from a = 0 to b = 0.8. Note that the exact value of the integral can be determined
analytically to be 1.640533.
f(x)
Solution: n = 5 (h = 0.16) :
f(0) = 0.2 f(0.16) = 1.297
f(0.32) = 1.743 f(0.48) = 3.186
f(0.64) = 3.182 f(0.8) = 0.232
For the first two segments use Simpson’s 1/3 :
0.16
I = (0.2 + 4(1.297) + 1.743) = 0.380
3
For the last three segments use Simpson’s 3/8 : x
0 0.16 0.32 0.48 0.64 0.8
0.48
I = (1.743 + 3(3.186 + 3.182) + 0.232)
8
1/3 rule 3/8 rule
= 1.265
1.6405 − 1.645
Total integral : I = 0.380 + 1.265 = 1.645 εt = × 100 % = 0.274 %
1.6405
poly5.m
function p = poly5(x)
p=0.2+25*x-200*x.^2+675*x.^3-900*x.^4+400*x.^5;
ans =
1.6405
where t ∈ [ 0, 3π ]
>> t = 0:0.1:3*pi;
>> plot3(sin(2*t), cos(t), t)
10
0
1
0.5 1
0 0.5
0
-0.5
- 0.5
-1 -1
3π
∫
Length of the curve:
4 cos(2t )2 + sin(t )2 + 1 dt
Norm of derivative
0
hcurve.m
function f = hcurve(t)
f = sqrt(4*cos(2*t).^2 + sin(t).^2 + 1);
len =
17.2220
∫ ∫
y min x min
f ( x, y ) dx dy
- Basic Ideas
- Euler’s Method
- Higher Order One-step Methods
- Predictor-Corrector Approach
- Runge-Kutta Methods
- Adaptive Stepsize Algorithms
Mongkol JIRAVACHARADET
SURANAREE INSTITUTE OF ENGINEERING
UNIVERSITY OF TECHNOLOGY SCHOOL OF CIVIL ENGINEERING
dy
= f (t , y )
dt
where t = independent variable
y = dependent variable
odeEuler.m
function [t,y] = odeEuler(diffeq,tn,h,y0)
% Input: diffeq = (string) name of m-file that
% evaluate right hand side of ODE
% tn = stopping value of independent variable
% h = stepsize
% y0 = initial condition at t=0
t = (0:h:tn)’;
n = length(t);
y = y0*ones(n,1);
for i=2:n
y(i) = y(i-1)+h*feval(diffeq,t(i-1),y(i-1));
end
dy
Example: = t − 2 y, y (0) = 1, h = 0.2
dt
rhs1.m
function dydt = rhs1(t,y)
dydt = t-2*y;
>> t0=0:0.01:0.6;
>> y0 = (1/4)*(2*t0-ones(size(t0))+5*exp(-2*t0));
>> plot(t0,y0,t,y)
y Euler:
y0i = yi-1 + hk1 slope:
k2 = f(ti, y0i)
slope:
k1 = f(ti-1, yi-1)
h
t
ti-1 ti
k1 + k 2 f (t i −1, y i −1 ) + f (t i , y i0 )
Average slope = =
2 2
y
k + k2
y i = y i −1 + h 1
2
Use slope:
(k1 + k2)/2
h
t
ti-1 ti
Heun’s Method
dy k1 = f (ti −1 , yi −1 )
= f (t , y ) y i0
dt
k2 = f (ti −1 + h, yi −1 + hk1 )
Euler
k +k
yi = yi −1 + h 1 2
2
Heun
True
yi-1
ti-1 ti
Predictor-Corrector Approach
Heun’s Method:
k1 = f (ti −1 , yi −1 )
Euler’s Method:
k2 = f (ti −1 + h, yi −1 + hk1 ) yi = yi −1 + h f (ti −1 , yi −1 )
k1 + k2
yi = yi −1 + h
2
f (ti −1 , yi −1 ) + f (ti , y ) 0
yi yi −1 + h i
2
Example: Use Heun’s method to integrate
Analytical solution: y=
1.3
(
4 0.8 x −0.5 x
e −e ) + 2e −0.5 x
True value: y =
1.3
(
4 0.8(1) −0.5(1)
e −e ) + 2e −0.5(1) = 6.1946
6.1946 − 6.7011
Relative error, Et = ×100% = 8.18%
6.1946
>> y = 2+(1/2)*((4*exp(0)-0.5*2)+(4*exp(0.8*1)-0.5*y))
y = 6.7011 % 1st Corrector of y1
MATLAB’s Implementation
func1.m
function dydx = func1(x,y)
dydx = 4*exp(0.8*x)-0.5*y;
odeHeun.m
function [x,y] = odeHeun(diffeq,xn,h,y0,iter)
% Input: iter = number of corrector iterations
x = (0:h:xn)’;
n = length(x);
y = y0*ones(n,1);
for i=2:n
y(i) = y(i-1)+h*feval(diffeq,x(i-1),y(i-1));
for j=1:iter
y(i) = y(i-1)+h*(feval(diffeq,x(i-1),y(i-1)) ...
+ feval(diffeq,x(i),y(i)))/2;
end
end
For x = 0 to 4, step size = 1, y(0) = 2, Iteration = 15
40
y
20
0
0 1 2 3 4
x
h
yi = yi −1 + ( k1 + 4k2 + k3 )
6
where
k1 = f ( xi −1 , yi −1 )
1 1
k2 = f xi −1 + h, yi −1 + k1h
2 2
k3 = f ( xi −1 + h, yi −1 − k1h + 2k 2 h )
k1 = 4e0 − 0.5(2) = 3
1
k2 = 4e0.8(0.5) − 0.5(2 + × 3 × 1) = 4.2173
2
1
k3 = 4e0.8(0.5) − 0.5(2 + × 4.2173 ×1) = 3.9130
2
k4 = 4e0.8(1) − 0.5(2 + 3.9130 ×1) = 5.9457
1
y1 = 2 + (3 + 2 × 4.2173 + 2 × 3.9130 + 5.9457 )
6
= 6.2011
True value: y =
1.3
(
4 0.8(1) −0.5(1)
e −e ) + 2e −0.5(1) = 6.1946
6.1946 − 6.2010
Relative error, Et = ×100% = 0.1038%
6.1946
MATLAB’s Implementation
odeRK4.m
function [x,y] = odeRK4(diffeq,xn,h,y0)
x = (0:h:xn)’;
n = length(x);
y = y0*ones(n,1);
for i=2:n
k1 = feval(diffeq,x(i-1),y(i-1));
k2 = feval(diffeq,x(i-1)+h/2,y(i-1)+k1*h/2);
k3 = feval(diffeq,x(i-1)+h/2,y(i-1)+k2*h/2);
k4 = feval(diffeq,x(i-1)+h,y(i-1)+k3*h);
y(i) = y(i-1)+h/6*(k1+2*k2+2*k3+k4);
end
Lecture 11 OPTIMIZATION
- Basic Ideas
- One-dimensional Unconstrained Optimization
- Golden-Section Search
- Quadratic Interpolation
- Newton’s Method
- Multidimensional Unconstrained Optimization
- Direct Methods
- Gradient Methods
Suranaree
UNIVERSITY OF TECHNOLOGY Mongkol JIRAVACHARADET
NAKORNRATCHSIMA School of Civil Engineering
BASIC IDEAS
f (x) f ′( x) = 0
Maximum
f ′′( x) < 0
f ( x) = 0
Root Root
0 x
Root
f ′( x) = 0
Minimum
f ′′( x) > 0
Examples of Optimization Problems in Engineering
f (x) Global
maximum
Local
maximum
0 x
Local
Global minimum
minimum
GOLDEN-SECTION SEARCH
- Pick 4th points then choose the first or last three points.
Intermediate
Point
Choice of Intermediate Points
Maximum
f (x)
0 x
xL xU
First L0
iteration L0 = L1 + L2
L1 L2
Second
iteration L1 L2
L2 =
L0 L1
L1 L2
From two condition, =
L1 + L2 L1
Set R = L2 / L1,
1
1+ R = or R2 + R −1 = 0
R
Solving for positive root,
−1 + 1 − 4(−1) 5 −1
R= = = 0.61803…
2 2
R = 0.61803 = Golden ratio since it allows optima to be
found efficiently
The Parthenon in Athens
0.61803 x
x
This golden ratio were considered aesthetically pleasing
by the Greeks.
Initial Step of the Golden-section Search
Eliminate
f (x) f (x1) Maximum
f (x2)
d
d
xL xU x
x2 x1
1) Guess initial bracket xL and xU
2) Choose two interior points x1 and x2 according to golden ratio,
5 −1
d= ( xU − xL )
2
x1 = xL + d , x2 = xU − d
3) If f (x1) > f (x2), eliminate [ xL , x2 ] and set x2 = xL for next round
Next Step to Complete the Algorithm
f (x) Maximum
xU x
xL x2 x1
Old x2 Old x1
4) Only new x1 need to be determined,
5 −1
x1 = xL + ( xU − xL )
2
Example: Golden-Section Search to find maximum
x2
f ( x) = 2sin x − , xL = 0 and xU = 4
10
Solution: (1) Create two interior points
5 −1
d= (4 − 0) = 2.472
2
x1 = 0 + 2.472 = 2.472
x2 = 4 − 2.472 = 1.528
(2) Evaluate function at interior points,
2.4722
f ( x1 ) = f (2.472) = 2sin(2.472) − = 0.63
10
f ( x2 ) = f (1.528) = 1.765
(3) Because f(x2) > f(x1), eliminate upper part,
New xU = x1 = 2.472 OLD
New x1 = x2 = 1.528
xL x2 x1 xU
(4) Compute new x2
5 −1 NEW
d= (2.472 − 0) = 1.528
2
x2 = 2.472 − 1.528 = 0.944 xL x2 x1 xU
i xL x2 f(x2) x1 f(x1) xU d
x0 x1 x3 x2 x
x0 = 1 f ( x0 ) = 1.5829
x1 = 1.5055 f ( x1 ) = 1.7691
x2 = 4 f ( x2 ) = −3.1136
f ( xi )
f ( x) = 0 → xi +1 = xi −
f ′( xi )
g ( xi ) f ′( xi )
g ( x) = 0 → xi +1 = xi − → xi +1 = xi −
g ′( xi ) f ′′( xi )
Example: Use Newton’s method to find maximum of
x2
f ( x) = 2sin x − , Initial guess: x0 = 2.5
10
Solution: (1) Evaluate 1st and 2nd derivatives of the function,
x
f ′( x) = 2 cos x −
5
1
f ′′( x) = −2sin x −
5
(2) Substitute into Newton’s formula,
2 cos xi − xi / 5
xi +1 = xi −
−2sin xi − 1/ 5
(3) Substituting initial guess,
2 cos 2.5 − 2.5 / 5
x1 = 2.5 − = 0.99508, f (0.99508) = 1.57859
−2sin 2.5 − 1/ 5
i x f(x)
0 2.5 0.57194
1 0.99508 1.57859
2 1.46901 1.77385
3 1.42764 1.77573
4 1.42755 1.77573
y Line of constant f
x
2D searches: Ascending a mountain (maximum) or
Descending into a valley (minimization)
DIRECT METHODS
Random Search
Univariate and Pattern Searches
GRADIENT METHODS
Example: f ( x , y ) = y − x − 2 x 2 − 2 xy − y 2
iter = 100;
maxf = -inf;
for i = 1:iter
x = xmin + (xmax - xmin)*rand;
y = ymin + (ymax - ymin)*rand;
fn = feval(objfun,x,y);
if fn > maxf
maxf = fn;
maxx = x;
maxy = y;
end
end
>>[maxx,maxy,maxf]=rndsrch('objfun1',-2,2,1,3)
Pattern directions
4 3
1
2 x
From (1) move along x axis with y constant to max at (2)
Powell’s Method
Use pattern directions to find optimum efficiently
- Pt. 1 and 2 are obtained by 1-D
y
search from different starting pts.
x
GRADIENT METHODS
Use derivative to locate optima
Gradient : ∆y
∆f
∆h
∆x
∆f
= Slope along axis h
∆h
∂f ∂f
∇f = i + j = Steepest direction
∂x ∂y
2
∂ f ∂ f ∂ f
2 2 2
H = 2 −
∂x ∂y ∂x ∂y
2
∂2 f
• If H > 0 and > 0 → local minimum
∂x 2
∂2 f
• If H > 0 and < 0 → local maximum
∂x 2
>> contour(x,y,fxy)
4
x=0:0.1:4;
y=0:0.1:4; 3
fxy=zeros(length(y),length(x));
for i=1:length(y)
2
y
for j=1:length(x)
fxy(i,j)=x(j)*y(i)^2;
1
end
end
0
0 1 2 3 4
x
>> surf(x,y,fxy)
>> mesh(x,y,fxy)
Determine elevation: f (2, 2) = 2 × 22 = 8
8
−1
Direction, θ = tan = 63.40 relative to x axis
4
Magnitude of ∇f is 42 + 82 = 8.944
63.4o
y 2
0
0 1 2 3 4
x
Relationship between steepest direction and x-y coordinate
x
2 6 ∂f ∂f
Define g (h) = f ( x0 + h, y0 + h)
∂x ∂y
Set g ′(h*) = 0 to find h *
Example: Developing 1-D function along a gradient direction
x = −1 + 6(0.2) = 0.2
y = 1 − 6(0.2) = −0.2
Set new starting point to (0.2, -0.2) and repeat the process.
MATLAB Optimization Toolbox
fminunc : the unconstrained optimization function
x =
0.5000 -1.0000
Numerical Methods for Civil Engineers
- Linear Programming
Suranaree
UNIVERSITY OF TECHNOLOGY Mongkol JIRAVACHARADET
NAKORNRATCHSIMA School of Civil Engineering
LINEAR PROGRAMMING
Standard Form
Product Resource
Resource Regular Premium Availability
10x1 + 8x2 80
x1 9 x2 6
x1 , x2 0
Graphical Solution Maximize Z = 150x1 + 175x2
x2 7 x1 + 11x2 ≤ 77 (1)
10x1+8x2 = 80 10 x1 + 8 x2 ≤ 80 (2)
10x1+8x2 80 0 ≤ x1 ≤ 9 (3)
7
0 ≤ x2 ≤ 6 (4)
7x1+11x2 = 77
6
7x1+11x2 77
x1
0
9 11
Increasing Objective Function From Z = 150x1 + 175x2
For Z = 0, 150x1 + 175x2 = 0
x2
Unique solution
Z = 1400
x1
0
Z=0 Z = 600
Other Possible Outcomes
x2
Objective function parallel
to constraint.
Alternate Solution
0 x1
No Feasible Solution
x2
0 x1
Unbounded Problems
x2
0 x1
Simplex Method
Assumption: optimal solution will be an extreme point
x2
Extreme points: corner points
where two constraints meet
Z = 1400
Objective function
is LINEAR
x1
0 Z=0 Z = 600
Slack Variables
Reformulate constraint inequalities as equalities
slack variable
7 x1 + 11x2 ≤ 77 → 7 x1 + 11x2 + S1 = 77
IF . . .
7 x1 + 11x2 ≤ 77 7 x1 + 11x2 + S1 = 77
10 x1 + 8 x2 ≤ 80 10 x1 + 8 x2 + S2 = 80
0 ≤ x1 ≤ 9 x1 + S3 =9
0 ≤ x2 ≤ 6 x2 + S4 =6
x1 , x2 , S1 , S 2 , S3 , S4 ≥ 0
0
7 x1 + 11x2 + S1 = 77 11x2 + S1 = 77
0
10 x1 + 8 x2 + S2 = 80 8 x2 + S2 = 80
0
x1 + S3 =9 S3 =9
0
x2 + S4 =6 x2 =6
x1 x2 S1 S2 S3 S4
0 6 11 32 9 0
basic variables
Simplex Method Implementation S1 = 77
Start at point A : Setting x1 = x2 = 0 S2 = 80
S3 = 9
S4 = 6
A B F
0 x1
8 11
Z 1 0 -55 0 15 0 0 1200
Perform same operation on the remaining rows,
Basic Z x1 x2 S1 S2 S3 S4 Solution Intercept
Z 1 0 -55 0 15 0 0 1200
S1 0 0 5.4 1 -0.7 0 0 21 3.889
x1 0 1 0.8 0 0.1 0 0 8 10
S3 0 0 -0.8 0 -0.1 1 0 1 -1.25
S4 0 0 1 0 0 0 1 6 6
Z = 1414
MATLAB’s fmincon Function
f ( x) = e x1 ( 4 x12 + 2 x22 + 4 x1 x2 + 2 x2 + 1)
x1 x2 ≥ −10
x1 x2 − x1 − x2 + 1.5 ≤ 0
− x1 x2 − 10 ≤ 0
Write M-file confun.m for the constraints
> x0 = [-1,1];
> options = optimset(‘LargeScale’,’off’);
> [x, fval] = . . .
fmincon(‘objfun’,x0,[],[],[],[],[],[],’confun’,options)
x =
-9.5474 1.0474
fval =
0.0236