Systems of 1st-order IVPs and Higher-order IVPs Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca dwharder@alumni.uwaterloo.ca © 2012 by Douglas Wilhelm Harder. Some rights reserved. Systems of 1st-order IVPs and Higher-order IVPs Outline This topic takes us from simple first-order IVPs to: – Systems of IVPs – Higher-order IVPs • Systems of 2nd-order IVPs 2 Systems of 1st-order IVPs and Higher-order IVPs Outcomes Based Learning Objectives By the end of this laboratory, you will: – Understand how to approximate the solution to a system of N coupled IVPs – Understand the reformulation of an Nth-order IVP as a system of N coupled IVPs – Understand the reformulation of a system of N 2nd-order IVPs as a system of 2·N 1st-order IVPs 3 Systems of 1st-order IVPs and Higher-order IVPs Systems of ODEs A system of N coupled first-order ODEs are N different equations of the form: y1 t f1 t , y1 t , y2 t , , yN t y21 t f 2 t , y1 t , y2 t , , yN t yN 1 t f N t , y1 t , y2 t , , yN t 1 4 Systems of 1st-order IVPs and Higher-order IVPs 5 Systems of ODEs This is a simple system of two coupled ODEs: r 1 t r t 1 2 f t Rabbit families per acre: r(t) f 1 t f t r t 1 Fox families per acre : f(t) It represents the rates of growth of a predator and prey in a natural system: – Rabbits density will grow exponentially, r t 0 , if there aren’t too many foxes: f t 12 foxes per acre – Foxes will grow exponentially, f 1 t 0 , if there are sufficiently many rabbits: r t 1 fox per acre 1 Systems of 1st-order IVPs and Higher-order IVPs 6 Systems of IVPs However, there the relations are coupled: – As the rabbits grow, the foxes grow, – As the foxes grow, the density of rabbits shrinks faster How do the two interact? – Suppose that the initial state is: • A fox density of 0.3 foxes per acre • A rabbit density of 0.5 rabbits per acre r(0) = 0.3 f(0) = 0.5 – Initially: • The rabbit population will be increasing: • The fox population will be decreasing: 1 – 2 × 0.3 = 0.4 > 0 0.5 – 1 = –0.5 < 0 – Unfortunately, there is no exact solution to this system of ODEs Systems of 1st-order IVPs and Higher-order IVPs Systems of IVPs If you ask Maple 13 for a solution, it doesn’t find one: > dsolve( {D(r)(t) = r(t)*(1 - 2*f(t)), D(f)(t) = f(t)*(r(t) - 1), r(0) = 3/10, f(0) = 1/2} ); # No solution... We will have to find a numerical approximation… 7 Systems of 1st-order IVPs and Higher-order IVPs Systems of IVPs Solving this we expect the following behaviour: – As the rabbit population increases, so do the foxes – At some point, the foxes are so populous that they are killing most of the rabbits 8 Systems of 1st-order IVPs and Higher-order IVPs Foxes versus Rabits If we plot f(t) versus r(t), we note the solutions are cyclic – In nature, there are other variables that we don’t account for 9 Systems of 1st-order IVPs and Higher-order IVPs Lorenz Equations Another example are the Lorenz equations (N = 3) : x t y t x t 1 y t x t z t y t 1 z 1 t x t y t z t These three equations represent the relationships between three properties of atmospheric conditions x y x y x z y z xy z Here x, y and z are understood to be functions of time – These also do not have general solutions… 10 Systems of 1st-order IVPs and Higher-order IVPs Lorenz Equations As before, we would have to know the initial state of the system at some time t0: x t0 x0 y t 0 y0 z t0 z0 11 Systems of 1st-order IVPs and Higher-order IVPs Lorenz Equations Given = = = 1, the system converges on the origin – The initial point is (1, 1, 1) but any point will converge 12 Systems of 1st-order IVPs and Higher-order IVPs Lorenz Equations Given = = 1 and = –1, the system diverges spiralling along the z-axis – The initial point is (1, 1, 1) but most points will diverge 13 Systems of 1st-order IVPs and Higher-order IVPs Lorenz Equations Given = 10, = 99.96, and = 8/3, the system converges to a cycle – The initial point is (1, 1, 1) but all systems will approach the cycle (or attractor) 14 Systems of 1st-order IVPs and Higher-order IVPs Lorenz Equations Given = 10, = 28, and = 8/3, the system remains bounded; but there is no limit cycle – The initial point is (1, 1, 1) but most points will flow forever – The butterfly shape is called a strange attractor 15 Systems of 1st-order IVPs and Higher-order IVPs Systems of IVPs Solving systems of equations is, fortunately, straightforward: – Rewrite y1 t f1 t , y1 t , y2 t , , yN t y21 t f 2 t , y1 t , y2 t , , yN t yN1 t f N t , y1 t , y2 t , , yN t 1 y1 t y t 2 by starting with: y t yN t 16 Systems of 1st-order IVPs and Higher-order IVPs Systems of IVPs The differential equations become: y1 t f1 t , y1 t , y2 t , , yN t y1 t f1 t , y t y21 t f 2 t , y1 t , y2 t , , yN t y21 t f 2 t , y t yN1 t f N t , y1 t , y2 t , , yN t yN1 t f N t , y t 1 1 y1 t We can thus define a vector-valued function: y t y t 2 f1 t , y t f2 t, y t yN t f t, y t f N t, y t 17 Systems of 1st-order IVPs and Higher-order IVPs Systems of IVPs But, if y1 t y t y t 2 , it follows that yN t y11 t 1 y t 1 y t 2 y 1 t N f1 t , y t f2 t, y t 1 Therefore, y t f t, y t f N t, y t 18 Systems of 1st-order IVPs and Higher-order IVPs 19 Systems of IVPs In our two examples: function [dy] = f5a( t, y ) dy = [y(1)*(1 - 2*y(2)) y(2)*(y(1) - 1)]; end function [dy] = f5b( t, y ) sigma = 10; rho = 28; beta = 8/3; dy = [sigma*(y(2) - y(1)) y(1)*(rho - y(3)) - y(2) y(1)*y(2) - beta*y(3)]; end r 1 t r t 1 2 f t f 1 t f t r t 1 Here, the 2nd argument is a 2-dimensional column vector... Here it is a 3-d column vector x t y t x t 1 y t x t z t y t 1 z 1 t x t y t z t Systems of 1st-order IVPs and Higher-order IVPs Systems of ODEs We also need initial conditions: y1 t0 y t y t0 2 0 y 0 y t N 0 In our two examples: y5a0 = [3/10 1/2]'; y5b0 = [1 1 1]'; 20 Systems of 1st-order IVPs and Higher-order IVPs Systems of ODEs Thus, we have IVPs both a single ODE and a system of ODES: 1 1 t f t, y t y t 0 y0 y t f t, y t y t0 y 0 y 21 Systems of 1st-order IVPs and Higher-order IVPs Systems of ODEs With a single ODE, we saw that we could approximate: y t0 h y0 h f t0 , y t0 Is this any different with a system? y t0 h y 0 h f t0 , y t0 22 Systems of 1st-order IVPs and Higher-order IVPs 23 Systems of ODEs Thus, our output will be a vector of values: A vector for k = 1:(n - 1) K1 = f( t_out(k), of slopes y_out(k) ); K2 = f( t_out(k + 1), y_out(k) + h*K1 ); Scalar multiplication and vector addition y_out(k + 1) = y_out(k) + h*(K1 + K2)/2; end Scalar multiplication and vector addition Systems of 1st-order IVPs and Higher-order IVPs 24 Systems of ODEs You will recall with dp45, we began with an initial value and then continued to calculate subsequent entries: t_out = 0 0.1 0.2 0.3 0.5 0.7 y_out = 1 1.0321 1.7262 2.1570 2.5895 3.1539 Systems of 1st-order IVPs and Higher-order IVPs 25 Systems of ODEs We will now begin with a vector of initial values and at each step, calculate the solutions to all entries: t_out = 0 0.1 0.2 0.3 0.5 0.7 y_out = 1 2 1.0321 1.9832 1.7262 1.9792 2.1570 2.0012 2.5895 2.0678 3.1539 2.1134 Systems of 1st-order IVPs and Higher-order IVPs Systems of ODEs In fact, we can use the exact same routines: – For Heun’s method, we would simply determine the dimensions [N, y0_cols] = size( y0 ); if y0_cols ~= 1 % We have an exception end y_out = zeros( N, n ); y_out(:, 1) = y0; 26 Systems of 1st-order IVPs and Higher-order IVPs Systems of ODEs In addition, we will have to be more careful with assignments: for k = 1:(n - 1) K1 = f( t_out(k), y_out(:, k) ); K2 = f( t_out(k + 1), y_out(:, k) + h*K1 ); y_out(:, k + 1) = y_out(:, k) + h*(K1 + K2)/2; end Referring to the kth and (k + 1)st columns 27 Systems of 1st-order IVPs and Higher-order IVPs Modifying the Adaptive Euler-Heun Method 28 If you look at the code for an adaptive Euler-Heun, we need only one change: k = 1; while t_out(k) < tf K1 = f( t_out(k), y_out(:, k) ); K2 = f( t_out(k + 1), y_out(:, k) + h*K1 ); y = y_out(:, k) + h*K1; z = y_out(:, k) + h*(K1 + K2)/2; s = h*eps_step/(2*(tf - t0)*abs( y - z )); if s >= 2 % ... end end What is the absolute value of a vector? Systems of 1st-order IVPs and Higher-order IVPs Modifying the Adaptive Euler-Heun Method We cannot calculate the absolute value of the difference between two vectors: s = h*eps_abs/(2*(tf - t0)*abs( y - z )); Now, if both y and z approximate the system at time t_out(k + 1), they are both vectors approximating the same value – Two vectors are close if the norm of the difference is small – Assuming we want the final approximation to be within eabs, we must use the norm: s = h*eps_abs/(2*(tf - t0)*norm( y - z )); 29 Systems of 1st-order IVPs and Higher-order IVPs Modifying the Dormand-Prince Method The last step is modifying our Dormand-Prince function: – First, we must determine the number of IVPs we are using – We can use the initial condition y0 [N, y0_cols] = size( y0 ); – We need to check that y0,cols = 1, that is, the initial condition must be a column vector – Throw an appropriate exception if it is not a column vector – As before, we initialize y_out = y0; 30 Systems of 1st-order IVPs and Higher-order IVPs Modifying the Dormand-Prince Method The last step is modifying our Dormand-Prince function: – In addition to changes we discussed in Heun’s method, we will need to calculate slopes for each of the N differential equations: K = zeros( N, n_K ); for m = 1:n_K % Assign the output to the m'th column of K end – To approximate the next set of points, we must access the kth column of the previous approximation y = y_out(:, k) + h*K*by; z = y_out(:, k) + h*K*bz; 31 Systems of 1st-order IVPs and Higher-order IVPs Modifying the Dormand-Prince Method The last step is modifying our Dormand-Prince function: – In determining the scaling factor, as before, we use the norm – Finally, when we assign to yout, we must indicate that we are assigning to the (k + 1)st column – For example: >> v = [1 2]'; >> v(:, 2) = [3 4]'; >> v(:, 3) = [5 6]'; >> v(:, 4) = [7 8]' v = 1 3 5 2 4 6 7 8 32 Systems of 1st-order IVPs and Higher-order IVPs 33 Example As an example, using the fox-and-rabbit example: format long [t5a, y5a] = dp45( @f5a, [0, 1], [3/10 1/2]', 0.1, 1e-5 ) t5a = 0 0.100000000000000 0.300000000000000 y5a = 0.300000000000000 0.301027434326078 0.308934964376129 0.500000000000000 0.466212959860404 0.405658801479583 0.700000000000000 1.000000000000000 0.346907366856264 0.309729860800291 0.395416197986561 0.256363047854678 Systems of 1st-order IVPs and Higher-order IVPs Example In this example, the values of K, y, z, and s at the four steps are 0.0042 0.0062 0.0164 0.0181 0.0203 0.0203 0 K 0.3500 0.3451 0.3427 0.3306 0.3285 0.3259 0.3259 t1 = 0.0 h = 0.1 Approximating at t2 = 0.1 0.301027434326078 y 0.466212959860404 0.301027433242441 z 0.466212959694911 s = 4.621361955798724 Note: double the value of h for the next interval... 34 Systems of 1st-order IVPs and Higher-order IVPs Example In this example, the values of K, y, z, and s at the four steps are 0.0203 0.0283 0.0320 0.0509 0.0542 0.0583 0.0583 K 0.3259 0.3164 0.3118 0.2891 0.2852 0.2803 0.2803 t2 = 0.1 h = 0.2 Approximating at t3 = 0.3 0.308934964376129 y 0.405658801479583 0.308934940809368 z 0.405658801343818 s = 2.552250077040883 Note: double the value of h for the next interval... 35 Systems of 1st-order IVPs and Higher-order IVPs Example In this example, the values of K, y, z, and s at the four steps are 0.0583 0.0732 0.0802 0.1170 0.1238 0.1322 0.1320 K 0.2803 0.2631 0.2550 0.2164 0.2098 0.2020 0.2023 t3 = 0.3 h = 0.4 Approximating at t4 = 0.7 0.346907366856264 y 0.309729860800291 0.346907152277278 z 0.309729948827600 s = 1.713629107867625 Note: h is unchanged: 1 ≤ s < 2, but 0.7 + 0.4 > 1, so use h = 1 – 0.7 = 0.3 36 Systems of 1st-order IVPs and Higher-order IVPs Example In this example, the values of K, y, z, and s at the four steps are 0.1856 0.1928 0.1927 0.1320 0.1436 0.1494 0.1799 K 0.2023 0.1920 0.1871 0.1637 0.1597 0.1549 0.1550 t4 = 0.7 h = 0.3 Approximating at t5 = 1.0 0.395416197986561 y 0.256363047854678 0.395416207967321 z 0.256363054795897 s = 3.332843689929424 37 Systems of 1st-order IVPs and Higher-order IVPs Predator-Prey Models In addition, you can try the following: [t5a, y5a] = dp45( @f5a, [0, 6.66], [3/10 1/2]', 0.001, 1e-10 ); size( y5a ) ans = 2 817 plot( y5a(1, :), y5a(2, :) ) 38 Systems of 1st-order IVPs and Higher-order IVPs The Lorenz Equations In addition, you can try the following: [t5b, y5b] = dp45( @f5b, [0, 10], [1 1 1]', 0.001, 1e-5 ); size( y5b ) ans = 3 2318 plot3( y5b(1, :), y5b(2, :), y5b(3, :) ) 39 Systems of 1st-order IVPs and Higher-order IVPs The Lorenz Equations If you’re willing to wait longer: [t5b, y5b] = dp45( @f5b, [0, 100], [1 1 1]', 0.001, 1e-4 ); size( y5b ) ans = 3 29634 plot3( y5b(1, :), ... y5b(2, :), y5b(3, :) ) 40 Systems of 1st-order IVPs and Higher-order IVPs 41 Higher-order ODEs Now, what do we do if we want to approximate a higherorder IVP? – Suppose we have an Nth-order ODE Again, by the implicit function theorem, we can, in general, write any differential equation in the form y N t f t , y t , y 1 t ,..., y N 1 t For this, we need N initial values: y t 0 y0 1 1 y t 0 y0 All are given constants y N 1 t0 y0 N 1 Systems of 1st-order IVPs and Higher-order IVPs Higher-order ODEs To solve this problem, note that we can define w1 t y t 1 y t w2 t w3 t y 2 t w t w t N 2 t N 1 y w t N 1 N y t 42 Systems of 1st-order IVPs and Higher-order IVPs Higher-order ODEs Differentiating this vector, we have: w11 t y 1 t w t 1 2 2 w2 t y t w3 t 1 3 w t w t y t 4 1 w t 3 wN 11 t y N 1 t wN t N N 1 w t y t y t N w1 t y t 1 y t w t 2 w3 t y 2 t w t w t N 2 t N 1 y w t N 1 N y t 43 Systems of 1st-order IVPs and Higher-order IVPs Higher-order ODEs The only missing component is y(n)(t): w11 t y 1 t w t 1 2 2 w2 t y t w3 t 1 3 w t w t y t 4 1 w t 3 wN 11 t y N 1 t wN t N N 1 w t y t y t N 44 Systems of 1st-order IVPs and Higher-order IVPs 45 Higher-order ODEs Recall, however, the original ODE: y N t f t , y t , y 1 t ,..., y N 1 t f t , w1 t , w2 t ,..., wN t We can rewrite the ODE as a function of time and an nN dimensional vector: y t f t , w t w1 t y t 1 y t w t 2 w3 t y 2 t w t w t N 1 w t N N 2 y t N 1 y t Systems of 1st-order IVPs and Higher-order IVPs Higher-order ODEs Thus, we can use the nth-order ODE to create a system of n 1st-order ODEs w2 t w t 3 w4 t 1 w t w t N f t, w t 46 Systems of 1st-order IVPs and Higher-order IVPs 4th-order 47 IVP For example, consider the Euler-Bernoulli beam theory: v 4 z w1 z v z v 1 z w2 z w z w3 z v 2 z 3 w4 z v z User: Lzyvz q z EI Elastic modulus Second moment of area w2 z w z 3 1 w z w4 z q z EI Systems of 1st-order IVPs and Higher-order IVPs 48 Higher-order IVPs The initial values for these n functions are: w1 t0 y t0 y0 1 1 y t0 y0 w2 t0 w3 t0 y 2 t0 y 2 0 w t0 w t N 2 N 2 t0 y0 N 1 0 y N 1 w t N 1 N 0 y t0 y0 y t0 y0 y 1 t0 y01 y N 1 t0 y0 N 1 Systems of 1st-order IVPs and Higher-order IVPs 2nd-order IVP 49 Suppose we are given the IVP (N = 2): y 2 t y 1 t 3 y t t 1 y 0 1.2 y 1 0 1.3 We would therefore define: w1 t w t w t 2 w2 t w t w t 3 w t t 1 1 2 1 1.2 w t0 w 0 1.3 Systems of 1st-order IVPs and Higher-order IVPs 2nd-order 50 IVP In Matlab: w1 t w t w2 t w2 t w t w2 t 3w1 t t 1 1 would be translated as: function [dw] = f5c( t, w ) dw = [w(2) -w(2) - 3*w(1) + t - 1]; end 1.2 w t0 w 0 1.3 [1.2 1.3]' Systems of 1st-order IVPs and Higher-order IVPs 2nd-order IVP Now, we simply use our dp45 function: [t5c, y5c] = dp45( @f5c, [0, 1], [1.2, 1.3]', 0.1, 1e-4 ) t5c = 0 0.1000 0.3000 0.5000 0.7000 0.9000 1.0000 y5c = 1.2000 1.3011 1.3425 1.2068 0.9479 0.6233 0.4535 1.3000 0.7273 -0.2772 -1.0339 -1.5062 -1.6948 -1.6915 The 1st row of y5c are the values y(t) The 2nd row are the values of y(1)(t) 51 Systems of 1st-order IVPs and Higher-order IVPs 2nd-order IVP Taking a look at the results: plot( t5c, y5c(1, :), 'bo-' ); hold on plot( t5c, y5c(2, :), 'ro-' ) 52 Systems of 1st-order IVPs and Higher-order IVPs 2nd-order IVP Solving this in Maple, we have: > dsolve( {(D@@2)(y)(t) = -D(y)(t) - 3*y(t) + t - 1, y(0) = 1.2, D(y)(0) = 1.3}, y(t) ); > CodeGeneration[Matlab]( rhs(%) ); cg0 = 0.161e3 / 0.495e3 * exp(-t / 0.2e1) * sin(sqrt(0.11e2) * t / 0.2e1) * sqrt(0.11e2) + 0.74e2 / 0.45e2 * exp(-t / 0.2e1) * cos(sqrt(0.11e2) * t / 0.2e1) 0.4e1 / 0.9e1 + t / 0.3e1; > plot( rhs(%), t = 0..10 ); 53 Systems of 1st-order IVPs and Higher-order IVPs 2nd-order IVP 54 We can save the actual solution as a Matlab function: function [y] = y5c_soln(t) y = 0.161e3/0.495e3.*exp( -t/0.2e1 ) ... .*sin( sqrt( 0.11e2 ).*t/0.2e1 ).*sqrt( 0.11e2 ) ... + 0.740e2/0.450e2.*exp( -t/0.2e1 ) ... .*cos( sqrt( 0.11e2 ).*t/0.2e1 ) ... - 0.4e1/0.9e1 + t/0.3e1; end Systems of 1st-order IVPs and Higher-order IVPs 2nd-order IVP We can now solve and plot the actual solution: [t5c, y5c] = dp45( @f5c, [0, 10], [1.2, 1.3]', 0.1, 1e-4 ); plot( t5c, y5c_soln( t5c ), 'r' ); hold on plot( t5c, y5c(1, :), 'o' ); abs( y5c(1, end) - y5c_soln( 10 ) ) ans = 1.4711e-006 55 Systems of 1st-order IVPs and Higher-order IVPs 2nd-order 56 IVP How good is this approximation overall? hold off; plot( t5c, y5c(1, :) - y5c_soln( t5c ), 'ro' ); eabs = 0.0001 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit Consider the RLC circuit Applying KCL, we have: 1 t L R d vC t0 Vin C t0 Substituting q , we get 1 Lq Rq q Vin C 57 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit 58 Suppose we are given the IVP (N = 2): Vin R 1 q q q L L CL We would therefore define: w1 t w t w t 2 w2 t w 1 t Vin R 1 w2 t w1 t CL L L q0 w t0 w 0 0 Systems of 1st-order IVPs and Higher-order IVPs 59 RLC Circuit In Matlab: w1 t w t w2 t w2 t w 1 t Vin R 1 w2 t w1 t CL L L q0 w t0 w 0 0 would be translated as: function [dw] = f5d( t, w ) L = 2; % 2 H R = 1; % 1 ohm C = 1/3; % 0.333 F V_in = 1; dw = [w(2) V_in/L - R/L*w(2) - 1/C/L*w(1)]; end Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit Now, we simply use our dp45 function: [t5d, y5d] = dp45( @f5d, [0, 1], [0, 0]', 0.1, 1e-4 ) t5d = 0 0.1000 0.3000 0.7000 1.0000 y5d = 0 0.0025 0.0212 0.1030 0.1886 0 0.0486 0.1362 0.2605 0.3026 The 1st row of y5d are the values q(t) The 2nd row are the values of q(1)(t) = (t) 60 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit Taking a look at the results: plot( t5d, y5d(1, :), 'bo-' ); hold on plot( t5d, y5d(2, :), 'ro-' ) 61 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit Solving this in Maple, we have: > L := 2; R := 1; C := 1/3; V_in := 1; > dsolve({(D@@2)(q)(t) = V_in/L - R/L*D(q)(t) - 1/C/L*q(t), q(0) = 0, D(q)(0) = 0}, q(t) ); > CodeGeneration[Matlab]( rhs(%) ); cg = -exp(-t / 0.4e1) * sin(sqrt(0.23e2) * t / 0.4e1) * sqrt(0.23e2) / 0.69e2 exp(-t / 0.4e1) * cos(sqrt(0.23e2) * t / 0.4e1) / 0.3e1 + 0.1e1 / 0.3e1; > plot( rhs(%), t = 0..10 ); 62 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit 63 We can now solve and plot the actual solution: function [y] = y5d_soln(t) y = -exp( -t/0.4e1 ).*sin( sqrt(0.23e2)*t/0.4e1 )*sqrt( 0.23e2 )/0.69e2 ... - exp( -t/0.4e1 ).*cos( sqrt(0.23e2)*t/0.4e1 )/0.3e1+0.1e1/0.3e1; end Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit We can now solve and plot the actual solution: [t5d, y5d] = dp45( @f5d, [0, 10], [0, 0]', 0.1, 1e-4 ); plot( t5d, y5d_soln( t5d ), 'r' ); hold on; plot( t5d, y5d(1, :), 'o' ); 64 Systems of 1st-order IVPs and Higher-order IVPs 65 RLC Circuit How good is this approximation? plot( t5d, y5d(1, :) - y5d_soln( t5d ), 'ro' ); eabs = 0.0001 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit with a Discontinuous Forcing Function Given a set of initial conditions, we could solve this for any initial conditions, but then again, we can do this analytically, too! What happens if the coefficients are time varying? – What if we shut off the voltage source at time t = 5? 66 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit with a Discontinuous Forcing Function w2 t w 1 t Vin u 5 t R 1 w t w t 2 1 L L CL In Matlab: would be translated as: function [dw] = f5d( t, w ) L = 2; % 2 H R = 1; % 1 ohm C = 1/3; % 0.333 F V_in = 1; dw = [w(2) V_in*(t < 5)/L - R/L*w(2) - 1/C/L*w(1)]; end 67 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit with a Discontinuous Forcing Function Suddenly, it seems that dp45 goes into an infinite loop [t5d, y5d] = dp45( @f5d, [0, 10], [0, 0]', 0.1, 1e-4 ); What could have happened? – The discontinuity in the input voltage means that even the derivative at t = 5 is discontinuous – The Dormand-Prince method assumes the solution to be continuously differentiable—consequently, it keeps dividing and dividing h until t_out(k) + h == t_out(k) 68 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit with a Discontinuous Forcing Function How do we fix this? – After having calculated y and z, if t_out(k) + h/2 == t_out(k) then accept the current value of h regardless of the value of s – Another possibility may be to have a minimum on h relative to the magnitude of tk, for example, if t_out(k) + 0.001*h/2 == t_out(k) 69 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit with a Discontinuous Forcing Function We can now solve and plot the actual solution: [t5d, y5d] = dp45( @f5d, [0, 10], [0, 0]', 0.1, 1e-4 ); plot( t5d, y5d(1, :), 'o' ); 70 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit with a Discontinuous Forcing Function We can zoom in on the discontinuity: [t5d, y5d] = dp45( @f5d, [0, 10], [0, 0]', 0.1, 1e-4 ); plot( t5d(28:113), y5d(1, 28:113), 'o' ); 71 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit with a Discontinuous Forcing Function Plotting the size of h, it is unchanging away from the discontinuity; however, near the discontinuity, it is first halved and then doubled: plot( t5d(20:122), diff( t5d(20:123) ), '.' ) 72 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit with a Discontinuous Forcing Function This is easier to see on a log scale: plot( t5d(20:122), log( diff( t5d(20:123) ) ), '.' ) 73 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit with a Sinusoidal Forcing Function Suppose the voltage source is sinusoidal – The voltage source could be Vin sin(10t) 74 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit with a Sinusoidal Forcing Function w2 t w 1 t Vin sin 10t R 1 w t w t 2 1 L L CL In Matlab: would be translated as: function [dw] = f5d( t, w ) L = 2; % 2 H R = 1; % 1 ohm C = 1/3; % 0.333 F V_in = 1; dw = [w(2) V_in*sin(10*t)/L - R/L*w(2) - 1/C/L*w(1)]; end 75 Systems of 1st-order IVPs and Higher-order IVPs RLC Circuit with a Sinusoidal Forcing Function We can now solve and plot the actual solution: [t5d, y5d] = dp45( @f5d, [0, 20], [0 0]', 0.01, 1e-6 ); plot( t5d, y5d(1, :), '.' ); 76 Systems of 1st-order IVPs and Higher-order IVPs Systems of 2nd-order 77 IVPs Suppose we have a system of 2nd-order IVPs y1 t0 y1,0 t , y t f t, y t , y t , 2 1 y 2 2 t f1 t , y1 t , y 2 1 , yN t , y 1 1 , yN t , yN t y N t f N t , y1 t , y1 t , 2 t 1 1 1 1 N 1 , y N t , y N t 1 1 y11 t0 y1,0 y2 t0 y2,0 y2 t0 y2,0 1 1 y N t0 y N ,0 y N t0 y N ,0 1 1 Systems of 1st-order IVPs and Higher-order IVPs Systems of 2nd-order IVPs We could define a vector-valued function of size 2N: w1 t y1 t y 1 t w2 t 1 w3 t y2 t 1 w t w4 t y2 t w2 N 1 t y N t w t 1 2 N y N t 78 Systems of 1st-order IVPs and Higher-order IVPs Systems of 2nd-order IVPs We could define a vector-valued function of size 2N: w11 t y11 t w t 2 1 2 w2 t y1 t f1 t , w t 1 1 w3 t y2 t w4 t w 1 t w41 t y2 2 t f 2 t , w t 1 1 w2 N 1 t y N t w2 N t 1 2 f t , w t N w2 N t y N t 79 Systems of 1st-order IVPs and Higher-order IVPs Systems of 2nd-order IVPs We could define a vector-valued function of size 2N: y1 2 t t y1 t y11 t y2 t y21 t y2 t t y1 t y1 t y2 t y2 t 2 1 1 function [dw] = f5e( t, w ) dw = [w(2) t + w(1) + w(2) + w(3) + w(4) w(4) t + w(1) - w(2) + w(3) - w(4)]; end 80 Systems of 1st-order IVPs and Higher-order IVPs Systems of 2nd-order IVPs Thus, we have: [t5e, y5e] = dp45( @f5e, [0, 1], [2, 1, 0, -1]', 0.1, 1e-4 ); plot( t5e, y5e(1, :), 'ro-' ); hold on plot( t5e, y5e(3, :), 'bo-' ); 81 Systems of 1st-order IVPs and Higher-order IVPs Summary We have looked at using the Dormand-Prince method for – Solving systems of N coupled IVPs, and – Solving higher-order IVPs • Convert an Nth-order IVP into N 1st-order IVPs • Convert N 2nd-order IVPs into 2N 1st-order IVPs – We considered time-varying systems 82 Systems of 1st-order IVPs and Higher-order IVPs References [1] Glyn James, Modern Engineering Mathematics, 4th Ed., Prentice Hall, 2007, p.778. [2] Glyn James, Advanced Modern Engineering Mathematics, 4th Ed., Prentice Hall, 2011, p.164. [3] J.R. Dormand and P. J. Prince, "A family of embedded Runge-Kutta formulae," J. Comp. Appl. Math., Vol. 6, 1980, pp. 19-26. 83