di erential equations using scilab · 13/09/2010 · i weight loss per month = 10% of weight i...
TRANSCRIPT
![Page 1: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/1.jpg)
Differential equations usingScilab
Kannan M. MoudgalyaIIT Bombay
Scilab Workshop13 September 2010
Kannan Moudgalya Differential equations using Scilab 1/31
![Page 2: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/2.jpg)
Outline
I Weight reduction ODE model - analyticalsolution
I Numerical integrationI Functions in ScilabI Euler’s method
I Predator-prey systemI ModellingI Euler method - user created integratorI Backward difference method - built-in
function
Kannan Moudgalya Differential equations using Scilab 2/31
![Page 3: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/3.jpg)
Weight Reduction Model
I Weight of person = x kgI Tries to reduce weightI Weight loss per month = 10% of weightI Starting weight = 100 kg
dx
dt= −0.1x
Initial conditions:
x = 100 at t=0
Determine x(t) as a function of t.Kannan Moudgalya Differential equations using Scilab 3/31
![Page 4: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/4.jpg)
Analytical Solution of Simple Model I
Recall the model:
dx
dt= −0.1x
x(t = 0) = 100
Cross multiplying,dx
x= −0.1dt
Integrating both sides from 0 to t,∫dx
x= −0.1
∫dt
C + ln x(t) = −0.1tKannan Moudgalya Differential equations using Scilab 4/31
![Page 5: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/5.jpg)
Analytical Solution of Simple Model II
Using initial conditions,
C = − ln 100
Thus, the final solution is,
lnx(t)
100= −0.1t or x(t) = 100e−0.1t
Kannan Moudgalya Differential equations using Scilab 5/31
![Page 6: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/6.jpg)
Solution, Continued
I Weight of person = x kgI Tries to reduce weightI Weight loss per month = 10% of weightI Starting weight = 100 kg
x(t) = 100e−0.1t
Compute, plot for 2 years, i.e. for 24 months:
1 T= 0 : 0 . 1 : 2 4 ;2 p l o t 2 d (T, 1 0 0∗ exp (−0.1∗T ) ) ;3 x t i t l e ( ’ Weight v s . month ’ , . . .4 ’ Time i n months ’ , . .5 ’ Weight ( kg ) ’ )
Kannan Moudgalya Differential equations using Scilab 6/31
![Page 7: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/7.jpg)
0 4 8 12 16 20 240
10
20
30
40
50
60
70
80
90
100
Weight vs. month
Time, in months
Weight (kg)
Kannan Moudgalya Differential equations using Scilab 7/31
![Page 8: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/8.jpg)
Need for Numerical Solution
I Exact solution is ok for simple models
I What if the model is complicated?
I Consider integrating the more difficultproblem:
dx
dt=
2 + 18t + 68t2 + 180t3 + 250t4 + 250t5
x2
with initial condition, x(t = 0) = 1
I Analytical (i.e. exact) solution difficult tofind
I Let us look for a numerical solutionKannan Moudgalya Differential equations using Scilab 8/31
![Page 9: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/9.jpg)
Simple numerical solution: Explicit Euler
I Suppose that we want to integrate
dx
dt= g(x, t)
with initial condition: x(t = 0) = x0
I Approximate numerical method - dividetime into equal intervals: t0, t1, t2, etc.
xn − xn−1
∆t= g(xn−1, tn−1)
I Simplifying,
xn − xn−1 = ∆t g(xn−1, tn−1)
xn = xn−1 + ∆t g(xn−1, tn−1)
I Given x0, can march forward anddetermine xn for all future n.
Kannan Moudgalya Differential equations using Scilab 9/31
![Page 10: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/10.jpg)
Example revisited
Recall the problem statement for numericalsolution:
dx
dt=
2 + 18t + 68t2 + 180t3 + 250t4 + 250t5
x2
with initial condition,
x(t = 0) = 1
Recall the Euler method:
dx
dt= g(x, t)
Solution for initial condition, x(t = 0) = x0 is,
xn = xn−1 + ∆t g(xn−1, tn−1)Kannan Moudgalya Differential equations using Scilab 10/31
![Page 11: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/11.jpg)
Scilab Code I
1 exec ( ” d i f f 1 . s c i ” ) ;2 exec ( ” E u l e r . s c i ” ) ;3 x0 =1; t0 =0; T= 0 : 1 : 2 4 ;4 s o l = E u l e r ( x0 , t0 , T , d i f f 1 ) ;5 / / s o l = o d e ( x 0 , t 0 , T , d i f f 1 ) ;
6 p l o t 2 d (T , s o l ) , pause7 p l o t 2 d (T,1+2∗T+5∗Tˆ 2 , 5 )8 x t i t l e ( ’ x v s . t : P o l y n o m i a l Problem ’ , ’ t ’ , ’ x ’ )
Kannan Moudgalya Differential equations using Scilab 11/31
![Page 12: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/12.jpg)
Scilab Code II
1 f u n c t i o n x = E u l e r ( x0 , t0 , t , g )2 n = l e n g t h ( t ) , x = x0 ;3 f o r j = 1 : n−14 x0 = x0 + ( t ( j +1)−t ( j ) ) ∗ g ( t ( j ) , x0 ) ;5 x = [ x x0 ] ;6 end ;7 e n d f u n c t i o n
1 f u n c t i o n xdot = d i f f 1 ( t , x )2 xdot = (2+18∗ t +68∗ t ˆ2+180∗ t ˆ3+250∗ t ˆ4+250∗ t ˆ5)/ x ˆ 2 ;3 e n d f u n c t i o n
Kannan Moudgalya Differential equations using Scilab 12/31
![Page 13: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/13.jpg)
Comparison of numerical, exact solution
0 4 8 12 16 20 240
400
800
1200
1600
2000
2400
2800
3200
0 4 8 12 16 20 240
400
800
1200
1600
2000
2400
2800
3200
x vs. t: Polynomial Problem
t
x
Kannan Moudgalya Differential equations using Scilab 13/31
![Page 14: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/14.jpg)
Predator-Prey Problem I
I Population dynamics of predator-prey
I Prey can find food, but gets killed onmeeting predator
I Examples: parasites and certain hosts;wolves and rabbits
I x1(t) - number of prey; x2(t) - number ofpredator at time t
I Prey, if left alone, grows at a rateproportional to x1
Kannan Moudgalya Differential equations using Scilab 14/31
![Page 15: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/15.jpg)
Predator-Prey Problem II
I Predator, on meeting prey, kills it ⇒proportional to x1x2
dx1
dt= 0.25x1 − 0.01x1x2
I Predator, if left alone, decrease by naturalcauses
I Predators increase their number onmeeting prey
dx2
dt= −x2 + 0.01x1x2
I Determine x1(t), x2(t) when x1(0) = 80,x2(0) = 30
Kannan Moudgalya Differential equations using Scilab 15/31
![Page 16: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/16.jpg)
Explicit Euler for a System of Equations I
dx1
dt= g1(x1, . . . , xn, t)
...
dxN
dt= gn(x1, . . . , xn, t)
Kannan Moudgalya Differential equations using Scilab 16/31
![Page 17: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/17.jpg)
Explicit Euler for a System of Equations II
d
dt
x1...
xn
=
g1(x1, . . . , xn, t− 1)...
gn(x1, . . . , xn, t− 1)
x1
...xn
t
=
x1...
xn
t−1
+ ∆t
g1((x1, . . . , xn)|t−1, t− 1)...
gN((x1, . . . , xn)|t−1, t− 1)
Solution in vector form:
xt = xt−1 + ∆tg(xt−1)
Kannan Moudgalya Differential equations using Scilab 17/31
![Page 18: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/18.jpg)
Scilab Code for Predator-Prey Problem I
1 exec ( ” pred . s c i ” ) ;2 exec ( ” E u l e r . s c i ” ) ;3 x0 = [ 8 0 , 3 0 ] ’ ; t0 =0; T= 0 : 0 . 1 : 2 0 ; T=T ’ ;4 s o l = E u l e r ( x0 , t0 , T , pred ) ;5 / / s o l = o d e ( x 0 , t 0 , T , p r e d ) ;
6 c l f ( ) ;7 p l o t 2 d (T , s o l ’ )8 x s e t ( ’ window ’ , 1 )9 p l o t 2 d ( s o l ( 2 , : ) , s o l ( 1 , : ) )
Kannan Moudgalya Differential equations using Scilab 18/31
![Page 19: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/19.jpg)
Scilab Code for Predator-Prey Problem II
1 f u n c t i o n x = E u l e r ( x0 , t0 , t , g )2 n = l e n g t h ( t ) , x = x0 ;3 f o r j = 1 : n−14 x0 = x0 + ( t ( j +1)−t ( j ) ) ∗ g ( t ( j ) , x0 ) ;5 x = [ x x0 ] ;6 end ;7 e n d f u n c t i o n
1 f u n c t i o n xdot = pred ( t , x )2 xdot ( 1 ) = 0 . 2 5∗ x (1)−0.01∗ x ( 1 )∗ x ( 2 ) ;3 xdot ( 2 ) = −x (2)+0.01∗ x ( 1 )∗ x ( 2 ) ;4 e n d f u n c t i o n
Kannan Moudgalya Differential equations using Scilab 19/31
![Page 20: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/20.jpg)
Predator-Prey Problem: Solution by Euler
10 14 18 22 26 30 34 38 42 4660
70
80
90
100
110
120
130
140
10 14 18 22 26 30 34 38 42 4660
70
80
90
100
110
120
130
140
Predator−Prey by Euler for different step sizes, Black = 0.25, Red = 0.1
Predator
Prey
As step size increases, the solution divergesmore from the actual.
Kannan Moudgalya Differential equations using Scilab 20/31
![Page 21: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/21.jpg)
General method to handle stiff systems
I The predator-prey problem is an exampleof a stiff system
I Results because of sudden changes in thederivative
I Approximation of using previous timevalues does not work
I General approach to solve this problem:
xn = xn−1 + ∆t g(xn, tn)
I Requires solution by trial and error, asg(xn, tn) is unknown
I Scilab has state of the art methods (ode)to solve such systems
Kannan Moudgalya Differential equations using Scilab 21/31
![Page 22: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/22.jpg)
General method to handle stiff systems
I Derived from ODEPACKI FOSSI In use for thirty yearsI Bugs have been removed by millions of users
Kannan Moudgalya Differential equations using Scilab 22/31
![Page 23: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/23.jpg)
Predator-Prey Problem by ScilabIntegrator I
Execute the following code, after commentingout Euler and uncommenting ode:
1 exec ( ” pred . s c i ” ) ;2 exec ( ” E u l e r . s c i ” ) ;3 x0 = [ 8 0 , 3 0 ] ’ ; t0 =0; T= 0 : 0 . 1 : 2 0 ; T=T ’ ;4 s o l = E u l e r ( x0 , t0 , T , pred ) ;5 / / s o l = o d e ( x 0 , t 0 , T , p r e d ) ;
6 c l f ( ) ;7 p l o t 2 d (T , s o l ’ )8 x s e t ( ’ window ’ , 1 )
Kannan Moudgalya Differential equations using Scilab 23/31
![Page 24: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/24.jpg)
Predator-Prey Problem by ScilabIntegrator II
9 p l o t 2 d ( s o l ( 2 , : ) , s o l ( 1 , : ) )
1 f u n c t i o n xdot = pred ( t , x )2 xdot ( 1 ) = 0 . 2 5∗ x (1)−0.01∗ x ( 1 )∗ x ( 2 ) ;3 xdot ( 2 ) = −x (2)+0.01∗ x ( 1 )∗ x ( 2 ) ;4 e n d f u n c t i o n
Kannan Moudgalya Differential equations using Scilab 24/31
![Page 25: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/25.jpg)
Predator-Prey Problem: Scilab Integrator
15 19 23 27 31 35 3970
80
90
100
110
120
130
Predator−Prey by Scilab integrator
Predator
Prey
Use the Scilab built-in integrator to get thecorrect solution
Kannan Moudgalya Differential equations using Scilab 25/31
![Page 26: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/26.jpg)
Partial Differential Equations
Kannan Moudgalya Differential equations using Scilab 26/31
![Page 27: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/27.jpg)
Parabolic Differential Equations
I Heat conduction equation
I Diffusion equation
∂u(t, x)
∂t= c
∂2u(t, x)
∂x2
I Initial condition:
u(0, x) = g(x), 0 ≤ x ≤ 1
I Boundary conditions:
u(t, 0) = α, u(t, 1) = β, t ≥ 0
I Let umj be approximate solution at xj = j∆x,
tm = m∆t
um+1j − um
j
∆t=
c
(∆x)2(um
j−1 − 2umj + um
j+1)Kannan Moudgalya Differential equations using Scilab 27/31
![Page 28: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/28.jpg)
Finite Difference Approach
um+1j − um
j
∆t=
c
(∆x)2(um
j−1 − 2umj + um
j+1), µ =c∆t
(∆x)2
um+1j = um
j + µ(umj−1 − 2um
j + umj+1)
= µumj−1 + (1− 2µ)um
j + µumj+1
Write this equation at every spatial grid:
um+11 = µum
0 + (1− 2µ)um1 + µum
2
um+12 = µum
1 + (1− 2µ)um2 + µum
3
...
um+1N = µum
N−1 + (1− 2µ)umN + µum
N+1
Kannan Moudgalya Differential equations using Scilab 28/31
![Page 29: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/29.jpg)
Finite Difference Approach - Continued
um+11 = µum
0 + (1− 2µ)um1 + µum
2
um+12 = µum
1 + (1− 2µ)um2 + µum
3
...
um+1N = µum
N−1 + (1− 2µ)umN + µum
N+1
In matrix form,u1
u2...
uN
m+1
=
1− 2µ µµ 1− 2µ µ
. . .µ 1− 2µ
u1
u2...
uN
m
+
µum
00...
µumN+1
Kannan Moudgalya Differential equations using Scilab 29/31
![Page 30: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/30.jpg)
Conclusions
I Scilab is ideal for educational institutions,including schools
I Built on a sound numerical platform
I It has good integrators for differentialequations
I It is free
I Also suitable for industrial applications
Kannan Moudgalya Differential equations using Scilab 30/31
![Page 31: Di erential equations using Scilab · 13/09/2010 · I Weight loss per month = 10% of weight I Starting weight = 100 kg dx dt = 0:1x Initial conditions: x = 100 at t=0 Determine](https://reader035.vdocument.in/reader035/viewer/2022071008/5fc53d373aa27d4fb403518b/html5/thumbnails/31.jpg)
Thank you
Kannan Moudgalya Differential equations using Scilab 31/31