University of Waterloo Faculty of Engineering Department of Electrical and Computer Engineering ECE 204A Pre-Laboratory 11 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 8 February 2016 11.1a Definitions: 1. An ordinary-differential equation (ODE) is any equation involving expressions of a variable x, an unknown function u(x), and at least one of its derivatives: u(1)(x), u(2)(x), u(3)(x), u(4)(x), ... . 2. A 2nd-order ordinary-differential equation (2nd-order ODE) is any equation involving expressions of a variable x, the 2nd-derivative of an unknown function u(2)(x) and possibly the function itself or its first derivatives, u(x) and u(1)(x). 3. A 2nd-order linear ODE (2nd-order LODE) is any equation of the form p(x) u(2)(x) + q(x) u(1)(x) + r(x) u(x) = g(x) where p(x), q(x), r(x), and g(x) are functions of x with p(x) ≠ 0. The function g(x) is said to be the forcing function. 4. A 2nd-order homogenous LODE (2nd-order hom. LODE) is any equation of the form p(x) u(2)(x) + q(x) u(1)(x) + r(x) u(x) = 0 where p(x), q(x), and r(x) are functions of x with p(x) ≠ 0. 5. A 2nd-order LODE with constant coefficients (2nd-order LODE w/cc) is any equation of the form a u(2)(x) + b u(1)(x) + c u(x) = g(x) where a, b, and c are constants with respect to x with a ≠ 0 and g(x) is a function of x. For each of the following, indicate with an x is the most specific description of the given equation: ODE 2nd-order ODE 2nd-order LODE 2nd-order hom. LODE 2nd-order LODE w/cc 2nd-order hom. LODE w/cc u(2)(x) = 0 u(2)(x) + 2u(1)(x) + 5u(x) = 4 u(2)(x) + u(1)(x) u(x) = 0 u(2)(x) + sin(x) 2u(1)(x) + u(x) = cos(x) u(2)(x) + 2u(1)(x + 1) = 0 u(2)(x) + 2x u(x) = 0 u(3)(x) + 3u(2)(x) + u(x) = sin(x) x2 + 4x – 2 = u(x) 11.1bA quick review: Given the 2nd-order LODE u(2)(x) + 4u(1)(x) – 7u(x) = g(x), we can write this as a system of two 1st-order LODEs: 1. Define w1 ≡ u and w2 ≡ u(1). That is, w1(x) = u(x) and w2(x) = u(1)(x) for all values of x. 2. Notice now that if w1 and u are equal, then w1(1)(x) = u(1)(x); however, we also defined w2(x) to be equal to u(1)(x), so we can write w1(1)(x) = w2(x). 3. Notice also that if w2(x) = u(1)(x) then u(2)(x) = w2(1)(x). Therefore, given the differential equation above, we can write it as a system of linear differential equations w1(1)(x) = w2(x) w2(1)(x) + 4w2(x) – 7w1(x) = g(x) or w1(1)(x) = w2(x) w2(1)(x) = –4w2(x) + 7w1(x) + g(x). The 2nd-order ODE above could be implemented as follows: function [ddu] = f( x, u, du ) ddu = -4*du + 7*u + g(x); end 2 None of These where we would, of course, have to know what g(x) is defined as. Instead, rewrite this function so that it takes a vector of two values w and returns a vector of two values, the derivatives of the arguments. function [dw] = f( x, w ) ddu = [... ...]; end 3 11.2a The following code approximate the derivative f(1)(x) by using a formula found by two steps of Richardson extrapolation: function [dy] = diffr2( f, x, h ) dy = (f(x+h) - 40*f(x+h/2) + 256*f(x+h/4) ... - 256*f(x-h/4) + 40*f(x-h/2) - f(x-h))/(90*h); end Suppose the function f11a is a vector-valued function, for example, sin x1 x2 0.2 f11a x cos x2 x3 0.3 e x3 x1 1 which may be implemented using function [y] = f11a( x ) y = [sin( x(1)) - x(2) + 0.2; cos( x(2)) + x(3) - 0.3; exp(-x(3)) - x(1) - 1]; end In this case, root-finding means that we wish to find to find a value of the vector x such 0 that f11a (x) 0 . This is quite common in electrical engineering: suppose you have a 0 non-linear circuit and you apply KCL at three nodes. At each node, the sum of the voltages must be zero and if the circuit elements are non-linear, you may have to solve a system similar to the one shown above. We will use something similar to Newton’s method in one dimension where we solve xk 1 xk f xk f 1 xk . In higher dimensions, however, what is the derivative of an n-dimensional function? The solution appears by rewriting this equation as f 1 xk xk 1 xk f xk . 4 If we write this as f xk xk 1 f xk , then we solve for xk 1 and set 1 xk 1 xk xk 1 . The question is what is the derivative in higher dimensions? It turns out to be a matrix, the Jacobian (written as J(f)(x)) which can be evaluated as f1 x1 f J f x 2 x1 f 3 x1 f1 x2 f 2 x2 f3 x2 f1 x3 f 2 . x3 f3 x3 To do create this matrix, we must calculate the partial derivatives of the function f(x) with respect to each of the three variables. The modification function [dy] = diffr2( f, x, h, n ) dh = zeros( size( x ) ); dh(n) = h; dy = ( f(x+dh) - 40*f(x+dh/2) + 256*f(x+dh/4) ... - f(x-dh) + 40*f(x-dh/2) - 256*f(x-dh/4) )/(90*h); end f1 xn f approximates 2 evaluated at the point x. By evaluating this function at n = 1, 2, and xn f 3 xn 3, we can create three vectors and can create the Jacobian matrix. Once we have this matrix, we can then solve J f xk xk 1 f xk for xk 1 by solving the system of linear equations and then just setting xk 1 xk xk 1 . Implement the function newton_nd which takes the same arguments as newton but where f is a function as described above and x0 is an n-dimensional column vector. Note that the dimension n is different from the maximum number of iterations N. 5 function [x] = newton_nd( f, x0, h, eps_step, eps_abs, N ) % REPLACE THIS WITH YOUR IMPLEMENTATION % get the dimension n of the vector x0 x = x0; for k = 1:N x_old = x; % Create a matrix J of zeros for j = 1:n % Set the jth column of J to the vector of % partials w.r.t. x(j) evaluated at x end % Solve the system described above for the unknown dx x = x + dx; if norm( x - x_old ) < eps_step && norm( f(x) ) < eps_abs return; end end throw( MException( 'MATLAB:non_convergence', ... 'Newton''s method didn''t converge.' ) ); end Test your code by running x = newton_nd( @f11a, [1 1 -1]', 0.01, 1e-10, 1e-10, 10 ) % Copy and paste your output here You have a value which is returned from newton_nd. How can you test the output to determine whether or not you have a reasonable solution? Your answer here. 6 11.2b Find a root of the following system of non-linear equations: e x1 2 x2 x3 1 x1 e0.1x2 3x3 2 x1 x2 e0.3x3 1 by first converting the three equations into expressions where the right-hand side is zero. Then, define a vector-valued function f which returns the function which evaluates the non-zero left-hand side. Enter the function here and make sure that you can find a root with your choice of initial conditions. function [y] = f11b(x) % Your implementation here end x = newton_nd( @f11b, [? ? ?]', 0.01, 1e-10, 1e-10, 10 ) % Your output here 7