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

  • SEARCH
  • Home
  • 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 8727859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:31:06+00:00 2026-06-13T08:31:06+00:00

I have been given an assignment in which I am supposed to write an

  • 0

I have been given an assignment in which I am supposed to write an algorithm which performs polynomial interpolation by the barycentric formula. The formulas states that:

p(x) = (SIGMA_(j=0 to n) w(j)*f(j)/(x – x(j)))/(SIGMA_(j=0 to n) w(j)/(x – x(j)))

I have written an algorithm which works just fine, and I get the polynomial output I desire. However, this requires the use of some quite long loops, and for a large grid number, lots of nastly loop operations will have to be done. Thus, I would appreciate it greatly if anyone has any hints as to how I may improve this, so that I will avoid all these loops.

In the algorithm, x and f stand for the given points we are supposed to interpolate. w stands for the barycentric weights, which have been calculated before running the algorithm. And grid is the linspace over which the interpolation should take place:

function p = barycentric_formula(x,f,w,grid)

%Assert x-vectors and f-vectors have same length.
if length(x) ~= length(f)
    sprintf('Not equal amounts of x- and y-values. Function is terminated.')
    return;
end

n = length(x);
m = length(grid);
p = zeros(1,m);

% Loops for finding polynomial values at grid points.  All values are
% calculated by the barycentric formula.
for i = 1:m
    var = 0;
    sum1 = 0;
    sum2 = 0;
    for j = 1:n
        if grid(i) == x(j)
            p(i) = f(j);
            var = 1;
        else
            sum1 = sum1 + (w(j)*f(j))/(grid(i) - x(j));
            sum2 = sum2 + (w(j)/(grid(i) - x(j)));
        end
    end
    if var == 0
        p(i) = sum1/sum2;
    end    
end
  • 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-13T08:31:07+00:00Added an answer on June 13, 2026 at 8:31 am

    This is a classical case for matlab ‘vectorization’. I would say – just remove the loops. It is almost that simple. First, have a look at this code:

    function p = bf2(x, f, w, grid)
    
    m = length(grid);
    p = zeros(1,m);
    
    for i = 1:m
        var = grid(i)==x;
        if any(var)
            p(i) = f(var);
        else
            sum1 = sum((w.*f)./(grid(i) - x));
            sum2 = sum(w./(grid(i) - x));
            p(i) = sum1/sum2;
        end
    end
    end
    

    I have removed the inner loop over j. All I did here was in fact removing the (j) indexing and changing the arithmetic operators from / to ./ and from * to .* – the same, but with a dot in front to signify that the operation is performed on element by element basis. This is called array operators in contrast to ordinary matrix operators. Also note that treating the special case where the grid points fall onto x is very similar to what you had in the original implementation, only using a vector var such that x(var)==grid(i).

    Now, you can also remove the outermost loop. This is a bit more tricky and there are two major approaches how you can do that in MATLAB. I will do it the simpler way, which can be less efficient, but more clear to read – using repmat:

    function p = bf3(x, f, w, grid)
    
    % Find grid points that coincide with x.
    % The below compares all grid values with all x values
    % and returns a matrix of 0/1. 1 is in the (row,col)
    % for which grid(row)==x(col)
    
    var  = bsxfun(@eq, grid', x);
    
    % find the logical indexes of those x entries
    varx = sum(var, 1)~=0;
    
    % and of those grid entries
    varp = sum(var, 2)~=0;
    
    % Outer-most loop removal - use repmat to
    % replicate the vectors into matrices.
    % Thus, instead of having a loop over j
    % you have matrices of values that would be
    % referenced in the loop
    
    ww = repmat(w, numel(grid), 1);
    ff = repmat(f, numel(grid), 1);
    xx = repmat(x, numel(grid), 1);
    gg = repmat(grid', 1, numel(x));
    
    % perform the calculations element-wise on the matrices
    sum1 = sum((ww.*ff)./(gg - xx),2);
    sum2 = sum(ww./(gg - xx),2);
    p    = sum1./sum2;
    
    % fix the case where grid==x and return
    p(varp) = f(varx);
    
    end
    

    The fully vectorized version can be implemented with bsxfun rather than repmat. This can potentially be a bit faster, since the matrices are not explicitly formed. However, the speed difference may not be large for small system sizes.

    Also, the first solution with one loop is also not too bad performance-wise. I suggest you test those and see, what is better. Maybe it is not worth it to fully vectorize? The first code looks a bit more readable..

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

Sidebar

Related Questions

We have been given a site xyz.com, in which the home page has images
I have been given an assignment for university where i have to read from
I just been given a new assignment which looks like its going to be
I have been given an assignement for a graphics module, one part of which
I have been given an assignment to drop one of our product's dll and
I have been given an assignment in my CS class to come up with
Good day, I have been given an assignment that gets data off a CSV
I have been given an assignment to simulate an NFA in Java. Now the
I have been given an old dll and the assignment of accessing it through
I have been given an assignment to complete the following task: I will be

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.