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,
where n is the length of the vector c.
Hints:
The functions
isempty,isscalar,iscolumn, andlengthwill tell you everything you need to know about the vectorc.When
cis a vector, use an element-wise exponent to determine the vector \([x \: x^2 \: x^3\, \cdots\, x^n]\).When
cis a vector, use thesumfunction 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