Professional Documents
Culture Documents
University
50
f(c) 4030
solution
20
10
-10
0 5 10 15 20 c
1. Soving Nonlinear Equation
(Theorem) Existence of a solution
If a function f(x) is continuous on the interval [a,b] and
f(a)f(b) < 0, at least one solution satisfying f(x)= 0
exists within the interval.
f(a) f(a)
b b
x x
a a
f(b) f(b)
When more than one solution exists in the interval, the
interval may be appropriately adjusted to include only
one solution. Repeat this process to find all solutions.
1. Soving Nonlinear Equation
Newton-Raphson Method
A method to obtain x= by iteratively approximating
starting from the initial value x0 and the slope of f(x).
Step 1: The straight line passing through (x0, f(x0))
with the slope f'(x0) at x0 is
y f ( x0 ) f' ( x0 )( x x0 )
The intersection point x1 with the
x-axis can be obtained from
inserting y= 0.
f ( x0 )
x1 x0 '
f ( x0 )
1. Soving Nonlinear Equation
Step 2: Next, from the new point (x1, f(x1)), the
straight line passing through this point with the slope
f'(x1) at x1 is
y f ( x1 ) f '( x1 )( x x1 )
The intersection point x2 with the
x-axis can be obtained from
inserting y= 0.
f ( x1 )
x2 x1 '
f ( x1 )
1. Soving Nonlinear Equation
Step n: Repeat this procedure to get the point (xn,
f(xn)), and the straight line passing through this point
with the slope f'(xn) at xn is
y f ( xn ) f '( xn )( x xn )
The intersection point xn+1 with
the x-axis can be obtained from
inserting y= 0.
f ( xn )
xn 1 xn ' (n 0,1, 2,3,...)
f ( xn )
1. Soving Nonlinear Equation
Stop Condition
Absolute error: |xn+1-xn| 1 or
|f(xn+1)| 2
xn 1 xn
Relative error: 3
xn 1
where 1, 2 and 3 are the bounds for the absolute and
relative errors.
1. Soving Nonlinear Equation
To find the root of e-x= sinx, 0x1 using the Newton-
Raphson method, let f(x)= e-x-sinx and x0= 0. Since
f(x)= -e-x-cosx,
(1) x0= 0, f(x0)= 1, f(x0)= -1-1= -2
f ( x0 ) 1
x1 x0 ' 0 0.5
f ( x0 ) 2
(2) x1= 0.5, f(x1)= 0.127, f(x1)= -1.484
f ( x1 ) 0.127
x2 x1 ' 0.5 0.586
f ( x1 ) 1.484
(3) x2= 0.586, f(x2)= 0.004, f(x2)= -1.390
f ( x2 ) 0.004
x3 x2 ' 0.586 0.5885
f ( x2 ) 1.390
1. Soving Nonlinear Equation
Example 1: Write a script m-file that obtains the
solution of f(x)= e-x-sinx= 0 in three steps using the
Newton-Raphson method with x0= 0.
x= 0;
f= @(x) exp(-x)-sin(x);
df= @(x) -exp(-x)-cos(x);
for i= 1:3
x= x-f(x)/df(x)
end
x = 0.5000
x = 0.5856
x = 0.5885
1. Soving Nonlinear Equation
Example 2: Write a function function that obtains the
solution of f(x)= e-x-sinx= 0 using the Newton-Raphson
method with x0= 0.
function x= NewRap(f,df,x0) % Script m-file
eps= 1e-6; x= x0; f= @(x) exp(-x)-sin(x);
for i= 1:50 df= @(x) -exp(-x)-cos(x)
x= x-f(x)/df(x); x0= 0;
if abs(x-x0) < eps x= NewRap(f,df,x0)
return;
end
x0= x; x= 0.5885
end
disp('Unsuccessfully finished')
1. Soving Nonlinear Equation
Secant Method
A method to obtain x= by using
the approximate derivative
instead of f'(x) and iteratively
approximates it starting from the
two initial values x0 and x1.
x=
0.5885
2. Numerical Differentiation
Numerical Differentiation
df ( x)
f ( x)
'
dx
f ( x x) f ( x)
lim
x 0 x
x0 x1 x2
Step 1. Obtain the Lagrange polynomial using the three
points ( x x )( x x ) ( x x )( x x )
P2 ( x) 1 2
f ( x0 ) 0 2
f ( x1 )
( x0 x1 )( x0 x2 ) ( x1 x0 )( x1 x2 )
( x x0 )( x x1 )
f ( x2 )
( x2 x0 )( x2 x1 )
2. Numerical Differentiation
Step 2. Differentiate both sides once
2 x x1 x2 2 x x0 x2
P ( x)
2
'
f ( x0 ) f ( x1 )
( x0 x1 )( x0 x2 ) ( x1 x0 )( x1 x2 )
2 x x0 x1
f ( x2 )
( x2 x0 )( x2 x1 )
1
f ( x j ) 2h [ f ( x j 1 ) f ( x j 1 )] ( j 1, 2,..., n 1)
'
1
f ( xn ) 2h [ f ( xn 2 ) 4 f ( xn 1 ) 3 f ( xn )] ( j n)
'
f(xj) [25f(xj)+48f(xj+h)36f(xj+2h)+
16f(xj+3h)3f(xj+4h)]/(12h)
(j= 0,1)
f(xj) [f(xj2h)8f(xjh)+8f(xj+h)f(xj+2h)] /(12h)
(j= 2, 3,…, n-2)
f(xj) [3f(xj4h)16f(xj3h)+36f(xj2h)
48f(xjh)+25f(xj)]/(12h)
(j= n-1, n)
2. Numerical Differentiation
Four data are taken from
f ( x) cos( x), x [1, 2]
2
as (-1,0), (-0.5,0.707), (0,1), (0.5,0.707)
2h
1
(3 0 4 0.707 1) 1.828
2 0.5
2. Numerical Differentiation
1 1
f ( x1 ) [ f ( x0 ) f ( x2 )]
'
(0 1) 1
2h 2 0.5
1
f ( x2 ) [ f ( x1 ) f ( x3 )]
'
2h
1
(0.707 0.707) 0
2 0.5
1
f ( x3 ) [ f ( x1 ) 4 f ( x2 ) 3 f ( x3 )]
'
2h
1
(0.707 4 1 3 0.707) 1.172
2 0.5
2. Numerical Differentiation
f ( x) cos( x)
2
f '( x) sin( x)
2 2
2. Numerical Differentiation
Example 5: Write a script m-file program that obtains
f'(x) using the three-point formula where h= 0.02.
f ( x) sin(e 0.1x x 2 1), x 0, 0.1, 0.2, , 0.5
f= @(x) sin(exp(-0.1*x).*sqrt(x.^2+1));
a= 0; b= 20;
[x,df]= diff3p(f,a,b); 1
f(x)
df(x)
plot(x,f(x),'b',x,df,'r','linewidth',2)
0.5
legend('f(x)','df(x)')
xlabel('x') 0
-0.5
0 5 10 15 20
x
3. Numerical Integration
Numerical Integration
b
S f ( x)dx
a
F ( x) a F (b) F (a)
b
f ( xk 1 ) xk f ( xk ) xk
h xk 1
( x xk )dx
h xk 1
( x xk 1 )dx
f ( xk 1 ) 2 xk f ( xk ) 2 xk
( x xk x) ( x xk 1 x)
h xk 1 h xk 1
h
[ f ( xk 1 ) f ( xk )] (k 1, 2,.., n)
2
So, the final integral is
3. Numerical Integration
b
S f ( x)dx S1 S 2 ... S n
a
h h h
[ f ( x0 ) f ( x1 )] [ f ( x1 ) f ( x2 )] [ f ( xn 1 ) f ( xn )]
2 2 2
h
[ f ( x0 ) 2 f ( x1 ) ... 2 f ( xn 1 ) f ( xn )]
2
Trapzoidal Rule for Integration
b
S f ( x)dx
a
h
[ f ( x0 ) 2 f ( x1 ) ... 2 f ( xn 1 ) f ( xn )]
2
3. Numerical Integration
2
Consider S (1/ x)dx . Divide the interval [1, 2]
1
into quarters and applying the trapezoidal rule gives
h= (b-a)/n= 1/4= 0.25 h h h h
x0= 1, f(x0)= 1, x0 x1 x2 x3 x4
x1= 1.25, f(x1)= 1/1.25= 0.8000
x2= 1.5, f(x2)= 1/1.5= 0.6667
x3= 1.75, f(x3)= 1/1.75= 0.5714
x4= 2, f(x4)= 1/2= 0.5
h
S [ f ( x0 ) 2 f ( x1 ) 2 f ( x2 ) 2 f ( x3 ) f ( x4 )]
2
1
(1 2 0.8 2 0.6667 2 0.5714 0.5) 0.6970
8
3. Numerical Integration
Example 8: Write a function function program that
accepts f(x) and a, b as inputs and returns the integral
S using the trapezoidal rule.
function S= TrapzInt(f,a,b)
h= 0.01;
x= a:h:b;
fx= f(x);
S= 0.5*h*(fx(1)+2*sum(fx(2:end-1))+fx(end));
3. Numerical Integration
Example 9: Call the TrapzInt function to calculate S.
2
S sin(e x
x 1)dx
2
0
f= @(x) sin(exp(-x).*sqrt(x.^2+1));
a= 1; b= 2*pi;
S= TrapzInt(f,a,b)
S1= integral(f,a,b) % MATLAB built-in function
S=
0.8069
S1 =
0.8070
3. Numerical Integration
Simpson's Rule
An integral formula is derived based on a second-order
Lagrange polynomial that connects three consecutive
points of f(x).
3. Numerical Integration
Given three points (xk-2, f(xk-2)), f(x)
(xk-1, f(xk-1)), (xk, f(xk)) and h= f(xk-2)
xk–xk-1, the 2nd-order Lagrange f(xk)
polynomial passing through f(xk-1) P2(x)
the three points is Sk/2
x
xk-2
h xk-1 h xk
( x xk 1 )( x xk ) ( x xk 2 )( x xk )
P2 ( x) f ( xk 2 ) f ( xk 1 )
( xk 2 xk 1 )( xk 2 xk ) ( xk 1 xk 2 )( xk 1 xk )
( x xk 2 )( x xk 1 )
f ( xk )
( xk xk 2 )( xk xk 1 )
3. Numerical Integration
Thus,
f ( xk 2 ) f ( xk 1 )
P2 ( x) ( x xk 1 )( x xk ) ( x xk 2 )( x xk )
2h 2
h 2
f ( xk )
2
( x xk 2 )( x xk 1 )
2h
Finding the integral of a subinterval yields
xk
Sk /2 P2 ( x)dx
xk 2
h
[ f ( xk 2 ) 4 f ( xk 1 ) f ( xk )] (k 2, 4,.., n)
3
3. Numerical Integration
So, the approximate integral is
b
S f ( x)dx S1 S 2 ... Sn /2
a
h h
[ f ( x0 ) 4 f ( x1 ) f ( x2 )] [ f ( x2 ) 4 f ( x3 ) f ( x4 )] ...
3 3
h
[ f ( xn 2 ) 4 f ( xn 1 ) f ( xn )]
3
h
{ f ( x0 ) 4[ f ( x1 ) f ( x3 ) ... f ( xn 1 )]
3
2[ f ( x2 ) f ( x4 ) ... f ( xn 2 )] f ( xn )}
3. Numerical Integration
Simpson's Rule for Integration
b
S f ( x)dx
a
h
{ f ( x0 ) 4[ f ( x1 ) f ( x3 ) ... f ( xn 1 )]
3
2[ f ( x2 ) f ( x4 ) ... f ( xn 2 )] f ( xn )}
3. Numerical Integration
2
Consider S (1/ x)dx . Divide the interval [1, 2]
1
into 4 subintervals and the Simpson's rule gives
h= (b-a)/n= 1/4= 0.25
x0= 1, f(x0)= 1,
x1= 1.25, f(x1)= 1/1.25= 0.8000
x2= 1.5, f(x2)= 1/1.5= 0.6667
x3= 1.75, f(x3)= 1/1.75= 0.5714
x4= 2, f(x4)= 1/2= 0.5
h
S [ f ( x0 ) 4 f ( x1 ) 2 f ( x2 ) 4 f ( x3 ) f ( x4 )]
3
1
(1 4 0.8 2 0.6667 4 0.5714 0.5) 0.6933
12
3. Numerical Integration
Example 10: Write a function function program that
accepts f(x) and a, b as inputs and returns the integral
S using the Simpson's rule.
function S= SimpInt(f,a,b)
n= 101; h= (b-a)/(n-1);
x= linspace(a,b,n);
fx= f(x);
Seven= sum(fx(2:2:n-1));
Sodd= sum(fx(3:2:n-2));
S= h/3*(fx(1)+4*Seven+2*Sodd+fx(end));
3. Numerical Integration
Example 11: Call the SimpInt function to calculate S.
2
S sin(e x
x 1)dx
2
0
f= @(x) sin(exp(-x).*sqrt(x.^2+1));
a= 1; b= 2*pi;
S= SimpInt(f,a,b)
S1= integral(f,a,b) % MATLAB built-in function
S=
0.8070
S1 =
0.8070
3. Numerical Integration
MATLAB integral
S= integral(fun,a,b) % approximates the integral of
function fun from a to b. fun must be a function handle.
a and b can be -inf or inf.
f= @(x) 2*pi*x.*sqrt(1+4*x.^2);
a= 1; b= 2;
S= integral(f,a,b)
S=
30.8465
4. Solving ODEs
First-Order ODE
y ' (t ) f (t , y (t )), y (a) (a t b)
h= (b-a)/N
h h
t
a= t0 t1 t2 … tN= b
4. Solving ODEs
Euler's Method
Expanding the Taylor series to derive the Euler's
method gives
y(tn+1)= y(tn+h)= y(tn)+hy(tn)+O(h2)
where O(h2) is the truncation
y(t)
error and is omitted. y(tn+1)
Rewriting the equation yields y(tn)
y(tn+1)= y(tn)+hy(tn)
h
= y(tn)+hf(tn,y(tn))
t
tn tn+1
4. Solving ODEs
Euler's General Equation
y(tn+1)= y(tn)+hf(tn,y(tn)), y(t0)= y(a)
(n= 0,1,2,…,N-1)
Given the following ODE, find y(t3) by the Euler's
method where N= 10.
y ' (t ) 2 y (t ) t 1, y (0) 0
(0 t 2)
From the problem
f(t,y(t))= 2y(t)-t+1, y(0)= 0
h= (b-a)/N= (2-0)/10= 0.2
tn= a+nh= 0.2n (n= 0,1,2,3)
4. Solving ODEs
(1) n= 0, t1= 0.2 y(t)
0.25
for i= 1:500 0.2
y= Euler(f,t,y,h); 0.15
t= t+h; 0.1
end 0
0 2 4
t
6 8 10
plot(buf(:,1),buf(:,2),'linewidth',2)
xlabel('t'), ylabel('y(t)')
4. Solving ODEs
2nd-Order Runge-Kutta Method
Expanding the Taylor series to derive the 2nd-order
Runge-Kutta method gives
2
h ''
y (tn 1 ) y (tn ) hy (tn ) y (tn ) O(h3 )
'
2
where O(h3) is the truncation error and is omitted.
Substituting y ' (t ) f (t , y (t )), y '' (t ) f ' (t , y (t )) into
the equation and rewriting yields
h2 '
y (tn 1 ) y (tn ) hf (tn , y (tn )) f (tn , y (tn ))
2
h '
y (tn ) h[ f (tn , y (tn )) f (tn , y (tn ))]
2
4. Solving ODEs
h '
y (tn 1 ) y (tn ) h[ f (tn , y (tn )) f (tn , y (tn ))]
2
y (tn ) hT (t , y (t ))
h '
where T (t , y (t )) f (tn , y (tn )) f (tn , y (tn ))
2
Applying the chain rule
df f f '
f (t , y )
'
y
dt t y
to yields
h f h ' f
T (t , y (t )) f (t , y (t )) (t , y (t )) y (t ) (t , y (t ))
2 t 2 y
4. Solving ODEs
From Taylor's theorem for two-variable function
T (t , y (t )) af (t , y (t ) )
f f
a{ f (t , y (t )) (t , y (t )) (t , y (t ))}
t y
Comparing with gives
h h ' h
a 1, , y (t ) f (t , y (t ))
2 2 2
Therefore,
4. Solving ODEs
y (tn 1 ) y (tn ) h T (tn , y (tn )) y (tn ) h af (tn , y (tn ) )
h h
y (tn ) hf [tn , y (tn ) f (tn , y (tn ))]
2 2
Rewriting this equation gives
The General Equation
y (tn 1 ) y (tn ) K 2
where K1 hf (tn , y (tn ))
h K1
K 2 hf (tn , y (tn ) )
2 2
y (t0 ) y (a) (n 0,1, 2, , N 1)
4. Solving ODEs
Example 14: Write a function function program that
accepts f(t,y(t)), t, y and h as inputs and returns the
updated y using the 2nd-order Runge-Kutta method.
function y= RK2(f,t,y,h)
K1= h*f(t,y);
K2= h*f(t+0.5*h,y+0.5*K1);
y= y+K2;
y (tn 1 ) y (tn ) K 2
where K1 hf (tn , y (tn ))
h K1
K 2 hf (tn , y (tn ) )
2 2
4. Solving ODEs
Example 15: Call the RK2 function to calculate y(t).
y ' (t ) 2 y (t ) e t , y (0) 0 (0 t 10)
f= @(t,y) -2*y+exp(-t);
t= 0; y= 0; h= 0.02; buf= [t y];
0.3
0.25
for i= 1:500 0.2
y= RK2(f,t,y,h); 0.15
t= t+h; 0.1
end 0
0 2 4
t
6 8 10
plot(buf(:,1),buf(:,2),'linewidth',2)
xlabel('t'), ylabel('y(t)')
4. Solving ODEs
4th-Order Runge-Kutta Method
1
y (tn 1 ) y (tn ) ( K1 2 K 2 2 K 3 K 4 )
6
where K1 hf (tn , y (tn ))
K 2 hf (tn h / 2, y (tn ) K1 / 2)
K 3 hf (tn h / 2, y (tn ) K 2 / 2)
K 4 hf (tn h, y (tn ) K3 )
y (t0 ) y (a) (n 0,1, 2, , N 1)
4. Solving ODEs
Example 16: Write a function function program that
accepts f(t,y(t)), t, y and h as inputs and returns the
updated y using the 4th-order Runge-Kutta method.
function y= RK4_H(f,t,y,h)
K1= h*f(t,y);
K2= h*f(t+0.5*h,y+0.5*K1);
K3= h*f(t+0.5*h,y+0.5*K2);
K4= h*f(t+h,y+K3);
y= y+(K1+2*(K2+K3)+K4)/6;
4. Solving ODEs
Example 17: Call the RK2 and RK4_H functions to
calculate y(t). y ' (t ) 2 y (t ) e t , y (0) 0 (0 t 10)
f= @(t,y) -2*y+exp(-t);
t= 0; y1= 0; y2= 0; h= 0.02; buf= [t y1 y2];
0.25
y1= RK2(f,t,y1,h);
0.15
y2= RK4_H(f,t,y2,h);
0.1
end
0
plot(buf(:,1),buf(:,2),buf(:,1),buf(:,3),':','linewidth',2)
0 2 4
t
6 8 10
xlabel('t'), ylabel('y(t)')
legend('RK2','RK4')
4. Solving ODEs
3rd-Order Non-Homogeneous ODEs
y a1
y a2 y a3 y b0 u b1u b2u b3u
with y (t0 ), y (t0 ),
y (t0 ), u (t0 ), u (t0 ), u(t0 )
function x= RK4(f,t,x,u,h)
K1= h*f(t,x,u);
K2= h*f(t+0.5*h,x+0.5*K1,u);
K3= h*f(t+0.5*h,x+0.5*K2,u);
K4= h*f(t+h,x+K3,u);
x= x+(K1+2*(K2+K3)+K4)/6;
4. Solving ODEs
Example 19: Call the RK4 function to calculate y(t).
x1 (t ) x1 (t ) x2 (t )
x2 (t ) 2 x1 (t ) 4u (t )
y (t ) x1 (t )
x1 (0) x2 (0) 0, u (t ) 1 (0 t 15)
xdot(1)= -x(1)+x(2);
2.5
0.5
0
0 5 10 15
t
4. Solving ODEs
MATLAB ode45
[tv,xv]= ode45(fun,tspan,x0) % integrates x'= f(t,x,u)
from time t0 to tfinal with initial conditions x0. fun is a
function handle. For a scalar t, a vector x and a scalar u,
fun(t,x,u) must return a column vector corresponding to
f(t,x,u).
u= 1;
[t,x]= ode45(@(t,x) f(t,x,u),[0 15],[0 0]);
plot(t,x(:,1))
4. Solving ODEs
function xdot= f(t,x,u)
xdot= zeros(2,1);
xdot(1)= -x(1)+x(2);
xdot(2)= -2*x(1)+4*u;
5. Curve Fitting
Curve fitting is the process of specifying the model
that provides the best fit to the specific curves in given
data set.
• Used to show general trends in data or to represent
data as a simple curve.
• A typical method is the least squares method.
y
y= ax+b
x
5. Curve Fitting
y a0 x n a1 x n 1
an 1 x an
Model Least
Squares Model coefficients
Method (a0, a1, ..., an)
Data
(x0,y0), (x1,y1),
..., (xm,ym)
y= x+0.067
-1
0 1 x
J min ( xˆ ) yT ( y Axˆ )
0.9 1 1
1
0.9 0 1.1 ( 0 0 1 ) 0.0067
0.067
1.1 1 1
5. Curve Fitting
Example 20: Write a function program that accepts x, y
and n as inputs and returns p using the least squares
method.
function p= lsq(x,y,n)
a= ones(size(y));
for i= 1:n
a= [x.^i a];
end
p= inv(a'*a)*a'*y;
5. Curve Fitting
Example 21: Given 4 points (1,0), (2,2), (3,3), (4,6),
find the quadratic curve y= a0x2+a1x+a2 representing
these points by calling the lsq function.
x= [1 2 3 4]';
y= [0 2 3 6]';
p= lsq(x,y,2)
p=
0.2500
0.6500
-0.7500
5. Curve Fitting
MATLAB polyfit
p= polyfit(x,y,n) % finds the coefficients of a
polynomial p(x) of degree n that fits the data y best in a
least-squares sense. p is a row vector of length n+1
containing the polynomial coefficients in descending
powers, p(1)*x^n + p(2)*x^(n-1) +...+ p(n)*x+ p(n+1).
x= [1 2 3 4]; y= [0 2 3 6];
p= polyfit(x,y,2)
p=
0.2500 0.6500 -0.7500
Q&A