Correct the code for me please

1 次查看(过去 30 天)
Mohammed
Mohammed 2023-12-28
编辑: Hassaan 2023-12-28
% Given data points
x = [0,0.1, 0.8, 0.6, 0.9, 1];
f = [-1,-1.2299,-3.455, -2.9949, -3.3929, -3];
% Part (a) - Construct the divided difference table
n = length(x);
d = zeros(n);
d(1,1) = f(1);
for i = 2:n
d(i,1) = (f(i) - f(i-1))/(x(i) - x(i-1));
for j = 2:i
d(i,j) = (d(i,j-1) - d(i-1,j-1))/(x(i) - x(i-j));
end
end
Array indices must be positive integers or logical values.
disp('Divided Difference Table:')
disp(d)
% Part (b) - Evaluate the polynomial of order "n-1"
y = 0;
for i = 1:n
temp = f(1);
for j = 1:i
temp = temp * (0.5 - x(1:i-j)) + d(i,j);
end
y = y + temp;
end
fprintf('The value of the polynomial at x = 0.5 is %.4f\n', y)
% Part (c) - Evaluate f(0.5)
f_interp = interp1(x,f,0.5);
fprintf('The value of f(0.5) is %.4f\n', f_interp)
% Part (d) - Check your answers using built-in MATLAB functions
p = polyfit(x,f,5);
y_polyfit = polyval(p,0.5);
fprintf('The value of the polynomial at x = 0.5 using polyfit is %.4f\n', y_polyfit)
f_interp_polyfit = interp1(x,f,0.5,'linear');
fprintf('The value of f(0.5) using interp1 is %.4f\n', f_interp_polyfit)

回答(2 个)

Image Analyst
Image Analyst 2023-12-28
I don't know what you're trying to do but this line:
d(i,j) = (d(i,j-1) - d(i-1,j-1))/(x(i) - x(i-j));
when i and j are both 2, you get i-j = 0 and there is no zeroeth index of a vector. Indexes start at 1, not 0. So rethink what you really want to do.

Hassaan
Hassaan 2023-12-28
编辑:Hassaan 2023-12-28
In this code snippet:
  • The divided difference table d is correctly initialized to be n by n.
  • The array2table function is used to format the divided difference table output.
  • The polynomial evaluation using Newton's divided differences (Part b) is fixed to correctly accumulate the product of (xi - x(k)) for the ith term of the polynomial.
  • The interp1 function is correctly called with 'linear' to perform linear interpolation (Part c).
  • The polyfit and polyval functions are used to fit a polynomial and evaluate it at xi = 0.5 (Part d).
When you run this code, it will calculate and display the divided difference table, evaluate the polynomial at x = 0.5 using Newton's divided differences, interpolate the value at x = 0.5, and verify the results using MATLAB's built-in functions.
% Given data points
x = [0, 0.1, 0.8, 0.6, 0.9, 1];
f = [-1, -1.2299, -3.455, -2.9949, -3.3929, -3];
% Part (a) - Construct the divided difference table
n = length(x);
d = zeros(n, n); % Make sure the matrix has dimensions n x n
d(:,1) = f(:); % Fill in the first column with f values
% Calculate divided differences
for i = 2:n
for j = 2:i
d(i,j) = (d(i,j-1) - d(i-1,j-1)) / (x(i) - x(i-j+1));
end
end
% Display the divided difference table
disp('Divided Difference Table:')
Divided Difference Table:
disp(array2table(d, 'VariableNames', cellstr(num2str((1:n)')), ...
'RowNames', cellstr(num2str(x'))))
1 2 3 4 5 6 _______ _______ _______ ______ ______ ______ 0 -1 0 0 0 0 0 0.1 -1.2299 -2.299 0 0 0 0 0.8 -3.455 -3.1787 -1.0996 0 0 0 0.6 -2.9949 -2.3005 1.7564 4.7601 0 0 0.9 -3.3929 -1.3267 9.7383 9.9774 5.797 0 1 -3 3.929 13.139 17.004 7.8075 2.0106
% Part (b) - Evaluate the polynomial at x = 0.5 using Newton's divided differences
xi = 0.5;
y = f(1);
P = 1;
for i = 2:n
P = P * (xi - x(i-1));
y = y + P * d(i,i);
end
fprintf('The value of the polynomial at x = 0.5 using Newtons divided differences is: %.4f\n', y);
The value of the polynomial at x = 0.5 using Newtons divided differences is: -2.6251
% Part (c) - Directly evaluate f(0.5) using linear interpolation
f_interp = interp1(x, f, xi, 'linear');
fprintf('The value of f(0.5) using linear interpolation is: %.4f\n', f_interp);
The value of f(0.5) using linear interpolation is: -2.6419
% Part (d) - Check the results using built-in MATLAB functions
% Fit a polynomial of order n-1
p = polyfit(x, f, n-1);
% Evaluate the polynomial at xi using polyval
y_polyfit = polyval(p, xi);
fprintf('The value of the polynomial at x = 0.5 using polyfit is: %.4f\n', y_polyfit);
The value of the polynomial at x = 0.5 using polyfit is: -2.6251
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.

类别

Help CenterFile Exchange 中查找有关 Polynomials 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by