Download - e7 Ordinary Differential Equation New_0
-
8/10/2019 e7 Ordinary Differential Equation New_0
1/28
ENT 258 Numerical Analysis Laboratory Module
EXPERIMENT 7
SOLVING ORDINARY DIFFERENTIAL EQUATION
1.0 OBJECTIVES
1.1. To understand fundamentals of ordinary differential equations1.2. To apply various method for solving ordinary differential equations numerically using
MATLAB software
2.0. EQUIPMENT
Computers and Matla program in the Mechanical !esign.
3.0 INTRODUCTION & THEORY
An equation involving the !"#$%#$!'or differentials of the dependent variale iscalled a differential equation. A differential equation involving only one independent iscalled an ("#)%"* #++!"!)#%, !-%#(). "f a differential equation involves two or moreindependent variales# it is called a partial differential equation.
$rdinary differential equations are classified according to their ("!"# ,#)!%"#*#
and /()%"* ()##()'. The order of an ("#)%"* #++!"!)#%, !-%#()isdefined to e the order of the highest derivative present in that equation. %omee&les of first'# second'# and third'order differential equations are
)(xfaydx
dy=+ ' 1st'order equation
xydx
dy
dx
ydx sin2
2
22 =++ ' 2nd'order equation
and
xcdx
dyby
dx
yda
dx
ydsin
2
2
3
3
=++ ' (rd'order equation
wherexis the #)!!)!) $%"#%/,!) y is the !!)!) $%"#%/,!. $rdinary differentialequations can e classified as ,#)!%"and )(),#)!%"equations. A differentialequation is linear if it can e written in form
)()()(...)()( 011
1
1 xfyxadx
dyxa
dx
ydxa
dx
ydxa
n
n
nn
n
n =++++
1
-
8/10/2019 e7 Ordinary Differential Equation New_0
2/28
ENT 258 Numerical Analysis Laboratory Module
$rdinary differential equations can e classified as #)##%, $%,! "(/,!'or/()%"* $%,! "(/,!'. An equation is called an #)##%, $%,! "(/,! IVP4ifthe values of the dependent variales or derivatives are *nown at the initial valueof the independent variales. An equation for which the values of the dependentvariale or their derivatives are *nown at the final value of the independent varialeis called a +#)%, $%,! "(/,!. "f the dependent variale or its derivatives are*nown at more than one point of the independent variale# the differential equationis a /()%"*5$%,! "(/,!.
F#6"! 1The sequence of events in the application of $!+s forengineering prolem solving
3.1. O"#)%"* #++!"!)#%, !-%#()' 8#9 MATLAB +)#()
Te standard MATLA! "ac#a$e as built%in &unctions &or sol'in$ Es. Te standard
E sol'ers include t*o &unctions to im"lement te ada"ti'e ste" si+e ,un$e%
-utta metod. Tese are ode23 *ic uses second% and tird%order &ormula to
attain medium accuracy and ode45 *ic uses &ort% and &i&t%order &ormulas to
attain i$er accuracy.Te &ull synta/ o& tese &unctions
[t, y]=solver (your_function, tspan, y0)
y e"endent 'ariable
t nde"endent 'ariable
Solver ode23 or ode5
your_function Tis &unction contains te Es you *ant to sol'e
2
-
8/10/2019 e7 Ordinary Differential Equation New_0
3/28
ENT 258 Numerical Analysis Laboratory Module
tspan A 'ector s"eci&yin$ te inter'al o& inte$ration to t&4. To obtain
solutions at s"eci&ic times (all increasin$ or all decreasin$) use
ts"antot1..t&4.
y0 A 'ector o& initial conditions
Tere are tree *ays to de&ine a &unction
% M-file
% Inline function
E:%,! 7.1
,ind the solution of the prolem
1)0(6 == yy
t
dt
dyand e&act solution is 1)( 2 += tty
in interval - t 2 with step si/e is -.2# using MATLAB functions (!23and (!;#),#)!?.@*?4
The other way is via a function M-file# y typing
+)#() > +)*4
> .@*
To calculate and plot the appro&imate solution )(tya on the interval -# 2. Type
33 (!;
-
8/10/2019 e7 Ordinary Differential Equation New_0
4/28
ENT 258 Numerical Analysis Laboratory Module
To e&tract numerical values type 6-de78 does not produce a graph9
==*%>(!;(!;'-".214
== +("% ,()6
== *% *
%)' >
- 1.-------------- 1.--------------
-.2------------- 1.-1>-(2
-
8/10/2019 e7 Ordinary Differential Equation New_0
5/28
ENT 258 Numerical Analysis Laboratory Module
3.2. N!"#%, M!9(
3.2.1. E,!"' M!9(
Although +ulers method can e derived in several ways# the derivation ased on Taylors
series e&pansion is considered here. The value of 1+iy can e e&pressed using Taylors
series e&pansion aoutxi# as
)6(2
1
772
7
1 ++
-
8/10/2019 e7 Ordinary Differential Equation New_0
6/28
ENT 258 Numerical Analysis Laboratory Module
F,(89%" E,!"
9
:es
No
;tart
/(1)/0 y(1)y
0
ste"s si+e h
;T< ;tabilit
y
hxx ii +=+1
).(1 iiii yxhfyy +=+
i=Ni 1
i N >
-
8/10/2019 e7 Ordinary Differential Equation New_0
7/28
ENT 258 Numerical Analysis Laboratory Module
E:%,! 7.2
se +ulers method to integrateyey t 5.0 8.07 =
from t:- to 7 with step si/e of 1. Theinitial condition at t:- is y:2. Dote that the e&act solution can e determined analytically as
ttt eeey 5.05.08.0 2)(3.1
+=
P"(!"!'5MATLAB P"(6"%1. %tart a new MatLabfile y clic*ing on F#,!# N!8and M5+#,!that opens an empty file in
the +ditorE!eugger window.2. Frite the program given elow in M5+#,!
?n"ut % & is te &unction entered as a strin$ 7&7
? % 7a7 and 7b7 are te le&t and ri$t end"oints
? % 7ya7 is te initial condition y(a) @ de"endent 'alue
? % 7dt or Bdt7 is ste" si+e
? % Bta is te initial condition t(a) @ inde"endent 'alue
?ut"ut % E
df=input('Enter te ordinary differential e!uation(df"#'"$
f=input('Enter te e%act e!uation(f"#'"$a=input('Enter te left endpoints(a"#'"$
&=input('Enter te rit endpoints(&"#'"$
dt=input('Enter te step sie(dt"#'"$
ya=input('Enter te initial condition(ya"#'"$
%a=input('Enter te initial condition(%a"#'"$
M=(&-a")dt$
t=eros(M*+"$
y_euler=eros(M*+"$
y_e%act=eros(M*+"$
error=eros(M*+"$y_euler(+"=ya$
y_e%act(+"=ya$
t(+"=ta$
for ,=+#M
t(,+"=t(,"dt$
y_euler(,+"=y_euler(,"(dt.feval(df*t(,"*y_euler(,"""$
y_e%act(,+"=feval(f*t(,+""$
error(,+"=a&s((y_e%act(,+"-y_euler(,+"")y_e%cat(,+"".+00$
end
E=/t y_euler y_e%act error
-
8/10/2019 e7 Ordinary Differential Equation New_0
8/28
ENT 258 Numerical Analysis Laboratory Module
3. Clic* on S%$! A'to save it as E,!"..;. To see how it wor*s# type E,!" in MatLab Command Window.
3.2.2. I"($!!)' (+ E,!"' M!9(
3.2.2.1 H!)' M!9(
"n +ulers method# the value of the function f 6x#y9# which denotes the derivativedx
dy# is
computed at the eginning of the interval hand is assumed to e a constant over the
entire interval. This assumption is a ma@or source of error since the derivative#dx
dy#
changes from point over the interval h. "n Geuns method# the derivative or slope#dx
dy# is
compute at two points'one at the eginning and the other at the end of the interval h'andtheir average value is used to achieve an improvement.
Hecall that in +ulers method# the slope at the eginning of an interval
( )iii yxfy 7= 6>.29
"s used to e&trapolate linearly to 1+iy 0
( )hyxfyyiiii
0 1 +=+ 6>.(9
,or the standard +uler method we would stop at this point. Gowever# in Geuns method the0
1+iy calculated in +q.6>.(9 is not the final answer# ut an intermediate prediction. This is
why we have distinguished it with a superscript -. +quation6>.(9 is called a predictor
equation. "t provides an estimate of 1+iy that allows the calculation of an estimated slope
at the end of the interval0
( 0 117
1 +++ = iii yxfy 6>.79
Thus# the two slopes +qs.6>.29 and 6>.79 can e comined to otain an average slope forthe interval0
( ) (2
2
0
11
7
1
7
7 +++ +=+
= iiiiiiyxfyxfyy
y
This average slope is then used to e&trapolate linearly from iy
to1+
iy
using +ulersmethod0
( ) ( )h
yxfyxfyy iiiiii
2
011
1
+++
++=
which is called a corrector equation. The Geun method is apredictor-corrector approach.
8
-
8/10/2019 e7 Ordinary Differential Equation New_0
9/28
ENT 258 Numerical Analysis Laboratory Module
The computational procedure of Geuns method can e stated as follows0
1. %tart with the *nown initial condition# ( ) 00 yxy = .
2. Choose h(. ,ind the value of
( )01+iy as
( ) ( )iiii yxhfyy .0
1 +=+
7. !etermine the value of yatxiI 1
( ) ( )( )
++= +++
2
0
11
1
iiii
ii
yxfyxfhyy '8#9( #!"%#()
or
( ) ( ) ( )( )
.....21062
1111 =
++= ++++
yxfyxfhyy
iiii
i
i 58#9 #!"%#()
8. Chec* the convergence criterion as
( ) ( )
( ) si
i
i
y
yy
+
+
++
+1
1
1
1
1%1it iteration
?. Chec* the staility of the solution y changing h6ma*e it smaller9a9 "f the solution changes for the selected numer of significant figures#
recalculate y decreasing h6go to step 299 "f the solution does not change for the selected numer of significant
figures# that is the final solution
C
-
8/10/2019 e7 Ordinary Differential Equation New_0
10/28
ENT 258 Numerical Analysis Laboratory Module
F,(8C9%" (+ H!)' M!9(
10
;tart
/(1)/0 y(1)y
0
Number o& ste"s N
:es
No
;T< ;tabilit
y
i=!
i 0
i 1> i
( )iiii yxhfyy 1 +=+
N
0
1>
( ) ( ) ( )( )
++= ++++
2
1111
iiii
i
i
yxfyxfhyy
-
8/10/2019 e7 Ordinary Differential Equation New_0
11/28
ENT 258 Numerical Analysis Laboratory Module
D
E:%,! 7.3
se Guens method to integrate yey t 5.0 8.07 = from t:- to 7 with step si/e of 1. The
initial condition at t:- is y:2. Dote that the e&act solution can e determined analytically as
ttt eeey 5.05.08.0 2)(3.1
+=
P"(!"!'5MATLAB P"(6"%
1. %tart a new MatLabfile y clic*ing on F#,!# N!8and M5+#,!that opens an empty file inthe +ditorE!eugger window.
2. Frite the program given elow in M5+#,!
?n"ut % & is te &unction entered as a strin$ 7&7
? % 7a7 and 7b7 are te le&t and ri$t end"oints
? % 7ya7 is te initial condition y(a)
? % 7dt7 is te ste" si+e
? % Bta is te initial condition t(a)
?ut"ut @ ,
df=input('Enter the ordinary differential equation(df):');f=input('Enter the exact equation(f):');a=input('Enter the left endpoints(a):');b=input('Enter the right endpoints(b):');dt=input('Enter the step size(dt):');ya=input('Enter the initial condition(ya):');xa=input('Enter the initial condition(xa):');esp=input(Enter the percent tolerance(esp):);=input(Enter the nu!ber of iteration():);
"=(b#a)$dt;t=zeros("%&);yhuen=zeros("%&);yexact=zeros("%&);
( ) ( )
( )
i
i
yyy + +
+
+1
1
1
1
11
:es
No
hxx ii +=+1
-
8/10/2019 e7 Ordinary Differential Equation New_0
12/28
ENT 258 Numerical Analysis Laboratory Module
error=zeros("%&);yhuen(&)=ya;yexact(&)=ya;t(&)=ta;
for =&:" t(&)=t()dt; yexact(&)=fe*al(f%t(&)); yhuen(&)=yhuen()fe*al(df%t()%yhuen())+dt; ,yhuen(&)=yhuen()(dt$-+(fe*al(df%t()%yhuen())fe*al(df%t(&)%yhuen(&))));
for i=&: yhuen(&)=yhuen()(dt$-+(fe*al(df%t()%yhuen())fe*al(df%t(&)%yhuen(&))));
error&(&)=abs((yhuen(&)#yhuen())$yhuen(&))+&..;if error& / esp breaend
yhuen(&)=yhuen(&)
end
error(&)=abs((yexact(&)#yhuen(&))$yexact(&))+&..end
0=1t yhuen yexact error2;
3. Clic* on S%$! A'to save it as H!)..;. To see how it wor*s# type H!) in MatLab Command Window.
3.2.2. R)6!5K%' M!9(
These methods are used for the solution of first order $!+ 6linear and nonlinear9.They are a particular set of self' starting numerical methods. They can e used togenerate an entire solution. They are more accurate than +ulers method# ut thecalculations are more involved
Huge'Jutta methods require only one initial point to start the procedure. The solutionusing Huge'Juttas method can e stated in the form0
)(1 hyxhyy iiii +=+ 6>.89
where )( hyx ii is called the increment function# which is chosen to represent the
average slope over the interval 1+ ii xxx . The increment function can e e&pressed as
nniiccchyx +++= ...)(
2211
12
-
8/10/2019 e7 Ordinary Differential Equation New_0
13/28
ENT 258 Numerical Analysis Laboratory Module
where ndenotes the order of the Hunge'Juttas method) nccc ... 21 are constants) and
n ... 21 are recurrence relations given y
)(
)(
)(
23213133
12122
1
hahayhpxf
ayhpxf
yxf
ii
ii
ii
+++=++=
=
.
.
.
and)....( 112211 +++++= nnnnninin hahahayhpxf 6>.?9
where p and a are constants.
3.2.2.1. S!()5O"!" R)6!5K% M!9(
Fe now consider three of most commonly used versions of second'order Hunge'Juttamethod4 Geuns method# Midpoints method# and Halstons method.
M#(#)' M!9(
21 hyy ii +=+ 6>.>9
with
)(1 ii yxf =
)2
2
(12
hy
hxf
ii ++=
H!)' M!9(
)(2
1211 hyy ii ++=+ 6>.=9
with
)(1 ii yxf =)( 12 hyhxf ii ++=
R%,'()' M!9(
)2(31
211 hyy ii ++=+ 6>.
-
8/10/2019 e7 Ordinary Differential Equation New_0
14/28
ENT 258 Numerical Analysis Laboratory Module
with
)(1 ii yxf =
)
3
3( 12 hyhxf ii ++=
R)6!5K%' M!9( P"(!"!
1. %tart with the *nown initial condition# ( ) 00 yxy = .
2. Choose h
(. Calculate the solution at each node point y using +q. 6>.>9'M#(#)' M!9(+q. 6>.=9'H!)' M!9( or +q. 6>.
-
8/10/2019 e7 Ordinary Differential Equation New_0
15/28
ENT 258 Numerical Analysis Laboratory Module
E:%,! 7.3
se +ulers method to integrate yey t 5.0 8.07 = from t:- to 7 with step si/e of 1. The
initial condition at t:- is y:2. Dote that the e&act solution can e determined analytically as
15
i i >1
-
8/10/2019 e7 Ordinary Differential Equation New_0
16/28
ENT 258 Numerical Analysis Laboratory Module
ttt eeey 5.05.08.0 2)(3.1
+=
P"(!"!'5MATLAB P"(6"%1. %tart a new MatLabfile y clic*ing on F#,!# N!8and M5+#,!that opens an emptyfile in the +ditorE!eugger window.
2. Frite the program given elow in M5+#,!
function =(f*a*&*ya* %a* "
?n"ut % & is te &unction entered as a strin$ 7&7
? % 7a7 and 7b7 are te le&t and ri$t end"oints
? % 7ya7 is te initial condition y(a)
? % 77 is te ste" si+e
? % Bta is te initial condition t(a)?ut"ut @ ,
df=input('Enter te ordinary differential e!uation(df"#'"$
f=input('Enter te e%act e!uation(f"#'"$
a=input('Enter te left endpoints(a"#'"$
&=input('Enter te rit endpoints(&"#'"$
dt=input('Enter te step sie(dt"#'"$
ya=input('Enter te initial condition(ya"#'"$
ta= input('Enter te initial condition(ta"#'"$
M=(&-a")dt$t=eros(M*+"$
y_r2=eros(M*+"$
y_e%act=eros(M*+"$
error=eros(M*+"$
y_r2(+"=ya$
y_e%act(+"=ya$
t(+"=ta$
for =+#M
t(+"=t("dt$
y_e%act(+"=feval(f*t(+""$ +=feval(df*t("*y_r2(""$
2=feval(df*t("dt*y_r2("(+.dt""$
y_r2(+"=y_r2("((+2")2"$
error(+"=a&s(y_e%act(+"-y_r2(+")y_e%act(+"".+00$
end
=/t y_r2 y_e%act error$
(. Clic* on S%$! A'to save it as R..7. To see how it wor*s# type R2 in MatLab Command Window.
19
-
8/10/2019 e7 Ordinary Differential Equation New_0
17/28
ENT 258 Numerical Analysis Laboratory Module
3.2.2.2. F("95O"!" R)6!5K% M!9('The fourth'order Hunge'Jutta methods are most popularly used and more accurate iscompared with the second'order Hunge'Jutta methods. The iterative process is given y
)22(9
3211
hyy
ii ++++=+ 6>.1-9
where
)(1 ii yxf =
)2
1
2
1( 12 hyhxf ii ++=
)
2
1
2
1( 23 hyhxf ii ++=
and
)( 3 hyhxf ii ++=
R)6!5K%' M!9( P"(!"!
1. %tart with the *nown initial condition# ( ) 00 yxy = .
2. Choose h.
(. Calculate the solution at each node point y using +q. 6>.1-9
7. Chec* the staility of the solution y changing h6ma*e it smaller9a9. "f the solution changes for the selected numer of significant figures# recalculate
y decreasing h6go to step 299. "f the solution does not change for the selected numer of significant figures# that is
the final solution
F,(89%" F("95O"!" R)6!5K%
1
;tart
-
8/10/2019 e7 Ordinary Differential Equation New_0
18/28
ENT 258 Numerical Analysis Laboratory Module
E:%,! 7.;
se +ulers method to integrate yey t 5.0 8.07 = from t:- to 7 with step si/e of 1. The
initial condition at t:- is y:2. Dote that the e&act solution can e determined analytically as
18
:es
No
/(1)/0
y(1)y0
;T