%computes the value of P(x)=a(1)+a(2)*x+...+a(n+1)*x^n at x=r, 
%where n+1=length(a)

a = [3,-4,-1,1]  %coefficients of P(x)
n = length(a)-1; 
r = 2;           
b = a(n+1)         %leading coefficient

for i=n:-1:1     %the -1 here means loop is done in reverse, from 3 to 1
    b = b*r+a(i)
end

disp('value of P is...')
b

