I need you to follow the pdf assignment and use the files attached. The pdf instructions is called EZ_homework9.pdf. You can use the template .m file to fill out the homework. This assignment must be checked at the end for 100% with the autograder. The autogravder is the .p file. I think it is helpful to check with the autogravder as you go.
E7 Fall 2019, UC Berkeley Homework Assignment 9 Induction & Linear Algebra The purpose of this lab is to introduce you to induction and linear algebra in MATLAB. Some commands that may be helpful are: rank, spdiags, ones, reshape, pinv. Note: Use the templates provided (HW9 Template.m) in the assignment download to com- plete this assignment. The template has a very specific format to enable auto-grading, and if the format is altered your assignment may not be properly graded (this will be apparent to you). Please also notice that variables in BOLD FONT will be checked to calculate your grade. In addition, the template file already reflects much of the content of the assignment. The due time is 11:59am (noon) November 15, 2019. NO LATE HOMEWORK WILL BE ACCEPTED. Please upload the following files through the bCourses website: • HW9 solution.m • indplot.m, indplot2.m, linearequationtest.m, rotate2d.m, rotate3d.m, inverserot.m, pet weights.m, avgArray.m, gaussJ.m • SummationPlot.png, FunctionPlot.png, 2DSquareRotated.png, 3DPlaneRotated.png, InverseRotation.png • HW9 solution.pdf (created using the publish command) Directions to upload files can be found here. 1. Induction. You can read more about Induction here. (a) Given the series S1 = N∑ n=1 (2n− 1), S2 = N∑ n=1 n2, create the function indplot function [S1,S2] = indplot(N) The function indplot takes an integer N as its input and calculates the summation S1 and S2 according to the above equations. It also generates one plot with two curves showing how each series varies as a function of the integer n, when n ranges from 1 to N. Save the plot as a .png with the print command, with the filename ’SummationPlot’. 1 https://guides.instructure.com/m/4212/l/54353-how-do-i-upload-a-file-to-my-assignment-submission https://en.wikipedia.org/wiki/Mathematical_induction E7 Fall 2019, UC Berkeley Note:You should use the plot to better understand the relationship between the expression on the left and right hand side of the equation. (b) Create a function indplot2.m which plots the following expressions for the same values of N as in part (a) on a new figure: f1(N) = N 2, f2(N) = N(N+1)(2N+1)6 . With the following function header, and f1 is the array of values of f1(N) above, and f2 is the array of values of f2(N) above. function [f1,f2] = indplot2(N) Evaluate indplot and indplot2 at N = 50 in your main script to view the plots. From a comparison of the plots, you should confirm the following identities for any N = 1, 2, ... N∑ n=1 (2n− 1) = N 2, N∑ n=1 n2 = N(N + 1)(2N + 1) 6 (c) Note: This following part is good practice, but will not be graded for this home- work assignment. Now that you have used MATLAB to prove the mathematical induction, try proving these identities by hand for N = 1, 2, ... 2. Consider the system Ax = b (1) Write a function with the following function declaration line func t i on message = l i n e a r e q u a t i o n t e s t (A, b) that will take A and b as inputs and output one of the following messages (stored in your main script as the variable message) depending on the existence and uniqueness of the solution of the system. There exists a solution to the system if the rank of the matrix A is equal to the rank of a the matrix A augmented with the right hand side solution vector b (you can read more about the rank of matrices here). The solution to the system is unique if the number of variables in the system is equal to the rank of the matrix A. – ’Solution exists and is unique.’ – ’Solution exists but is not unique.’ – ’Solution does not exist.’ A couple example systems are listed below. 2 https://www.mathworks.com/help/matlab/ref/rank.html E7 Fall 2019, UC Berkeley (a) x1 + 3x2 + 2x3 = 1 2x1 + x2 + x3 = 0 −8x1 + x2 − x3 = 1 Should return message = ’Solution does not exist.’ (b) x1 + 3x2 + 2x3 = 1 2x1 + x2 + x3 = 0 x1 + 8x2 + 10x3 = 1 Should return message = ’Solution exists and is unique.’ (c) x1 + 3x2 + 2x3 = 4 2x1 + x2 + x3 = 3 −8x1 + x2 − x3 = −7 Should return message = ’Solution exists but is not unique.’ Note: If a linear system has a solution that exists but is not unique, it cannot be solved with the backslash operation. Instead you must use pinv which returns the pseudoinverse of a matrix in the case where the inverse of the matrix A does not exist. You can read more about the pseudo-inverse here. 3. One useful application of linear algebra in engineering and the sciences is the rotation of shapes, images, objects, etc.. You can find a lot of helpful information for this problem here. (a) In this problem we will consider a rectangle with the following Cartesian coordi- nates, (x,y), for its vertices: (1,1) (4,1) (4,4) & (1,4). Store the coordinates in a 2× 4 matrix C, where the first row is the x coordinates and the second row is the y coordinates. Then write a function with the function declaration line: function [D,R] = rotate2d(C,theta) where theta is the rotation angle (in degrees), and the system of linear equations is: x′ = x cos θ − y sin θ y′ = x sin θ + y cos θ R is the ’rotation matrix’ which is described by the system above. D is the new (x,y) coordinates, denoted in the equations as x′ and y′. The system can be described in the matrix form as D=R*C. Rotate the rectangle 45 degrees. Plot the original shape and the rotated shape, with the command fill. Label your axes and save the plot as a .png with the print command, with the filename ’2DSquareRotated’. (b) Say the original rectangle given in the problem is now a plane in 3 dimensions, with coordinates in (x,y,z). The coordinates are (1,1,0), (4,1,0), (4,4,1), and (1,4,1). Store the coordinates in a 3 × 4 matrix E where each row corresponds to the x, 3 https://www.mathworks.com/help/matlab/ref/pinv.html http://mathworld.wolfram.com/RotationMatrix.html E7 Fall 2019, UC Berkeley y, and z coordinates respectively (like in part a). Then write a function with the declaration line: function [F,Rx,Ry,Rz] = rotate3d(E,alpha,beta,gamma) where alpha is the x-axis rotation angle, beta is the y-axis rotation angle, gamma is the z-axis rotation angle, and the system of linear equations for each direction are given as follows. The angles should be in degrees. For Rotation about X: x′ = x y′ = y cosα− z sinα z′ = y sinα + z cosα For Rotation about Y: x′ = x cos β + z sin β y′ = y z′ = −x sin β + z cos β For Rotation about Z: x′ = x cos γ − y sin γ y′ = x sin γ + y cos γ z′ = z Rx, Ry, and Rz are the ’rotation matrices’ described by the systems above. F is the new (x,y,z) coordinates, denoted in the equations as x′, y′, and z′. Rotate this plane, first 30 degrees about the Z axis, second 60 degrees about the Y axis, and finally 45 degrees about the X axis. Plot the original shape, and the rotated shape using the command fill3. Label your axes and save the plot as a .png with the print command, with the filename ’3DPlaneRotated’. (c) Say instead you are the shape in it’s final position and you want to find it’s original position. Using the relations above, and a square with (x,y) coordinates: (1,0), (0,1), (-1,0), and (0,-1). Store the coordinates in a 2 × 4 matrix XYnew, where the first row is the x coordinates and the second row is the y coordinates. Then write a function with the function declaration line: function [XYold] = inverserot(XYnew,theta) Find what this squares original position, stored in the variable XYold, was if it had been rotated 45 degrees(the angle should be stored in the variable theta). Plot the original shape and the rotated shape, with the command fill. Label your axes and save the plot as a .png with the print command, with the filename ’InverseRotation’. 4 E7 Fall 2019, UC Berkeley 4. Suppose the figure below depicts a network of one-way roads, with arrows indicating the direction of traffic flow. The number of vehicles entering and leaving the network (per hour) at nodes A, B, C and D are shown on the figure and x1, x2, x3 and x4 denote the number of vehicles (per hour) passing through the branches AD, DC, CB and AB respectively. Suppose the system is in equilibrium, that is, no new vehicles appear within the network and none are lost. Thus, at each node, the number of vehicles coming towards the node is equal to the number of vehicles moving away from the node. Figure 1: Problem 4 (a) Write down the equations governing the above system. Read the paragraph ex- plaining the system carefully in order to write the equations. (b) Build a linear system Ax = b by transforming the equations you wrote down in part a. Use the variable SYSA for 4×4 matrix A, and the variable veca for vector b (4 × 1 that represents the amount of cars entering and leaving the network per hour). Each row of SYSA should correspond to the equations at each node A, B, C and D. Each column of SYSA should correspond to x1, x2, x3 and x4. (c) Use your function from problem 2 to determine the existence and uniqueness of the solution of the system, save the message in the variable solution4a. Then solve for x if possible, save the result in variable vpha. If the solution exists, but is not unique remember to use the function pinv in order to find a solution to the problem. Also, if the solution does not exist return vpha as an empty array [ ]. 5 E7 Fall 2019, UC Berkeley (d) Suppose the branch AB is closed for maintenance, so you know x4 = 0. Create a new linear system of equations by adding an additional equation to the linear system you created in part a, use the variable SYSB for the matrix A and vecb for the vector b. Determine the existence and uniqueness of the solution of the new system, using your function from problem 2