MATLAB: C0 and x are scalars, c – vector, and p – scalar. If c is [ ], then p = c0. If c is a scalar, then p = c0 + c*x . Else, p =

matlab functionpolynomial

function [p] = poly_val(c0,c,x)
N = length(c);
n=1:1:N;
if (N<=1)
if(isempty(c))
p=c0;
else
p= c0+(c*x);
end
end
if(N>1)
p = c0+(sum(c(n).*(power(x,n))));
end
end

Best Answer

function y = mypolyval(c0,c,x)
if isempty(c)
y = c0;
else
y = c0 + power(x, 1:numel(c))*c(:);
end
The case y = c0 + c*x is already covered by the else. And you can use matrix multiplication of a row and a column vector r*c instead of sum(r.*c').
Related Question