Index exceeds the number of array elements (6) (I´m doing the program for composite simpson 3/8 rule)

1 view (last 30 days)
r = mod(n,3);
if (r ==0)
n = n;
end
x = linspace(alfa,beta,n)';
%evaluamos la función en los puntos de la partición x
f = zeros(n,1);
for k = 1:n
f(k) = feval(fx,x(k));
end
h = x(4)-x(1);
Ix = 0;
% n es multiplo de 3 / n = 3*m / #de subintercalos es: (n)/3 = m.
m = floor(n/3);
for j = 1:m
ind1 = 3*(j-1)+1; ind2 = 3*j-1; ind3 = 3*j; ind4 = 3*j+1;
Ix = Ix + ((3*h)/8)*(f(ind1)+3*f(ind2)+3*f(ind3)+f(ind4));
end
end
  2 Comments
Sindar
Sindar on 25 Oct 2020
Please copy the full error text.
Also, what's the point of this block?
if (r ==0)
n = n;
end
The only effect would be that the code might throw an 'n is undefined' error when r=0 and leaves it for later if not
Ivonne Márquez Delgado
Ivonne Márquez Delgado on 25 Oct 2020
Index exceeds the number of array elements (6).
Error in misimpson38 (line 36)
Ix = Ix + ((3*h)/8)*(f(ind1)+3*f(ind2)+3*f(ind3)+f(ind4));
Error in prueba_misimpson38 (line 12)
[Ix] = misimpson38(fx,alfa,beta,n);
I tried to use that as n= n+1 so it would count the 4th element, but the answer was wrong

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 26 Oct 2020
The error generally means you are trying to extract a value of an element of a vector that does not exist. Here, your vector is f and one of your indices (ind1-4) contains a value greater than the length of f (which is 6).
f = [1 2 3 4 5 6]
f = 1×6
1 2 3 4 5 6
f(10)
Index exceeds the number of array elements (6).

Categories

Find more on Downloads in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!