14.5. Element-wise ArithmeticΒΆ

Write a function called poly_val that is called like this p = poly_val(c0,c,x), where c0 and x are scalars, and p is a scalar. If c is an empty vector, then p = c0. If c is a scalar, then p = c0 + c*x . Otherwise, p equals the polynomial,

c_0 + c_1x + c_2x^2 + \cdots + c_nx^n

where n is the length of the vector c.

Hints:

  1. The functions isempty, isscalar, iscolumn, and length will tell you everything you need to know about the vector c.
  2. When c is a vector, use an element-wise exponent to determine the vector [x \: x^2 \: x^3\, \cdots\, x^n].
  3. When c is a vector, use the sum function with element-wise multiplication. Matrix multiplication could also be used, but we have not yet covered that.

Here are three example runs:

>> p = poly_val(-17,[],5000)
p =
  -17
>> p = poly_val(3.2,[3,-4,10],2.2)
p =
   96.9200
>> p = poly_val(1,[1;1;1;1],10)
p =
   11111
>> p = poly_val(8,5,4)
p =
    28

MATLAB Grader Element-wise assignment page.