0
votes

I have a code written in Scilab:

function v=myhorner2(a,x)
    N=length(a)
    v=a(1)
    for i=2:N do
        v=v*x+a(i))
    end
endfunction

which is working well. Now I need to do the recursion with the same code, but it seems that it's not working - where's the mistake?

function myhorner2(a,x)
    if i=2:N then
         myhorner2(a,x)
         disp (i=2:i+1)
         myhorner2(a+1,x)
    else 
        disp ([v=a])
    end
endfunction

I'm a beginner. Thanks for your feedback

1
what these functions are supposed to do? can you explain, please? - Foad

1 Answers

1
votes

From your original sequential code I presume that coefficient are stored in vector a by decrasing order. Hence the recursive version of Horner algorithm should be

function v = rechorn(a,x)
    N = length(a)
    if N == 1 then
        v = a;
    else
        v = x*rechorn(a(1:N-1),x)+a(N)
    end
endfunction