University of Waterloo Faculty of Engineering Department of Electrical and Computer Engineering ECE 204A Pre-Laboratory 3 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 14 March 2016 Use format short (the default) for Section 3.1 3.1.1a Create a 4 × 4 matrix of all ones and a 4 × 4 identity matrix. Copy and paste your Matlab commands and the output here. 3.1.1b Efficiently create and assign to M the diagonal matrix 5.4 0 3.9 0 M 0 0 0 0 0 0 0 0 0 0 3.5 0 0 . 0 8.2 0 0 0 1 0 0 3.1.1c Create a 5 × 5 matrix of pseudo-random numbers taken from a standard normal distribution and add one to each entry of the diagonal. Copy and paste your Matlab commands and the output here. 3.1.2a Suppressing the output, write efficient Matlab code that generates the matrix 1 0 V 8 64 1 0 1 0 4 16 2 4 1 1 1 1 Copy and paste your Matlab commands and the output here. 3.1.3.1a Find the transpose of the matrix >> M1 = [1 2 3; 4 5 6; 3 8 9]; and assign it to the variable M2. 3.1.3.2a Add the transpose of M1 = [1 2 3; 4 5 6; 3 8 9] onto M1. Copy and paste your Matlab commands and the output here. 3.1.3.2b Subtract the transpose of M1 = [1 2 3; 4 5 6; 3 8 9] from M1. Copy and paste your Matlab commands and the output here. 3.1.3.3a Divide each entry of the matrix M2 = [3.2 -4.5; 7.6 8.1] by the maximum entry. Copy and paste your Matlab commands and the output here. 3.1.3.4a Subtract the smallest entry of the matrix M3 = [7.4 5.9; 6.2 8.1] from each entry in the matrix. Copy and paste your Matlab commands and the output here. 3.1.3.5a Given the matrix M4 = [6 2 1; 3 7 -2; 0 -3 9], calculate M4M4T. Copy and paste your Matlab commands and the output here. 3.1.3.5b Given the matrices M5 = [3 -2 4; 2 -1 4] and M6 = [1 3; 1 5; 1 9], calculate the matrix-matrix products M5M6 and M6M5. Copy and paste your Matlab commands and the output here. 2 3.1.3.6a Given M7 = [0.5 0.2; 0.4 0.7], show that M72 and M7M7 return the same matrix by calculating both in Matlab. Copy and paste your Matlab commands and the output here. 3.1.3.6b Given M8 = [0.9 0.2; 0.4 0.6]; M9 = [0.8 0.2; 0.4 0.6]; M10 = [0.7 0.2; 0.4 0.6]; calculate M8200, M9200, and M10200. Two of the matrices are printed in the form of 1.0en * ... meaning that each entry in the matrix is multiplied by that 10n. What are those two powers? n1 and n2 3.1.4a Create a 3 3 matrix of pseudo-random numbers uniformly distributed on the interval (1, 3). Copy and paste your Matlab commands and the output here. 3.1.5.1a Consider the Matrix >> M11 = [1 1 1 1 1 1 1 1; -2 -1 0 1 2 3 4 5; 4 1 0 1 4 9 16 25]; What is entry in the 3rd row and 7th column? Copy and paste your Matlab commands and the output here. 3.1.5.2a Consider the Matrix >> M12 = [1 1 1 1 1 1 1 1; -2 -1 0 1 2 3 4 5; 4 1 0 1 4 9 16 25]; Extract the 3rd row and 7th column and assign these to v1 and v2, respectively? Copy and paste your Matlab commands and the output here. 3 3.1.6.1-2a Assuming that the matrix is of the appropriate size, describe in one sentence what the following row operations perform: >> M(2, :) = 3.2*M(2,:); Your description here. >> M(3, :) = M(3,:) + 0.527*M(1,:); Your description here. 3.1.7.1a Given the matrix M13 = [1 2 3; 4 5 6; 7 8 9], calculate sin( x) of each entry and assign that to the variable ASM13. Copy and paste your Matlab commands and the output here. 3.1.7.2a Given the matrix ASM13 from the previous question, find the sum of each column. Copy and paste your Matlab commands and the output here. 3.1.7.3a Using the matrix ASM13, calculate the trace. Copy and paste your Matlab commands and the output here. 3.1.8a Solve the system of linear equations described by solving Mx = b 5 2 0 1 2 7 1 1 1 3 10 2 1 13 2 9 x 1 3 9 30 >> M14 = [5 2 -1 1; 2 7 1 -2; 0 1 10 3; -1 3 2 9]; >> b = [13 9 1 30]'; for the vector x by calculating x = M14 \ b. Show that you have the correct answer with >> M14*x % this should be the vector b Copy and paste your Matlab commands and the output here. 4 3.2.1a In Matlab, select File→New→Function and cut-and-paste the following function into that file: function [dy] = f3a( t, y ) % f(t,y) returns the slope y' = f3a(t,y) of the IVP. % The slope of the solution to the differential equation y' = f3a(t,y) % is given by the function f(t,y) which is passed the coordinates % of the point (t,y) at which we are attempting to find the slope. dy = y^2*t - t^2 - 1; return; end Save the file with the name f3a.m (the name of the file must be the name of the function, in this case f3a, followed by the filename extension .m). Calculate the following: >> >> >> >> f3a(0,0) f3a(0,1) f3a(1,0) f3a(1,1) Copy and paste your Matlab commands and the output here. What happens if you attempt to calculate: >> f3a([0,0,0]', [0,1,2]') ? Your answer here. 3.2.1b Given the differential equation y f3b (t , y) t y , we want to turn this into a Matlab function. To do so, create a new function and name it f3b.m with the contents: function [dy] = f3b( t, y ) dy = t + y; return; end Calculate f3b(1,1) and f3b(2,5). Copy and paste your Matlab commands and the output here. 5 3.2.2a Recall that if we have a Taylor series of a function around a point, we may approximate the function around that point. For example, the Taylor 1st-order Taylor series approximation of the exponential function et around the point t = 0 is 1 t while the same approximation around the point t = 1 is e e t 1 . For example, we may approximate e0.01 using the first formula by calculating >> a1 = 1 + 0.01 a1 = 1.010000000000000 >> exp(0.01) ans = 1.010050167084168 >> abs(exp(0.01) - a1)/exp(0.01) ans = 4.966791334014977e-005 % Calculate the relative error Similarly, we may approximate e1.01 using the second formula by calculating >> a2 = exp(1) + exp(1)*(1.01 - 1) a2 = 2.745464646743636 >> exp(1.01) ans = 2.745601015016916 >> abs(exp(1.01) - a2)/exp(1.01) ans = 4.966791334005586e-005 % Calculate the relative error Either of these are good approximations! Using these approximations, approximate e0.1 and e1.1 using these two formulas, respectively, and find the relative error of these two approximations. Copy and paste your Matlab commands and the output here. 6 3.2.2b Recall that a differential equation has many solutions. For example, given the differential equation y t t y t , any function of the form y : t cet t 1 where c is an arbitrary real number, satisfies this differential equation. You can demonstrate this to yourself simply by calculating the derivative: d d y t et t 1 et 1 et t 1 t y t t dt dt and d d y t 3et t 1 3et 1 3et t 1 t y t t . dt dt More of these solutions are demonstrated in Figure 1. Figure 1. Many solutions to the differential equation y t t y t with the t t solutions e t 1 and 3e t 1 highlighted in thick gold and plum, respectively. Summarizing this in a paragraph. 7 Now, consider Figure 2 which focuses on the region [0, 1] × [0, 1]. The circle in gold is the point (0.5, e0.5 – 0.5 – 1); that is, one point on the solution y t et t 1 . You will note that the slope at this point is approximately 0.65 which is what we expect if we calculate y t t at the point (0.5, e0.5 – 0.5 – 1). Figure 2. A zoom on the region [0, 1] × [0, 1] with one point on the solution y t et t 1 highlighted. Pick any other point on any other curve and you will see that the slope at that point equals the value of the function y(t) plus the time t. For example, at (0, 0.4), the slope of the red function at that point is 0 + 0.4 = 0.4 while the slope of the green function passing close to (1, 0.4) appears to have a slope at t = 1 that is very close to 1.4. 8 3.2.2c For this question, use format long. Next, recall that the general form of a 1storder Taylor series approximation is y t0 y t0 t t0 . If we have an initial-value problem y t f t, y t y t0 y0 , we may rewrite the Taylor series approximation as: y0 f t0 , y0 t t0 . For example, given the initial-value problem y t y t t y 2 1 , the Taylor series approximation around the point (2, –1) is 1 2 (1) t 2 . The point (2, –1) is shown in Figure 3. Figure 3. The point (2, –1) and 1st-order Taylor series approximation passing through it. Therefore, an approximation of the solution at time t = 2 + h is 1 2 (1) 2 h 2 1 h . 9 For example, if t = 2.01 = 2 + 0.01, the approximation is –1 + 0.01 = –0.99. The actual solution (to nine decimal digits) to the differential equation that satisfies the initial condition y(2) = –1 is the function y t e22 et t 1 which has the value at t = 2.01 of –0.989899666. Therefore, –0.99 is a reasonable approximation of this value. What is the relative error of our approximation to the exact value? Copy and paste your Matlab commands and the output here. 3.2.2d Given the approximation (2.01, –0.99), we may rephrase this as a new initial-value problem and repeat this operation: y t y t t y 2.01 0.99 . We may therefore use this to approximate y(2.02) (the step size is h = 0.01) by approximating –0.99 + 0.01(2.01 + (–0.99)) = –0.9798. That is, we used (2, –1) to approximate (2.01, –0.99) and then we used (2.01, –0.99) to approximate (2.02, –0.9798). Again, the actual value (to nine decimal digits) of the function y t e22 et t 1 at time t = 2.02 is –0.979597320. What is the relative error of our approximation to the exact value? Copy and paste your Matlab commands and the output here. 3.2.2e We could repeat this to approximate the solution at 2.03, then 2.04, 2.05, etc. After one hundred such iterations, we could find an approximation to the solution at time t = 3.00. In this case, we used a step size of h = 0.01. We could do the same thing using a step size of h = 0.1. In this case, we would have to approximate the solutions at 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9 and 3.0, or ten times. Using your intuition, will using such a larger step size make the approximations better or worse? Reread Question 3.2.2a for help. Better/Worse 10 3.2.3a This question will help you implement the euler function. First, create a new function and ensure the first line is [t_out, y_out] = euler( f, t_int, y0, n ) First, assign t0 and tf as directed in Section 3.2.3. Next, use the linspace function to create a row vector of n points and to assign this to the variable t_out. Next, define the variable h as directed in Section 3.2.3 on the web site. Create a row vector of n zeros and assign it to the variable y_out. Assign y_out(1) the parameter value y0. We now need a for loop which has a variable k that goes from 1 to n: for k = 1:(n - 1) % body end We will replace the body of the for loop with the Matlab equivalent of yk 1 yk hf tk , yk . This is given directly in Section 3.2.3. The last line before the closing end is return; Make sure that all Matlab commands (except those starting with for or end) have a semicolon after them. Save this file as euler.m . Note that what we are doing is, given (ts(1), ys(1)), we find the value of ys(2). Then, given (ts(2), ys(2)), we find ys(3), and so on finding ys(4), ys(5), ... . The following set of commands should produce the plot similar to the blurred image shown in Figure 4 where the t-axis is [2, 3] and the y-axis is [–1, 1.5]. Replace the plot with yours saved as a png. [ts, ys] = euler( @f3b, [2, 3], -1, 11 ); % use 11 points: 2, 2.1, 2.2, ... plot( ts, ys, 'o' ) hold on [ts, ys] = euler( @f3b, [2, 3], -1, 101 ); % use 101 points: 2, 2.01, 2.02, ... plot( ts, ys, 'xr' ) plot( ts, 2/exp(2)*exp(ts) - ts - 1, 'k' ) % plot the actual solution 11 Figure 4. The result of executing the given Matlab commands. In general, we will try to provide blurred images so that you can trust that your images are close to the expected images. You will need the euler.m function during the laboratory. Be sure to bring it. 12