Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8403647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:23:59+00:00 2026-06-09T22:23:59+00:00

I am having some trouble translating a pseudocode for Horner’s algorithm into a proper

  • 0

I am having some trouble translating a pseudocode for Horner’s algorithm into a proper code in MatLab. I think my confusion stems from the fact that the code assumes that the first vector entry can be referred to by 0, whereas in MatLab, this has to be 1. I have tried to modify my code accordingly, but I don’t get it to work properly. The pseducode is as follows:

input n, (a_i, : 0 ≤ i ≤ n), z_0
   for k = 0 to n-1 do
      for j = n-1 to k step -1 do
          a_j = a_j + z_0*a_(j+1)
      end do
end do
output (a_i: 0 ≤ i ≤ n)

Here is my attempt at writing this in MatLab, where a is an input vector representing coefficients in a polynomial:

function x = horner(a,z_0)
n = length(a);
for k = 1:n-1
    for j = n-1:-1:k
        a(j) = a(j) + (z_0)*a(j+1);
    end
end
x = a;

I tried this on the vector a = [1 -4 7 -5 -2] which represents coefficients in a polynomial. I also set z_0 = 3. According to my book, I should have received the output vecor a = [1 8 25 37 19], but my code gives the output vector a = [-245 -313 -146 -29 -2].

If anyone can help me clear up this code, I would be very grateful!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:24:01+00:00Added an answer on June 9, 2026 at 10:24 pm

    Try this – here a is the vector of polynomial coefficients listed with a(1) as the coefficient of the highest degree term in your polynomial. If your vector is the opposite way round, simply set

    b = fliplr(a)
    

    and call the function using vector b. This function will evaluate the polynomial using Horners algorithm. Note that this assumes z_0 is the value that you want the polynomial evaluated at, hence a single value is returned (not a vector)

    function x = horner(a,z_0)
    n = length(a);
    result = a(1);
    for j = 2:n
        result = result*z_0 + a(j);
    end
    x = result;
    

    If you want to pass in a vector of values z to evaluate so you can evaluate multiple points (the elements of z) at the same time, you can pass them in via a vector:

    function x = horner(a,z)
    n = length(a);
    m = length(z);
    result = a(1)*ones(1,m);
    for j = 2:n
        result = result.*z + a(j);
    end
    x = result;
    

    now the returned x will be your vector of results

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble translating my working C# regular expression into JavaScript's regular expression
Having some trouble with this code. Trying to return a tuple of tuples (coordinates)
I'm having some trouble with some code in Visual C# for Windows Phone The
I'm having some trouble with this code. I am trying to write a function
im having some trouble with the following code: Ext.define('...controller...', { extend: 'Ext.app.Controller', init: function()
am having some trouble with attempting to remove a memory leak from my code.
Having some trouble with R's garbage collection, when passing objects to C++. We have
Having some trouble getting a screenscraper webservice up and running on a weblogic 10.3.3
Having some trouble with what should be a very simple scenario. For example purposes,
Am having some trouble with the SkyDrive download process and hoping you can help

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.