Can someone help with error "index exceeds number of array elements"? I attached image as to what it needs to look like..

4 vues (au cours des 30 derniers jours)
a = 1.5;
b = 4.2;%lower and upper bounds
f = @(x) 2*x.^5 -x.^4 + 0.7*x.^3 - 2*x + 13;
syms x
fx = 2*x.^5 - (x.^4) + 0.7*x.^3 - 2*x + 13;
I_exact = double (int(fx ,x ,a ,b))
I_exact = 1.6393e+03
MPRTE = @(est) abs ((I_exact-est)/ I_exact)*100;
n = 1;
I_trap_n1 = (b-a)/2*(f(a) + f(b))
I_trap_n1 = 3.2152e+03
n = 7;
h = (b-a) /n;
I_trapn7 = h/2 * (f(a)+2*f(a+h) + 2*f(a+2*h) + 2*f(a+3*h) + 2*f(a + 4*h) + 2*f( a+5*h) + 2*f(a+6*h)+f(b))
I_trapn7 = 1.6741e+03
% Single application
n = 2;
h = (b-a)/n;
I_simp13_n2 = h/3 * (f(a) + 4* f(a+h) + f(b))
I_simp13_n2 = 1.6722e+03
% Single application
n = 3;
h = (b-a) / n;
I_simp38_n3 = 3*h/8 * (f(a) + 3*f(a+h) + 3*f(a + 2*h) + f(b))
I_simp38_n3 = 1.6539e+03
n = 7;
h = (b-a)/n;
part11 = h/3 * (f(a) + 4*f(a+h) + f(a +2*h));
part12 = h/3* (f(a+2*h) + 4*f(a+3*h) + f(a+4*h));
part13 = 3*h/8*(f(a+4*h) + 3*f(a+5*h) +3*f(a+6*h) + f(b));
I_S13_13_38 = part11 + part12 + part13;
part21 = 3*h/8*(f(a)+ 3*f(a+h) + 3*f(a+2*h) + f( a+3*h));
part22 = h/3 * (f(a + 3* h) + 4*f(a+4*h) + f( a+5*h));
part23 = h/3 * (f(a+5*h) + 4*f(a+6*h)+ f(b));
I_S38_13_13 = part21 + part22 + part23;
part31 = h/3*(f(a) + 4*f(a+h) + f(a+2*h));
part32 = 3*h/8 * (f(a+2*h)+3*f(a+3*h) + 3*f(a+4*h)+f(a+5*h));
part33 = h/3 * (f(a+5*h) + 4*f(a +6*h) + f(b));
I_S13_38_13 = part31 + part32 + part33;
top = ['Method' 'Value' 'MTPRE'];
integration = [ 'Exact', 'TrapSingle', 'Simpsons1/3', 'Simpson3/8',...
'TrapComposite', 'S13_S13_S38', 'S38_S13_S13','S13_S38_S13'];
iv = [I_exact, I_trap_n1, I_simp13_n2,...
I_simp38_n3,I_trapn7,I_S13_13_38,I_S38_13_13,I_S13_38_13];
for u = 1:length(integration)
if u == 1
fprintf(['Method Value MTPRE\n' ...
'--------------- --------- --------'], top)
end
fprintf('%-15s %-9.4f %-7.4f%%\n' , integration(u), ...
iv(u), MPRTE(iv(u)))
end
Method Value MTPRE --------------- --------- --------
E 1639.2954 0.0000 % x 3215.1656 96.1309% a 1672.1783 2.0059 % c 1653.9100 0.8915 % t 1674.0856 2.1223 % T 1639.6648 0.0225 % r 1639.5990 0.0185 % a 1639.6319 0.0205 %
Index exceeds the number of array elements. Index must not exceed 8.

Réponses (2)

Cris LaPierre
Cris LaPierre le 3 Oct 2022
The error means you are indexing a variable using an index value that is greater than the number of elements in your variable.
% Create a variable with 3 elements
a = 1:3
a = 1×3
1 2 3
% This works
a(2)
ans = 2
% This is your error
a(4)
Index exceeds the number of array elements. Index must not exceed 3.
In your case, the error is caused by iv(u). Here, iv is a 1x8 vector. You get the error when u>8. I'm not sure what iv represents, but it is either not as long as you think it is, or you are not using it as you should in your code.
  3 commentaires
Walter Roberson
Walter Roberson le 3 Oct 2022
You have
integration = [ 'Exact', 'TrapSingle', 'Simpsons1/3', 'Simpson3/8',...
'TrapComposite', 'S13_S13_S38', 'S38_S13_S13','S13_S38_S13'];
Remember that in MATLAB, 'Exact' is a 1 x 5 array of char. 'ABC' in MATLAB is the same as ['A', 'B', 'C'] which in turn is the same as taking uint16([65 66 67]) and setting a flag in the header saying 'display these as characters'. There is no special kind of hardware memory for characters: there is just unsigned integers and decades of convention about which unsigned integer is to be treated as equivalent to which character. So when you ['Exact', 'TrapSingle'] that is the same as char(horzcat(uint16('Exact'), uint16('TrapSingle'))) forming an array 'ExactTrapSingle' . This is the hardware representation of characters.
MATLAB also has cell arrays of characters,
integration = { 'Exact', 'TrapSingle', 'Simpsons1/3', 'Simpson3/8',...
'TrapComposite', 'S13_S13_S38', 'S38_S13_S13','S13_S38_S13' };
in which integration(4) would be the cell {'Simpson3/8'} and integration{4} would be the vector 'Simpson3/8' .
MATLAB also has (introduced between R2016b and R2017a) string objects, in which () indexing is similar to cell indexing
integration = [ "Exact", "TrapSingle", "Simpsons1/3", "Simpson3/8",...
"TrapComposite", "S13_S13_S38", "S38_S13_S13","S13_S38_S13"];
Then integration(4) would be "Simson3/8" -- a scalar string array. In a number of contexts (but not all) a scalar string array can be treated like a character vector.

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 3 Oct 2022
The root cause of the problem is that the variables top and integration aren't the sizes you think they are. You may think the length of top is 3 and the length of integration is 8. They aren't.
top = ['Method' 'Value' 'MTPRE']
top = 'MethodValueMTPRE'
integration = [ 'Exact', 'TrapSingle', 'Simpsons1/3', 'Simpson3/8',...
'TrapComposite', 'S13_S13_S38', 'S38_S13_S13','S13_S38_S13']
integration = 'ExactTrapSingleSimpsons1/3Simpson3/8TrapCompositeS13_S13_S38S38_S13_S13S13_S38_S13'
length(top)
ans = 16
length(integration)
ans = 82
Those variables don't contain the individual words, they contain all the letters of those words combinedintoonelongchararray.
If you used a string array instead:
top2 = ["Method" "Value" "MTPRE"]
top2 = 1×3 string array
"Method" "Value" "MTPRE"
integration2 = [ "Exact", "TrapSingle", "Simpsons1/3", "Simpson3/8",...
"TrapComposite", "S13_S13_S38", "S38_S13_S13","S13_S38_S13"]
integration2 = 1×8 string array
"Exact" "TrapSingle" "Simpsons1/3" "Simpson3/8" "TrapComposite" "S13_S13_S38" "S38_S13_S13" "S13_S38_S13"
length(top2)
ans = 3
length(integration2)
ans = 8
then each word is stored as a separate element of each string array.

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by