Newton's forward difference table is incorrect for some Y values

Hi all,
I am doing an assessment item that requires us to construct the Newton's forward difference table for the function values Y = [y0, y1, ... yn]. The activity is on MATLAB Grader.
I keep getting the same error: "code is incorrect for some values of Y." I think I have the correct code for the table itself but I can't figure out how to make it correct for every value.
Here is my code:
function T = forward_differences(Y)
if ~isnumeric(Y) || ~isvector(Y) % this is my attempt to fix the error
error('Input must be a numeric vector.');
end
n = length(Y);
T = zeros(n,n);
T(:,1) = Y(:);
for j = 2:n
for i = 1:n-j+1
T(i,j) = T(i+1,j-1) - T(i,j-1);
end
end
end
Here is an example of output:
Y = [2 4 9]
Y = 1×3
2 4 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
T = forward_differences(Y)
T = 3×3
2 2 3 4 5 0 9 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Thanks in advance!

3 Comments

I wonder if you need the transpose of that T -- so the first row represents the initial values, the second row represents the first differences, and so on.
@Pia. You can use diff function to get different order Newton's forward difference.
Y = [2 4 9];
dY = diff(Y);
d2Y = diff(Y,2);
T = zeros(numel(Y));
T(1,:) = Y;
T(2,:) = paddata(dY,3);
T(3,:) = paddata(d2Y,3);
T
T = 3×3
2 4 9 2 5 0 3 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Hi @Pia
The entries in Newton's forward difference table are computed as shown. While your computations appear to be correct, please verify whether the question requires you to include the column for "x" in the table as well. Occasionally, the warning messages in some grading questions are automated based on reasonable assumptions, regardless of the type of error.

Sign in to comment.

Answers (0)

Products

Asked:

Pia
on 19 Oct 2025

Commented:

on 19 Oct 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!