The error “Index exceeds the number of array elements (1)” has troubled many MatLab users. Not after today, though, as we will lend you great tips for tackling it! Click for more.
There are no fixed rules or theories behind this error. Hence, in our opinion, it would be best to foster your comprehension with an example!
Suppose you want to create one Matlab function that uses modified Euler methods for approximating solutions for certain differential equations. The first step is to write the codes:
function output = modified_euler2(T,n,g,C,K,L) f = @(v,y) (g - C*abs(v)*v -max(0, K*(y - L))); h = T / n; t = 0:h:T; y = zeros(1,n+1); v = zeros(1,n+1); k1 = zeros(1,n+1); k2 = zeros(1,n+1); for i = 1:n+1 y(i+1)=y(i)+0.5*(k1(i)+k2(i)); k1 = h*f(v(i),y(i)); k2=h*f(v(i)+h,y(i)+k1(i)); end output = t,y,v,h figure plot(y) end
Once done, you call the Window command function:
Example (Code) (cont):
Here is where the error occurs.
Index exceeds the number of array elements (1). Error in modified_euler2 (line 10) y(i+1)=y(i)+0.5*(k1(i)+k2(i));
By looking at a specific illustration, you will gain better insights into the cause of the errors and apply the suggested solution to your own problem. We will explore the reasons why this error occurs in the next section.
Why Does The Error Occur?
So what is the primary error here? It seems you have assigned your floating-point values to k1 and k2. That is a wrong move, because you are supposed to do that to k1(i) and k2(i).
The problem also lies in the next phase, where you encounter array elements with the “i=2” index. In these instances, your modified “k” are considered numbers.
Nevertheless, Matlab interprets it as the array of the “1” length. Are you still not convinced? You may testify to that notion by picking “i” and printing it at the loop starter, assigning it the debug output.
The problems do not end there! Worse, you seem to have performed the Heun technique in the wrong orders.
Though such approaches are often showcased in textbooks, they cannot apply to all situations.
As per traditions dating back to Kutta and Heun, the next value’s formula should be printed first, before you take care of the stages’ details. And how can you compute them? You must acquire the stages before adding their values to the steps’ formula.
Also, you seem to have treated “y” like time. And yet, you establish second-order equations, with “v’=y” and “v = y'”. Either view it as a first-order vector-valued system or call for “k” values for your y and v.
Last but not least, you are attempting several index operations at once, one of which is behind the error. One main issue here is that the factor you want to index only has one element (you will be informed of that by Matlab).
And yet, you still try to ask for i + 1 – th (or i-th) element. Remember that you cannot perform it unless the “i” equals 1. Thus, to assess the problem, you must identify which index operation is attempting to enter a non-existent element.
How to Fix The Error “Index Exceeds The Number of Array Elements (1)”
Okay, so that’s all there is about the causative factor of the error. How about its solutions? You need to fix the order in which you perform the Heun technique. Let’s see the illustration below.
y = zeros(1,n+1); v = zeros(1,n+1); for i = 1:n k1y = h*v(i); k1v = h*f(v(i),y(i)); k2y = h*(v(i)+k1v) k2v = h*f(v(i)+k1v,y(i)+k1y); y(i+1)=y(i)+0.5*(k1y+k2y); v(i+1)=v(i)+0.5*(k1v+k2v); end
This article has illustrated how to fix the error “Index exceeds the number of array elements (1).” We hope that the example is simple enough for you to understand! For similar errors on Matlab (such as solve() throws this result empty sym: 0-by-1 and fails to get the correct results), browse ITtutoria for more support!