Question: MATLAB (a) Write a function padrv which takes a row vector v, an integer n and inserts zeros at the beginning of v to create
MATLAB
(a) Write a function padrv which takes a row vector v, an integer n and inserts zeros at the beginning of v to create a new vector which has length n. The new vector is returned as the output. If n is smaller than numel(v), then padrv should return the original vector v. The function declaration line should be 1 function a = padrv(v,n) The function padrv can be written rather simply, with only one line of code after the function declaration line. (Hint: remember the stacking properties of empty arrays). Test your implementation as shown below: 1 v = [1 0 3 4 5]; 2 result1a1 = padrv(v,2); 3 result1a2 = padrv(v,9); The result should be that result1a1 equals [1 0 3 4 5], and result1a2 equals [0 0 0 0 1 0 3 4 5]. 1 of 5 Available: Feb 19 Spring 2021 Due: Feb 28 (b) Write a function unpad, which takes a row vector w, removes all zeros at the beginning of the vector and returns the remaining elements of the vector, i.e it drops the zeros at the beginning of the vector. The function declaration line should be 1 function b = unpad(v) The function unpad can be written rather simply, with only one line of code after the function declaration line. (Hint: the function find may be useful). Test your implementation in your main script file as shown below: 1 w = [0 0 0 0 1 2 3 0 4]; 2 result1b = unpad(w); Your result should be that result1b equals the array [1 2 3 0 4]. (c) Write a function polyadd which takes row vectors p1 and p2, interpreted as coefficients of two polynomials and returns the row vector that contains the coefficients of the sum of the two polynomials. The function declaration line of polyadd is 1 function pSum = polyadd(p1,p2) You will need to use the functions padrv and unpad, which you created in parts (a) and (b). Input arguments p1 and p2 are row vectors that respectively contain the coefficients (in decreasing degree) of the two polynomials that are to be added. For example, if the two polynomials that you want to add are P1(x) = x 2 + 3 and P2(x) = x 2 + 2x + 1, the row vectors of their coefficients are respectively p1 = [1 0 3] and p2 = [-1 2 1]. The output argument pSum is a row vector that contains the coefficients of the resulting polynomial addition. The result of adding the two polynomials is 2x+4, which is represented by the row vector of its coefficients pSum = [2 4]. The output pSum must be in the unpadded format. For instance, polyadd([-2,3,1],[2,2,2]) should output [5 3], not [0 5 3]. As an example, test your function with the following code. 1 p1 = [1 0 3]; 2 p2 = [-1 2 1]; 3 result1c1 = polyadd(p1,p2); 4 5 p3 = [1 1 0 3]; 6 p4 = [-1 2 1]; 7 result1c2 = polyadd(p3,p4);

1. (a) Write a function padry which takes a row vector v, an integer n and inserts zeros at the beginning of v to create a new vector which has length n. The new vector is returned as the output. If n is smaller than numel(v), then padrv should return the original vector v. The function declaration line should be function a padrv (v, n) The function padrv can be written rather simply, with only one line of code after the function declaration line. (Hint: remember the stacking properties of empty arrays). Test your implementation as shown below: IV-110 3 4 5 2 resultlal - padev iv, 2); resultla2 - padrv (v.9); The result should be that resultial equals [1 0 3 4 5), and result1a2 equals [0 0 0 010 3 4 5] 1 of 5 Available: Feb 19 Spring 2021 Due: Feb 28 (b) Write a function unpad, which takes a row vector w, removes all zeros at the beginning of the vector and returns the remaining elements of the vector, ie it drops the zeros at the beginning of the vector. The function declaration line should be i function - unpad (0) The function unpad can be written rather simply, with only one line of code after the function declaration line. (Hint: the function find may be useful). Test your implementation in your main script file as shown below: IV - 10000 12 3 0 4); 2 resultib- unpad (w); Your result should be that resultib equals the array [1 2 3 0 4). (e) Write a function polyadd which takes row vectors p1 and p3, interpreted as coefficients of two polynomials and returns the row vector that contains the coefficients of the sum of the two polynomials. The function declaration line of polyadd is 1 function poun - polyadd (p1.p2) You will need to use the functions padry and unpad, which you created in parts (a) and (b). Input arguments p1 and p2 are row vectors that respectively contain the coefficients (in decreasing degree) of the two polynomials that are to be added. For example, if the two polynomials that you want to add are P(x) = x2 +3 and P(1) = -1 + 2+ +1, the row vectors of their coefficients are respectively pi - [1 0 3] and p2 - [-1 2 1]. The output argument pSum is a row vector that contains the coefficients of the resulting polynomial addition. The result of adding the two polynomials is 21+4, which is represented by the row vector of its coefficients pSum - [2 4). The output pSun must be in the unpadded format. For instance, polyadd([-2,3,1),(2,2,2]) should output [5 3), not [0 5 3]. As an example, test your function with the following code. 1 pl -110 31; 2 p2 - 1-1 2112 resultiel - polyaddipl.p2); 5p3 -11 10 311 6 P4--1 2 111 7 result102 - polyaddp3, 4)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
