Clear Filters
Clear Filters

Where is the error and how is the code correct?

1 view (last 30 days)
% Sample data points
x = [1, 2, 3];
f = [2, 5, 10];
% Call the function for curve fitting
dividedDifferenceTable(x, f);
Divided Difference Table: 1 2 3 0 2 5 0 0 3 10 0 0 Polynomial: f(x) = 3*sym_x - 1
Error using solution>dividedDifferenceTable
Unable to run the 'fevalJSON' function because it calls the 'input' function, which is not supported for this product offering.
function dividedDifferenceTable(x, f)
% Check if the number of data points is consistent
if length(x) ~= length(f)
error('Number of data points must be the same for x and f.');
end
n = length(x) - 1; % Degree of polynomial
F = zeros(n + 1, n + 2); % Divided difference table
% Fill in the first two columns of the table
F(:, 1) = x';
F(:, 2) = f';
% Construct the divided difference table
for j = 3:n + 2
for i = 1:n + 2 - j
F(i, j) = (F(i + 1, j - 1) - F(i, j - 1)) / (F(i + j - 2, 1) - F(i, 1));
end
end
% Display the divided difference table
disp('Divided Difference Table:');
disp(F);
% Print the polynomial
syms sym_x;
poly = F(1, 2);
for i = 2:n + 1
term = F(1, i + 1);
for j = 1:i - 1
term = term * (sym_x - F(j, 1));
end
poly = poly + term;
end
disp(['Polynomial: f(x) = ' char(poly)]);
% Evaluate the function at any given x
x_val = input('Enter the value of x to evaluate f(x): ');
result = subs(poly, sym_x, x_val);
disp(['f(' num2str(x_val) ') = ' num2str(result)]);
% Evaluate the absolute error
true_value = input('Enter the true value of f(x): ');
error = abs(result - true_value);
disp(['Absolute Error: ' num2str(error)]);
end
  2 Comments
Dyuman Joshi
Dyuman Joshi on 23 Dec 2023
Which error are you talking about? Are you running the script in a live editor?
Mohammed
Mohammed on 23 Dec 2023
the error in line 9, variable maight be used before it is defined>
i do not know how i can fixed it

Sign in to comment.

Accepted Answer

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 23 Dec 2023
Here is the corrected code:
% Sample data points
x = [1, 2, 3];
f = [2, 5, 10];
% Call the function for curve fitting
dividedDifferenceTable(x, f);
function dividedDifferenceTable(x, f)
% Check if the number of data points is consistent
if length(x) ~= length(f)
error('Number of data points must be the same for x and f.');
end
n = length(x) - 1; % Degree of polynomial
F = zeros(n + 1, n + 2); % Divided difference table
% Fill in the first two columns of the table
F(:, 1) = x';
F(:, 2) = f';
% Construct the divided difference table
for j = 3:n + 2
for i = 1:n + 2 - j
F(i, j) = (F(i + 1, j - 1) - F(i, j - 1)) / (F(i + j - 2, 1) - F(i, 1));
end
end
% Display the divided difference table
disp('Divided Difference Table:');
disp(F);
% Print the polynomial
syms sym_x;
poly = F(1, 2);
for i = 2:n + 1
term = F(1, i + 1);
for j = 1:i - 1
term = term * (sym_x - F(j, 1));
end
poly = poly + term;
end
disp(['Polynomial: f(x) = ' char(poly)]);
% Evaluate the function at any given x
x_val = input('Enter the value of x to evaluate f(x): ');
result = subs(poly, sym_x, x_val);
fprintf('f(%f) = %f \n', [x_val, result])
% Evaluate the absolute error
true_value = input('Enter the true value of f(x): ');
error = abs(result - true_value);
fprintf('Absolute Error = %f \n', error)
end
  9 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!