You are on page 1of 80

Adama Science and Technology

University

Lecture 05: Applications of MATLAB


• Solving nonlinear equations
• Numerical differentiation
• Numerical integration
• Solving ODEs
• Curve Fitting
1. Soving Nonlinear Equation
Nonlinear Equation
f ( x)  0, a  x  b

(Question) The velocity equation of a


parachute in free fall is as follows:
gm 
ct
v: fall speed c: braking factor m: mass
v (1  e ) g: gravitational acceleration
m
c
Is it possible to find the braking factor c that causes a
paratrooper with m = 68kg to have a fall speed of 40m/s
in 10seconds after free fall?
(Answer) Numerically possible
1. Soving Nonlinear Equation
From the given equation
10 c
gm 
ct
9.81 68 
f (c )  (1  e m
)v  (1  e 68
)  40
c c
c is a solution so that f(c)= 0.
60

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, 0x1 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.

Step 1: Straight line passing


through two points (x0, f(x0)), (x1,
f(x1)) is
f ( x1 )  f ( x0 )
y  f ( x1 )  ( x  x1 )
x1  x0
1. Soving Nonlinear Equation
If the intersection of the straight
line and the x-axis is x2, then y= 0
at this point. Inserting y= 0 into
the previous equation yields
x1  x0
x2  x1  f ( x1 )
f ( x1 )  f ( x0 )
Step 2: Straight line passing
through two points (x1, f(x1)), (x2,
f(x2)), is
f ( x2 )  f ( x1 )
y  f ( x2 )  ( x  x2 )
x2  x1
1. Soving Nonlinear Equation
If the intersection of the straight line
and the x-axis is x3, then y= 0 at this
point. Inserting y= 0 into the
previous equation yields
x2  x1
x3  x2  f ( x2 )
f ( x2 )  f ( x1 )
Step n: By repeating this procedure,
we get the general formula as
xn  xn 1
xn 1  xn  f ( xn ) (n  0,1, 2,)
f ( xn )  f ( xn 1 )
1. Soving Nonlinear Equation
To find the root of e-x= sinx, 0x1 using the Secand
method, let f(x)= e-x-sinx with x0= 0 and x1= 1.

(1) x0= 0, f(x0)= 1 and x1= 1, f(x1)= -0.4736


x1  x0
x2  x1  f ( x1 )  0.6786
f ( x1 )  f ( x0 )
(2) x2= 0.6786, f(x2)= -0.1204
x2  x1
x3  x2  f ( x2 )  0.5691
f ( x2 )  f ( x1 )
(3) x3= 0.5691, f(x3)= 0.0272
x3  x2
x4  x3  f ( x3 )  0.5893
f ( x3 )  f ( x2 )
1. Soving Nonlinear Equation
Example 3: Write a script m-file that obtains the
solution of f(x)= e-x-sinx= 0 in three steps using the
Secand method with x0= 0 and x1= 1.
f= @(x) exp(-x)-sin(x);
x0= 0; f0= f(x0); x1= 1; f1= f(x1);
for i= 1:3
x2= x1-f1*(x1-x0)/(f1-f0);
f2= f(x2); [x2 f2]
x0= x1; x1= x2; f0= f1; f1= f2;
end
ans = 0.6786 -0.1204
ans = 0.5691 0.0272
ans = 0.5893 -0.0010
1. Soving Nonlinear Equation
Example 4: Write a function function that obtains the
solution of f(x)= e-x-sinx= 0 using the Secand method
with x0= 0 and x1= 1.
function x= Secant(f,x0,x1) x0= x1; x1= x2;
eps= 1e-6; f0= f1; f1= f2;
f0= f(x0); f1= f(x1); end
for i= 1:50 disp('Unsuccessfully finished')
x2= x1-f1*(x1-x0)/(f1-f0);
if abs(x2-x1) < eps % Script m-file
x= (x1+x2)/2; f= @(x) exp(-x)-sin(x);
return; x0= 0; x1= 1;
end x= Secant(f,x0,x1)
f2= f(x2); x= 0.5885
1. Soving Nonlinear Equation
MATLAB fzero
x= fzero(fun,x0) % find a zero of the function fun near
x0, if x0 is a scalar.
f= @(x) exp(-x)-sin(x);
x0= 0;
x = fzero(f, x0)

x=
0.5885
2. Numerical Differentiation
Numerical Differentiation
df ( x)
f ( x) 
'

dx
f ( x  x)  f ( x)
 lim
x  0 x

(Question) Given a function f(x) that is complex or


difficult to obtain its first derivative f'(x) analytically,
can it be approximated?
For example, f ( x)  sin(e  x x 2  1)
(Answer) Numerically possible
2. Numerical Differentiation
3-Point Formula
Three interpolation points ( 0, f( 0)), ( 1, f( 1)), ( 2, f(
2)) are given. 0, 1 and 2 are equally spaced with step
size ℎ as 1= 0+h, 2= 1+h.
h h

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 )

Step 3. Find the derivative at each interpolation point.


For x= x0, f' ( x0 )  P2' ( x) x  x  P2' ( x0 )
0

For x= x1, f' ( x1 )  P2' ( x1 )


For x= x2, f' ( x2 )  P2' ( x2 )
2. Numerical Differentiation
3-Point Formula
1
 f ( x0 )  2h [3 f ( x0 )  4 f ( x1 )  f ( x2 )] ( j  0)
'

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 ' ( x0 ) f ' ( x1 ) f ' ( xn )

x0 x1 x2 x3 ... xn-2 xn-1 xn


formula  formula  formula 
2. Numerical Differentiation
5-Point Formula

 f(xj) [25f(xj)+48f(xj+h)36f(xj+2h)+
16f(xj+3h)3f(xj+4h)]/(12h)
(j= 0,1)
 f(xj) [f(xj2h)8f(xjh)+8f(xj+h)f(xj+2h)] /(12h)
(j= 2, 3,…, n-2)
 f(xj) [3f(xj4h)16f(xj3h)+36f(xj2h)
48f(xjh)+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)

From data, h= 0.1, f(x0)= 0, f(x1)= 0.707, f(x2)= 1,


f(x3)= 0.707
Applying the three-point formula gives
1
f ( x0 )  [3 f ( x0 )  4 f ( x1 )  f ( x2 )]
'

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

h= 0.1; h2= 2*h; df


x= 0:h:0.5;
f= sin(exp(-0.1*x).*sqrt(x.^2+1)); df =
n= length(f); -0.0533 -0.0011 0.0488
df(1)= (-3*f(1)+4*f(2)-f(3))/h2; 0.0925 0.1277 0.1583
for i= 2:n-1
df(i)= (-f(i-1)+f(i+1))/ h2;
end
df(n)= (f(n-2)-4*f(n-1)+3*f(n))/ h2;
2. Numerical Differentiation
Example 6: Write a function program that accepts f(x)
and the range a and b as inputs and returns the x vector
and the first derivative df as outputs using a three-
point formula.
function [x,df]= diff3p(f,a,b)
h= 0.02; h2= 2*h; x= a:h:b;
n= length(x);
df(1)= (-3*f(x(1))+4*f(x(2))-f(x(3)))/h2;
for i= 2:n-1
df(i)= (-f(x(i-1))+f(x(i+1)))/h2;
end
df(n)= (f(x(n-2))-4*f(x(n-1))+3*f(x(n)))/h2;
2. Numerical Differentiation
Example 7: Call the diff3p function to calculate f'(x)
and plot f(x) and f'(x).
f ( x)  sin(e 0.1x x 2  1), x  [0, 20]

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

(Question) In definite integral problems, it is sometimes


difficult to know the anti-derivative F(x) of the integrand
analytically. Can the integral be approximated?
2
For example, S   sin(e  x x 2  1)dx
0

(Answer) Numerically possible


3. Numerical Integration
Trapezoidal Rule
Connect two consecutive points with a straight line,
that is, derive an integral formula based on the
approximate first-order Lagrange polynomial of f(x).
3. Numerical Integration
Given two points (xk-1, f(xk-1)), f(x)
(xk, f(xk)) and h= xk–xk-1, the f(xk-1)
straight line passing through f(xk)
the two points is P1(x)
Sk
x
xk-1 xk
h
x  xk x  xk 1
P1 ( x)  f ( xk 1 )  f ( xk )
xk 1  xk xk  xk 1
f ( xk 1 ) f ( xk )
 ( x  xk )  ( x  xk 1 )
h h
3. Numerical Integration
xk
Sk   P1 ( x)dx
xk 1

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)

First, if the given interval [a,b] is divided into N equal


intervals,

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)

y(t1)= y(t0)+hf(t0,y(t0)) 0.736


y(t3)
= 0+0.2(2 0-0+1)= 0.2 0.44
y(t2)

(2) n= 1, t2= 0.4 0.2


y(t0) y(t1)
y(t2)= y(t1)+hf(t1,y(t1)) t
a= t0 t1 t2 t3
= 0.2+0.2 (20.2-0.2+1)= 0.44
(2) n= 3, t3= 0.6
y(t3)= y(t2)+hf(t2,y(t2))
= 0.4+0.2(20.44-0.4+1)= 0.736
4. Solving ODEs
Example 12: Write a function function program that
accepts f(t,y(t)), t, y and h as inputs and returns the
updated y using the Euler's method.
function y= Euler(f,t,y,h)
y= y+h*f(t,y);

y(tn+1)= y(tn)+hf(tn,y(tn)), y(t0)= y(a)


(n= 0,1,2,…,N-1)
4. Solving ODEs
Example 13: Call the Euler 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= Euler(f,t,y,h); 0.15

t= t+h; 0.1

buf= [buf; t y]; 0.05

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

buf= [buf; t y]; 0.05

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

for i= 1:500 RK2


RK4
0.2

y1= RK2(f,t,y1,h);
0.15

y2= RK4_H(f,t,y2,h);
0.1

t= t+h; buf= [buf; t y1 y2];


0.05

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 )

Solving ODEs by the 4th-order R-K method


Step1: Write a 3rd-order ODE into three coupled 1st-
order equations by defining state variables, x1, x2, and
x3.
x1 (t )  f1 (t , x1 (t ), x2 (t ), x3 (t ), u (t ))
x2 (t )  f 2 (t , x1 (t ), x2 (t ), x3 (t ), u (t ))
x3 (t )  f3 (t , x1 (t ), x2 (t ), x3 (t ), u (t ))
4. Solving ODEs
Step2: Solve the coupled equations numerically using
the 4-order Runge-Kutta routine.
1
x1 (tn 1 )  x1 (tn )  ( K11  2 K12  2 K13  K14 )
6
1
x2 (tn 1 )  x2 (tn )  ( K 21  2 K 22  2 K 23  K 24 )
6
1
x3 (tn 1 )  x3 (tn )  ( K31  2 K32  2 K33  K34 )
6
where
K11  hf1 (tn , x1 (tn ), x2 (tn ), x3 (tn ), u (tn ))
K 21  hf 2 (tn , x1 (tn ), x2 (tn ), x3 (tn ), u (tn ))
K 31  hf3 (tn , x1 (tn ), x2 (tn ), x3 (tn ), u (tn ))
4. Solving ODEs
K12  hf1 (tn  h / 2, x1 (tn )  K11 / 2, x2 (tn )  K 21 / 2, x3 (tn )  K31 / 2, u (tn ))
K 22  hf 2 (tn  h / 2, x1 (tn )  K11 / 2, x2 (tn )  K 21 / 2, x3 (tn )  K31 / 2, u (tn ))
K 32  hf3 (tn  h / 2, x1 (tn )  K11 / 2, x2 (tn )  K 21 / 2, x3 (tn )  K31 / 2, u (tn ))

K13  hf1 (tn  h / 2, x1 (tn )  K12 / 2, x2 (tn )  K 22 / 2, x3 (tn )  K 32 / 2, u (tn ))


K 23  hf 2 (tn  h / 2, x1 (tn )  K12 / 2, x2 (tn )  K 22 / 2, x3 (tn )  K32 / 2, u (tn ))
K 33  hf3 (tn  h / 2, x1 (tn )  K12 / 2, x2 (tn )  K 22 / 2, x3 (tn )  K 32 / 2, u (tn ))

K14  hf1 (tn  h, x1 (tn )  K13 , x2 (tn )  K 23 , x3 (tn )  K33 , u (tn ))


K 24  hf 2 (tn  h, x1 (tn )  K13 , x2 (tn )  K 23 , x3 (tn )  K33 , u (tn ))
K 34  hf3 (tn  h, x1 (tn )  K13 , x2 (tn )  K 23 , x3 (tn )  K33 , u (tn ))
h  (b  a) / N , tn  a  nh (n  0,1, 2, , N )
4. Solving ODEs
Example 18: Write a function function program that
accepts f(t,y(t),u(t)), t, x, u and h as inputs and returns
the updated x using the 4th-order Runge-Kutta
method.

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).
x1 (t )   x1 (t )  x2 (t )
x2 (t )  2 x1 (t )  4u (t )
y (t )  x1 (t )
x1 (0)  x2 (0)  0, u (t )  1 (0  t  15)

t= 0; x= [0;0]; h= 0.01; loop= 1500;


buf= [t x(1)]; u= 1;
for i= 1:loop
x= RK4(@f,t,x,u,h);
t= t+h;
4. Solving ODEs
buf= [buf;t x(1)];
end
plot(buf(:,1),buf(:,2),'linewidth',2)
xlabel('t'), ylabel('y(t)')

function xdot= f(t,x,u)


xdot= zeros(2,1); 3

xdot(1)= -x(1)+x(2);
2.5

xdot(2)= -2*x(1)+4*u; 1.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)

• Order of the model: n


• Number of data: m+1 (m  n)
5. Curve Fitting
Substituting the data into a given model gives
y0  a0 x0n  a1 x0n 1    an 1 x0  an   0
y1  a0 x1n  a1 x1n 1    an 1 x1  an  1

ym  a0 xmn  a1 xmn 1    an 1 xm  an   m
where 0, 1, ..., m are the
errors caused by the m
y
discrepancy between the
data and the model. Model
0 1
Rewriting in a vector form equation
yields
x
5. Curve Fitting
y  Ax  ε
where
 y0   a0  0   x0n x0n 1  x0 1
y  a     n 
 1  1  1  x1 x1n 1  x1 1
y , x , ε ,A
         
       n 
 ym   an   m   xm xmn 1  xm 1
Defining a performance index as
J ( x )   02  12  ...   m2  εT ε
 ( Ax  y )T ( Ax  y )
 x T AT Ax  x T AT y  yT Ax  yT y
5. Curve Fitting
Determination of the model coefficients results in the
problem of finding x such that J(x) is minimized.
 x J ( x )   x ( xT AT Ax  x T AT y  yT Ax  yT y )
 2 AT Ax  AT y  AT y  0
Rewriting the above equation yields
x  ( AT A) 1 AT y

The minimum value of J(x) is


J min ( xˆ )  xˆ T AT Axˆ  xˆ T AT y  yT Axˆ  yT y
 yT ( y  Axˆ )
5. Curve Fitting
Given data (-1,-0.9), (0,0), (1,1.1), we obtain y= a0x+a1
representing these points by the least squares method,
and the sum of the estimated errors. From the data
 x0 1  1 1  y0   0.9 
A   x1 1   0 1 , y   y1    0 
 x2 1  1 1  y2   1.1 
 1 1
 1 0 1   2 0
A A
T
  0 1   
 1 1 1  1 1 
0 3 

1  3 0  1/ 2 0 
1
( A A)  
T

6 0 2   0 1/ 3

5. Curve Fitting
 0.9 
1 T 1/ 2 0   1 0 1    1 
xˆ  ( A A) A y  
T
    0 
 0.067 
 0 1/ 3  1 1 1  1.1   
 

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

You might also like