what's "error: x(7): out of bound 1 (dimensions are 1x1)"?

105 visualizzazioni (ultimi 30 giorni)
clc
clear
% Define the data points
x = [1 4 6 5 3 1.5 2.5 3.5];
y = [0 1.3862944 1.7917595 1.6094379 1.0986123 0.40546411 0.91629073 1.2527630];
% Define the number of data points
n = length(x);
% Initialize the coefficient array
a = zeros(1,n);
% Set the first coefficient to the first data point
a(1) = y(1);
% Use the recursive formula to compute the coefficient
for i = 2:n
for j = i:n
y(j) = (y(j) - y(j-1))/(x(j) - x(j-i+1));
end
a(i) = y(i);
end
% Print the coefficients
disp(a)
% Define the function using the coefficients
f = @(x) a(n);
for i = n-1:-1:1
f = @(x) a(i) + f(x).*(x - x(i));
end
% Approximate the values of ln(3), ln(5), and ln(3.256)
approx_3 = f(3);
approx_5 = f(5);
approx_3256 = f(3.256);
% Print the approximations
disp(approx_3)
disp(approx_5)
disp(approx_3256)
% Compute the errors of the approximations
error_3 = abs(approx_3 - log(3));
error_5 = abs(approx_5 - log(5));
error_3256 = abs(approx_3256 - log(3.256));
% Print the errors
disp(error_3)
disp(error_5)
disp(error_3256)
Here is an Octave script that uses the Newton's Divided Difference method to determine the parameters of an interpolating polynomial for a given set of data points and approximates the value of ln(x) for a given set of points.
and the output is as follows:
0 0.4621 0.0405 -0.2564 -0.0061 0.1040 0.1267 -0.1406
error: x(7): out of bound 1 (dimensions are 1x1)
error: called from
@<anonymous> at line 30 column 17
Project1 at line 34 column 10
I don't understand these errors and how to solve them, any help is appreciated.

Risposta accettata

Dyuman Joshi
Dyuman Joshi il 2 Gen 2023
Modificato: Dyuman Joshi il 22 Gen 2023
Use another letter/symbol for the function handle input variable, as x has already been defined as an array -
% Define the data points
x = [1 4 6 5 3 1.5 2.5 3.5];
y = [0 1.3862944 1.7917595 1.6094379 1.0986123 0.40546411 0.91629073 1.2527630];
% Define the number of data points
n = length(x);
% Initialize the coefficient array
a = zeros(1,n);
% Set the first coefficient to the first data point
a(1) = y(1);
% Use the recursive formula to compute the coefficient
for i = 2:n
for j = i:n
y(j) = (y(j) - y(j-1))/(x(j) - x(j-i+1));
end
a(i) = y(i);
end
% Print the coefficients
disp(a)
0 0.4621 0.0405 -0.2564 -0.0061 0.1040 0.1267 -0.1406
% Define the function using the coefficients
f = @(k) a(n);
for i = n-1:-1:1
f = @(k) a(i) + f(k).*(k - x(i));
%^ edit the bracket for k or x accordingly
end
% Approximate the values of ln(3), ln(5), and ln(3.256)
approx_3 = f(3);
approx_5 = f(5);
approx_3256 = f(3.256);
% Print the approximations
disp(approx_3)
-0.6225
disp(approx_5)
3.0363
disp(approx_3256)
-0.4451
% Compute the errors of the approximations
error_3 = abs(approx_3 - log(3));
error_5 = abs(approx_5 - log(5));
error_3256 = abs(approx_3256 - log(3.256));
% Print the errors
disp(error_3)
1.7211
disp(error_5)
1.4268
disp(error_3256)
1.6256

Più risposte (0)

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by