University of Waterloo Faculty of Engineering Department of Electrical and Computer Engineering ECE 204A Pre-Laboratory 10 Prepared by Surname/Last Name, Legal Given/First Name(s) UW Student ID Number: 2NNNNNNN UW User ID: uwuserid @uwaterloo.ca 2A Electrical/Computer Engineering 9 February 2016 10.1a Instead of having a single differential equation, it is possible to have a system of differential equations such that they are coupled: 1. The rate of change of x(t) depends on t, x(t), y(t) and z(t), 2. The rate of change of y(t) depends on t, x(t), y(t) and z(t), and 3. The rate of change of z(t) depends on t, x(t), y(t) and z(t). Thus, if we start with a different value of, say, x(t), then the rates of change of all three may vary, as well. Formally, we say that f1 t , y t y1 t def y t y2 t f t , y t f 2 t , y t . y t 3 f3 t , y t 1 def y1 t where each f k t , y t f k t , y2 t ; that is, each function fk depends potentially on y t 3 the values of t, y1(t), y2(t) and y3(t). Thus, f should take two arguments as inputs: the first is a scalar time while the second is a vector of n values. The example above shows the case where n = 3. In a simpler case where n = 2, the web site describes how a predator-prey model y t y1 t 1 2 y2 t f10 a t , 1 y2 t y t 1 y t 2 1 This would be implemented as the function function [dy] = f10a( t, y ) dy = [y(1)*(1 - 2*y(2)), -y(2)*(1 - y(1))]'; end If you cannot automatically see how one converts to the other, consider the following alternative: function [dy] = f10a( t, y ) y1 = y(1); y2 = y(2); dy = [y1*(1 - 2*y2), -y2*(1 - y1)]'; end Convert the following two systems of differential equations into function f10b and f10c, respectively. y t ty t 2 y2 t f10b t , 1 1 y2 t ty2 t 2 y1 t 10 y t y t 2 1 y1 t f10 c t , y2 t y1 t 28 y3 t y2 t . y t 3 y t y t 8 y t 1 2 3 3 function [dy] = f10b( t, y ) % Your implementation here end function [dy] = f10c( t, y ) % Your implementation here end 2 It would probably be a good idea to check your answers, for example, you could 5 substitute t = 3 and y 2 and calculate f10b((t, y2) and you should get the appropriate 7 output: the column vector containing the two values when you make the appropriate 5 substitutions. Similarly, you could substitute t = 3 and y 3 7 and calculate f10c(t, y3). 11 10.1b Recall that a second-order differential equation is linear if it can be written in the form a2 t y 2 t a1 t y1 t a0 t y t g (t ) where a0(t), a1(t), a2(t) , and g(t) are all functions are t. A system of first-order differential equations is said to be linear if each of the functions fk t , y t ak ,1 t y1 t ak ,2 t y2 t ak ,n t yn t gk (t ) . where all ai , j t are functions of t. Of the functions f10a, f10b, and f10c, which are linear? Recall that if y1(t) and y2(t) are solutions to a system of linear ordinary differential equations, then ay1(t) + by2(t) is also a solution to that system of linear ordinary differential equations. Your answer here. 10.1c A system of first-order differential equations is said to be time independent if each of the functions fk t , y t does not explicitly depend on t; that is, each f k uses only the values of y1(t), y2(t), ..., yn(t). Of the functions f10a, f10b, and f10c, which are time independent? Your answer here. Recall that if y1(t) and y2(t) are solutions to a time independent initial value-problem with initial conditions y(t1) = y(t2) but where t1 and t2 are different, then the solutions look identical, only that one is a shifted version of the other. 10.1d A system of n differential equations requires n initial conditions. If the initial conditions of a system with two differential equations are y1(0) = 0.3 and y2(0) = 0.5, what would the corresponding t0 and initial-condition vector be? Your answer here. 3 10.1e The following starts with a vector and gradually modifies it. Explain what is happening here. v = [1 v(:,2) v(:,3) v(:,4) 2 = = = 3]' [ 4 5 6]' [ 7 8 9]' [10 11 12]' Enter your answer here. 10.1f In one or two sentences, what does the following code do? v = [1 2 3]' for k = 1:10 v(:, k + 1) = v(:, k) + [k 2*k 3*k]'; end Enter your answer here. 4 10.1f Modify dp45 so that it accepts a column vector as an initial condition. This requires two changes: 1. Any references to either y_out(k) or y_out(k + 1) must be changed to y_out(:, k) and y_out(:, k + 1), respectively, and 2. Rather than using the absolute-value function to compare ytmp and ztmp, use the norm between a difference of vectors: replace abs( y_tmp – z_tmp ) with norm( y_tmp – z_tmp, Inf ). 10.1g Next, execute the following to give plots similar to Figure 1 and Figure 2 from the laboratory web site. hold off [ts, pts] = dp45( @f10a, [0, 6], [0.5 0.3]', 0.1, 1e-8 ); plot( pts(2,:), pts(1,:) ) % rabbits on the vertical axis, foxes on the horizontal plot( ts, pts(1,:) ) hold on plot( ts, pts(2,:) ) % plot population density of rabbits over time % plot population density of foxes over time 10.1h Next, execute the following should get an output similar to Figure 5 on the Laboratory web site: [ts, pts] = dp45( @f10c, [0, 100], [-5 0 28]', 0.1, 1e-5 ); size( pts ) ans = 3 53166 plot3( pts(1,:), pts(2,:), pts(3,:) ) If this is taking too long or if Matlab crashes, you can use abs = 0.001, in which case, the size of the resulting output vector will be 3 × 17314. 5 10.2a You will implement Newton’s method for a real-valued function of a real variable. The signature for this function is function [r] = newton( g, x0, h, eps_step, eps_abs, N ) The functioning of this algorithm will be as follows: 1. Set r = x0 2. Start a loop which iterates k from 0 to N times. That loop will: a. Set r_old = r g r b. Calculate r r 1 where the derivative is approximated by a call to g r the function richardson22( @Dc, g, r, h/2^k, N, eps_step ) c. If r rold step and g r abs , return. 3. If the loop ran N times without returning, throw the exception throw( MException( 'MATLAB:non_convergence', ... 'Newton''s method did not converge.' ) ); The functioning of this algorithm will be as follows: newton( @cos, 1.5, 0.01, 1e-3, 1e-3, 10 ) ans = 1.570796326794341 newton( @cos, 1.5, 0.01, 1e-5, 1e-5, 10 ) ans = 1.570796326794897 pi/2 ans = 1.570796326794897 function [r] = newton( g, x0, h, eps_step, eps_abs, N ) % Enter your implementation here end 6