Differential Equations Using Scilab
Total Page:16
File Type:pdf, Size:1020Kb
Differential equations using Scilab Kannan M. Moudgalya IIT Bombay [email protected] Scilab Workshop 13 September 2010 Kannan Moudgalya Differential equations using Scilab 1/31 Outline I Weight reduction ODE model - analytical solution I Numerical integration I Functions in Scilab I Euler's method I Predator-prey system I Modelling I Euler method - user created integrator I Backward difference method - built-in function Kannan Moudgalya Differential equations using Scilab 2/31 Weight Reduction Model I Weight of person = x kg I Tries to reduce weight I Weight loss per month = 10% of weight I Starting weight = 100 kg dx = −0:1x dt Initial conditions: x = 100 at t=0 Determine x(t) as a function of t. Kannan Moudgalya Differential equations using Scilab 3/31 Analytical Solution of Simple ModelI Recall the model: dx = −0:1x dt x(t = 0) = 100 dx Cross multiplying, = −0:1dt x Integrating both sides from 0 to t, Z dx Z = −0:1 dt x C + ln x(t) = −0:1t Kannan Moudgalya Differential equations using Scilab 4/31 Analytical Solution of Simple ModelII Using initial conditions, C = − ln 100 Thus, the final solution is, x(t) ln = −0:1t or x(t) = 100e−0:1t 100 Kannan Moudgalya Differential equations using Scilab 5/31 Solution, Continued I Weight of person = x kg I Tries to reduce weight I Weight loss per month = 10% of weight I Starting weight = 100 kg x(t) = 100e−0:1t Compute, plot for 2 years, i.e. for 24 months: 1 T=0:0.1:24; 2 plot2d(T,100 ∗ exp ( −0.1∗T)); 3 xtitle('Weight v s . month ' , . 4 ' Time i n months ' , . 5 ' Weight ( kg ) ' ) Kannan Moudgalya Differential equations using Scilab 6/31 Weight vs. month Weight (kg) 100 90 80 70 60 50 40 30 20 10 Time, in months 0 0 4 8 12 16 20 24 Kannan Moudgalya Differential equations using Scilab 7/31 Need for Numerical Solution I Exact solution is ok for simple models I What if the model is complicated? I Consider integrating the more difficult problem: dx 2 + 18t + 68t2 + 180t3 + 250t4 + 250t5 = dt x2 with initial condition, x(t = 0) = 1 I Analytical (i.e. exact) solution difficult to find I Let us look for a numerical solution Kannan Moudgalya Differential equations using Scilab 8/31 Simple numerical solution: Explicit Euler I Suppose that we want to integrate dx = g(x; t) dt with initial condition: x(t = 0) = x0 I Approximate numerical method - divide time into equal intervals: t0, t1, t2, etc. xn − xn−1 = g(xn−1; tn−1) ∆t I Simplifying, xn − xn−1 = ∆t g(xn−1; tn−1) xn = xn−1 + ∆t g(xn−1; tn−1) Kannan Moudgalya Differential equations using Scilab 9/31 I Given x0, can march forward and determine xn for all future n. Example revisited Recall the problem statement for numerical solution: dx 2 + 18t + 68t2 + 180t3 + 250t4 + 250t5 = dt x2 with initial condition, x(t = 0) = 1 Recall the Euler method: dx = g(x; t) dt Solution for initial condition, x(t = 0) = x0 is, Kannan Moudgalya Differential equations using Scilab 10/31 xn = xn−1 + ∆t g(xn−1; tn−1) Scilab CodeI 1 exec("diff1 .sci"); 2 exec("Euler. sci"); 3 x0=1; t0=0; T=0:1:24; 4 sol = Euler(x0,t0,T,diff1); 5 // sol = ode(x0,t0,T,diff1 ); 6 plot2d(T,sol), pause 7 plot2d(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 Scilab CodeII 1 function x = Euler(x0,t0,t,g) 2 n = length(t), x = x0; 3 f o r j = 1 : n−1 4 x0 = x0 + (t(j+1)−t ( j ) ) ∗ g(t(j),x0); 5 x = [ x x0 ] ; 6 end ; 7 endfunction 1 function xdot = diff1(t,x) 2 xdot = (2+18∗ t +68∗ t ^2+180∗ t ^3+250∗ t ^4+250∗ t ^5)/ x ^ 2 ; 3 endfunction Kannan Moudgalya Differential equations using Scilab 12/31 Comparison of numerical, exact solution x vs. t: Polynomial Problem x 3200 2800 2400 2000 1600 1200 800 400 t 0 0 4 8 12 16 20 24 Kannan Moudgalya Differential equations using Scilab 13/31 Predator-Prey Problem I I Population dynamics of predator-prey I Prey can find food, but gets killed on meeting predator I Examples: parasites and certain hosts; wolves and rabbits I x1(t) - number of prey; x2(t) - number of predator at time t I Prey, if left alone, grows at a rate proportional to x1 Kannan Moudgalya Differential equations using Scilab 14/31 Predator-Prey Problem II I Predator, on meeting prey, kills it ) proportional to x1x2 dx1 = 0:25x1 − 0:01x1x2 dt I Predator, if left alone, decrease by natural causes I Predators increase their number on meeting prey dx2 = −x2 + 0:01x1x2 dt I Determine x1(t), x2(t) when x1(0) = 80, x2(0) = 30 Kannan Moudgalya Differential equations using Scilab 15/31 Explicit Euler for a System of EquationsI dx1 = g1(x1;:::; xn; t) dt . dxN = gn(x1;:::; xn; t) dt Kannan Moudgalya Differential equations using Scilab 16/31 Explicit Euler for a System of EquationsII 2 3 2 3 x1 g1(x1;:::; xn; t − 1) d . 4 . 5 = 4 . 5 dt xn gn(x1;:::; xn; t − 1) 2 3 2 3 2 3 x1 x1 g1((x1;:::; xn)jt−1; t − 1) . 4 . 5 = 4 . 5 + ∆t 4 . 5 xn t xn t−1 gN((x1;:::; xn)jt−1; t − 1) Solution in vector form: xt = xt−1 + ∆tg(xt−1) Kannan Moudgalya Differential equations using Scilab 17/31 Scilab Code for Predator-Prey ProblemI 1 exec("pred. sci"); 2 exec("Euler. sci"); 3 x0=[80,30]'; t0=0; T=0:0.1:20; T=T'; 4 sol = Euler(x0,t0,T,pred); 5 // sol = ode(x0,t0 ,T,pred); 6 c l f ( ) ; 7 plot2d(T,sol ') 8 xset( 'window' ,1) 9 plot2d(sol(2,:),sol(1,:)) Kannan Moudgalya Differential equations using Scilab 18/31 Scilab Code for Predator-Prey ProblemII 1 function x = Euler(x0,t0,t,g) 2 n = length(t), x = x0; 3 f o r j = 1 : n−1 4 x0 = x0 + (t(j+1)−t ( j ) ) ∗ g(t(j),x0); 5 x = [ x x0 ] ; 6 end ; 7 endfunction 1 function xdot = pred(t,x) 2 xdot(1) = 0.25 ∗ x (1) −0.01∗ x ( 1 ) ∗ x ( 2 ) ; 3 xdot ( 2 ) = −x (2)+0.01∗ x ( 1 ) ∗ x ( 2 ) ; 4 endfunction Kannan Moudgalya Differential equations using Scilab 19/31 Predator-Prey Problem: Solution by Euler Predator−Prey by Euler for different step sizes, Black = 0.25, Red = 0.1 Prey 140 130 120 110 100 90 80 70 Predator 60 10 14 18 22 26 30 34 38 42 46 As step size increases, the solution diverges more from theKannan actual. Moudgalya Differential equations using Scilab 20/31 General method to handle stiff systems I The predator-prey problem is an example of a stiff system I Results because of sudden changes in the derivative I Approximation of using previous time values does not work I General approach to solve this problem: xn = xn−1 + ∆t g(xn; tn) I Requires solution by trial and error, as g(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 General method to handle stiff systems I Derived from ODEPACK I FOSS I In use for thirty years I Bugs have been removed by millions of users Kannan Moudgalya Differential equations using Scilab 22/31 Predator-Prey Problem by Scilab IntegratorI Execute the following code, after commenting out Euler and uncommenting ode: 1 exec("pred. sci"); 2 exec("Euler. sci"); 3 x0=[80,30]'; t0=0; T=0:0.1:20; T=T'; 4 sol = Euler(x0,t0,T,pred); 5 // sol = ode(x0 ,t0 ,T,pred); 6 c l f ( ) ; 7 plot2d(T,sol ') 8 xset( 'window' ,1) Kannan Moudgalya Differential equations using Scilab 23/31 Predator-Prey Problem by Scilab IntegratorII 9 plot2d(sol(2,:),sol(1,:)) 1 function xdot = pred(t,x) 2 xdot(1) = 0.25 ∗ x (1) −0.01∗ x ( 1 ) ∗ x ( 2 ) ; 3 xdot ( 2 ) = −x (2)+0.01∗ x ( 1 ) ∗ x ( 2 ) ; 4 endfunction Kannan Moudgalya Differential equations using Scilab 24/31 Predator-Prey Problem: Scilab Integrator Predator−Prey by Scilab integrator Prey 130 120 110 100 90 80 Predator 70 15 19 23 27 31 35 39 Use the Scilab built-in integrator to get the correct solution Kannan Moudgalya Differential equations using Scilab 25/31 Partial Differential Equations Kannan Moudgalya Differential equations using Scilab 26/31 Parabolic Differential Equations I Heat conduction equation I Diffusion equation @u(t; x) @2u(t; x) = c @t @x2 I Initial condition: u(0; x) = g(x); 0 ≤ x ≤ 1 I Boundary conditions: u(t; 0) = α, u(t; 1) = β; t ≥ 0 m I Let uj be approximate solution at xj = j∆x, tm = m∆t um+1 − um c j j = (um − 2um + um ) ∆t (∆x)2 j−1 j j+1 Kannan Moudgalya Differential equations using Scilab 27/31 Finite Difference Approach um+1 − um c c∆t j j = (um − 2um + um ); µ = ∆t (∆x)2 j−1 j j+1 (∆x)2 m+1 m m m m uj = uj + µ(uj−1 − 2uj + uj+1) m m m = µuj−1 + (1 − 2µ)uj + µuj+1 Write this equation at every spatial grid: m+1 m m m u1 = µu0 + (1 − 2µ)u1 + µu2 m+1 m m m u2 = µu1 + (1 − 2µ)u2 + µu3 .