Error - Index exceeds the number of array elements (1)

1 view (last 30 days)
clear all
close all

Accepted Answer

Superficial on 17 Nov 2020
Edited: Superficial on 17 Nov 2020
On a simple level, that error occurs because you are trying to 'index' a variable that isn't big enough for the index position it's seeking. It's like me asking you to please find the 4th number in the vector [1 2 3]. You can't do it; it doesn't exist.
But in general, your code is just quite confused. Where did you find it? There are a few problems, it's hard to know where to start.
Your 'x' series is initially defined as zeroes(1,length(t)). In your example, 1x1001. This means that when you try to index x(a,i) it will fail when a > 1. But anyway, you have already changed it so x = [0.03;0.01]; Now x is only 1x2 so you won't be able to index x(a,i) when a > 1 or i > 2.
In your ODE function, you have an input argument 't' but this is unused.
I think you may need to start with something more basic to get a better understanding of how to write in Matlab code E.g. defining variables and indexing.

More Answers (0)


Find more on Loops and Conditional Statements 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!