Download Operations with Matrices and Vectors Using MATLAB | PGE 310 and more Study Guides, Projects, Research Engineering in PDF only on Docsity! SOLUTION SET Computer Project No. 7 March 4, 2004 Due on March 9, 2004, 9:30 AM PGE310 (Unique No. 17235) Spring Semester 2004 Formulation and Solution of Geosystems Engineering Problems Instructor: Carlos T. Verdín OPERATIONS WITH MATRICES AND VECTORS USING MATLAB DESCRIPTION: This computer project is intended to introduce students to the programming of matrix and vector operations. YOUR REPORT SHOULD BE CLEAN, NEAT, AND WELL ORGANIZED; IT SHOULD INCLUDE CLEAR AND COMPLETE TECHNICAL DESCRIPTIONS WHEREVER NECESSARY. ALL FIGURES AND TABLES SHOULD BE LABELED AND PROPERLY ANNOTATED WITH A CAPTION. LOOSE FIGURES ARE NOT SELF-EXPLANATORY. POINTS WILL BE DEDUCTED FROM PROJECTS THAT DOES NOT ADHERE TO THESE PRESENTATION RULES. (1) Write a function add_310 to add two matrices A urur and B urur and return a matrix C urur . The function definition should be: function C = add_310(A,B) The function should also detect illegal combinations of input matrices and it should print an error message if the input matrices do not have the same dimensions. % Function add_310 % ------------------------- function C=add_310(A,B) [n, m]=size(A); [p, q]=size(B); if n~=p | m~=q display ('Matrix dimensions are not same re-enter matrix') else for i=1:n for j=1:q C(i,j)=A(i,j)+ B(i,j); end end end (2) Write a function mult_310 to multiply two matrices A urur and B urur and return a matrix C urur . The function definition should be: function C = mult_310(A,B) The function should also detect illegal combinations of input matrices and it should print an error message. The function should correctly handle the following cases: (a) is and is p A n m B q× × ur urur ur (b) Matrix – vector multiplication from the right hand (i.e. Ax ururr where x r is a column vector) (c) Vector – matrix multiplication from the left hand (i.e. tx A ururr where x r is a column vector) Do not use the built-in capability of Matlab to perform these operations. Rather write out the operations using for loops. % Function mult_310 % -------------------------- function C=mult_310(A,B) %C=A*B [n, m]=size(A); [p, q]=size(B); if m~=p disp ('Problems with Matrix dimensions ') else for i=1:n for j=1:q sum=0; 18000
16000 -
Plot of the first column of D1*D2
— Prod 310 D1*D2
—— Prod Matlab D1 *D2
x10" Plot of of D1*x
— Prod 310 D1*x
—— Prod Matlab Di*x
One can see that mult_310 function and Matlab’s built in function give the same results for each product. (4) Load the three matrices A urur , B urur and C urur and the vectors v r and w ur respectively from the files mat_A.txt, mat_B.txt, mat_C.txt, vec_v.txt, vec_w.txt. % (4) % --- A=load('mat_A.txt'); B=load('mat_B.txt'); C=load('mat_C.txt'); v=load('vec_v.txt'); w=load('vec_w.txt'); (5) Test your function add_310: (a) A B+ ur urur ur add_A_B=add_310(A,B) 2 8 3 4 -2 0 2 1 -6 -3 -2 -6 -3 3 2 2 4 2 -8 1 -1 2 4 6 4 2 -8 2 2 -4 0 4 0 -3 2 (b) A C+ ur urur ur add_A_C=add_310(A,C) ans = Matrices dimensions do not allow this addition (6) Test your function mult_310: (a) vw rur prod_v_w=mult_310(v,w) Matrices dimensions do not allow this product (b) tv w urur prod_vt_w=mult_310(v',w) Matrices dimensions do not allow this product (c) tvv urr prod_v_vt = 4 2 6 -4 0 2 -2 2 1 3 -2 0 1 -1 6 3 9 -6 0 3 -3 -4 -2 -6 4 0 -2 2 0 0 0 0 0 0 0 2 1 3 -2 0 1 -1 -2 -1 -3 2 0 -1 1 (d) tAv ururur prod_A_vt=mult_310(A,v')