Can someone help with error "index exceeds number of array elements"? I attached image as to what it needs to look like..
1 view (last 30 days)
Show older comments
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))
MPRTE = @(est) abs ((I_exact-est)/ I_exact)*100;
n = 1;
I_trap_n1 = (b-a)/2*(f(a) + f(b))
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))
% Single application
n = 2;
h = (b-a)/n;
I_simp13_n2 = h/3 * (f(a) + 4* f(a+h) + f(b))
% 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))
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
0 Comments
Answers (2)
Cris LaPierre
on 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
% This works
a(2)
% This is your error
a(4)
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 Comments
Walter Roberson
on 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.
Steven Lord
on 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']
integration = [ 'Exact', 'TrapSingle', 'Simpsons1/3', 'Simpson3/8',...
'TrapComposite', 'S13_S13_S38', 'S38_S13_S13','S13_S38_S13']
length(top)
length(integration)
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"]
integration2 = [ "Exact", "TrapSingle", "Simpsons1/3", "Simpson3/8",...
"TrapComposite", "S13_S13_S38", "S38_S13_S13","S13_S38_S13"]
length(top2)
length(integration2)
then each word is stored as a separate element of each string array.
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!