Newton's Divided Differences Function
15 次查看(过去 30 天)
显示 更早的评论
I am trying to make a simple code to compute newtons divided differences from an array of x values and an array of y values. I am new to coding so I am not sure if I am doing this right / where my error is and I keep getting a matrix of all zeros as my answer. Here is my coded attached below
function [B] = dividediffs(x,y)
%DIVIDEDDIFFS computes the divided differences of an array of (x,y) points
% INPUT: two arrays of x and corresponding y values OUTPUT: divided differences
n = length(x);
B = zeros(n,n);
if n == 1
B = y(1);
else
for k = 2:n
for i = 1: n-k+1
if (i+k) <= n
B(i,k) = (B(i+1,k-1)-B(i,k-1))/(x(i+k)-x(i));
end
end
end
end
0 个评论
回答(1 个)
Mann Baidi
2024-1-25
编辑:Mann Baidi
2024-1-25
Hi Abiageal,
As per the information provided in the question, I understand that you are facing issues in implementing 'Newton's Divided Differences Function' as you are getting 'B' a matrix of all zeros.
This is because in the 'Newton's Divided Differences Function' we need to initialize the first row of 'divided differences table'.
You can try initialising the first row with the help of this code.
x=1:10;
y= 11:20;
ans=dividediffs(x,y)
function [B] = dividediffs(x,y)
%DIVIDEDDIFFS computes the divided differences of an array of (x,y) points
% INPUT: two arrays of x and corresponding y values OUTPUT: divided differences
n = length(x);
B = zeros(n,n);
B(:, 1) = y';
if n == 1
B = y(1);
else
for k = 2:n
for i = 1: n-k+1
if (i+k) <= n
B(i,k) = (B(i+1,k-1)-B(i,k-1))/(x(i+k)-x(i));
end
end
end
end
end
Hope this resolve your query!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!