Using Matlab, write a function called poly_val that is called like this p = poly
ID: 3839508 • Letter: U
Question
Using Matlab, write a function called poly_val that is called like this p = poly_val(c0,c,x), where c0 and x are scalars, c is a vector, and p is a scalar. If c is an empty matrix, then p = c0. If c is a scalar, then p = c0 + c*x . Otherwise, p equals the polynomial,
0+1 +2 ++ ,
where N is the length of the vector c. Hint: you may want to use the .^ operator. Here are three example runs:
>> format long
>> p = poly_val(-17,[],5000)
p = -17
>> p = poly_val(3.2,[3,-4,10],2.2)
p = 96.920000000000030
>> p = poly_val(1,[1,1,1,1],10)
p = 11111
Explanation / Answer
note:i did not understand how poly_val(3.2,[3,-4,10],2.2) returned 96.92...If you can elaborate in the comment,i can change the code accordingly
function p=poly_val(c0,c,x)
if isempty(c)==1
p=c0;
else
if numel(c)==1
p=c0+c.*x;
else
power=0:numel(c)-1;
p=sum((x.^power).*c);
end
end