American University of Armenia Intensive Computer Program Matlab LESSON 6 LESSON 7 TOPIC 9. Polinomials. A polynomial of order n is an algebraic expression of type p(x) = c0 + c1x + c2x2 + … + cnx . In order to specify the polynomial, it is necessary and sufficient to specify the set of its coefficients. In Matlab it is natural to keep them in a vector, and namely, in a row vector with ascending or descending order of coefficients. There are predefined functions working with arrays as polynomials. Polynomial predefined function summary: n Function Description conv deconv poly polyder polyval polyvalm roots Multiply polynomials. Divide polynomials. Polynomial with specified roots. Polynomial derivative. Polynomial evaluation. Matrix polynomial evaluation. Find polynomial roots. Our immediate tasks, however, will be to implement our own versions of these functions and add some new. In this section polynomials will be represented by the arrays of their coefficients, where the first element is the constant, the second one is the coefficient of the linear member and the last element is the coefficient of the highest order. For example, p(x) = x2 – 4 is to be represented as [-4 0 1], and p(x) = 7 x5 as [0 0 0 0 0 7]. It is obvious that the length of the array of coefficients exceeds by 1 the order of the polynomial. Task 1. Write a function that, given a polynomial, returns the latter’s value at the specified point. 1. Start a new M-function as follows: function result = polyval(coeffs, x) 2. In order to compute the value of the polynomial, x0 must be raised into powers from 0 to the order of the polynomial. So, the range of the required exponents will be: powers = x .^ (0 : length(coeffs) - 1); 3. The last touch of the brush: result = powers * coeffs(:); ATTENTION: length (X) returns the length of vector X. American University of Armenia Intensive Computer Program Matlab LESSON 6 After raising x0 into powers on element- by-element basis, the scalar product of the produced row with the transposed coeffs is computed. This statement will correctly run only if the argument coeffs is a row-vector, which is usually the case. An alternative and more general statement, working for both row-vector and column vector types of coeffs, sums up elementby-element products: result = sum( powers .* coeffs); 4. Save the function in C:\Matlab\Pol folder under polyval.m name. In order to create a new folder, press “Create New Folder” button in the “Save as” window, type in the name, press “Enter” and click on the “Open” button. Task 2. Write a function that returns the sum of two specified polynomials. 1. Start a new M-function: function result = polysum (this, that) The polynomials to be summed up are formally called this and that respectively. 2. Coefficients in the resulting polynomial are sums of the respective pairs, if the arguments are of equal lengths. Otherwise, extra zeros must be attached at the end of both vectors to get the same length: same_length_sum = [this, zeros(1, length(that))] + [that, zeros(1, length(this))]; 3. In the obtained vector same_length_sum extract the indexes of non zero elements: non_zeros = find(same_length_sum); ATTENTION: I = find(vector) returns the indices of the vector that are non-zero. I = find(A > 100) returns the indices of the elements of A that are greater than 100. [I , J] = find (X) returns the row and column indices of the nonzero entries in the matrix X. 4. Find the maximum index of the last non zero element: last_non_zero = max(non_zeros); 5. And finally, vector same_length_sum is assigned to the result with missing zeros: result = same_length_sum(1 : last_non_zero); American University of Armenia Intensive Computer Program Matlab LESSON 6 Task 3. Write a function that computes the first derivative of a polinomial. 1. Start a new M-function: function result = polyder (coeffs) 2. The derivative of the constant is 0, therefore the first element of the vector coeffs is cut out by assigning it empty array: coeffs(1) = []; 3. Numbers from the range 1 : length(coeffs) now are multiplied element – by – element by the coeffs: result = (1 : length(coeffs)) .* coeffs; Task 4. Create a function that computes the nth derivative of the polinomial. 1. Start a new M-function: function result = polydern (coeffs, n) 2. If the amount of arguments the function was called with is equal to 1 or order of the derivative is equal to 1 we just compute the first derivative calling polyder function. if nargin == 1 | n == 1 polyder(coeffs); 3. Otherwise, polydern function is called recursively to compute the (n - 1)th derivative of the polynomial. else result = polydern(polydern(coeffs), n - 1); end Task 5. Write a function that computes the integral of the polinomial. 1. Start a new M-function: function result = polygral (coeffs, x0, x) 2. If the amount of arguments the function was called with is equal to 1, then an indefinite integral is computed. Recall that the integral from a polynomial is another polynomial of higher by 1 degree with coefficients equal to the old coefficients divided by the powers: c0x + (c2 / 2) x2 + … + (cn / n)xn. if nargin == 1 result = [0 coeffs ./ (1 : length(coeffs))]; American University of Armenia Intensive Computer Program Matlab LESSON 6 Adding 0 to the vector increases its length and, hence the polynomial degree, by 1. Meantime, the rest of the coefficients are divided element-by-element by the range from 1 to the old length. 3. Otherwise, it is assumed that the definite integral is computed. The definite integral is the difference between the indefinite integral taken at specific points x and x0. else result = polyval(polygral(coeffs), x) - polyval(polygral(coeffs), x0); end