MATLAB: Recieving error Index exceeds the number of array elements (93) when attempting to useODE45

arraycodedifferential equationserrorhelplinematrixode45

I get only 93 elements in the t matrix and i have to find the 350th one.
I need the 350th element in the entire matrix so i need the 35th column on the 4th row element. That is my number. how should I write it so i wont get any errors?
How should I write this Code: ??

Best Answer

There are a lot of severe problems in your code. One of them:
function vp = Code1(t,v)
...
k=max(size(t));
You call this as function to be integrated through ode45(). Then t is the time, which is a scalar for each call. While size(t) replies [1,1], the maximum is 1 in every case. Therefore the loop for j=1:k is meaningless.
Another problem is that Code1 replies a function with discontinuities, but ODE45 handles only smooth functions. See ODE45 discontinuities (link).
Your t is a [93x1] double vector, I assume it is the time. Then you cannot extract "the 35th column on the 4th row". Why do you think, that such an element should exist?
If you integrate the function Code1 for the inputs t,v, it is strange, that you overwrite the values if v:
v(1)=0;
v(2)=0;
v(3)=0;
v(4)=0;
v(5)=0;
Then you evaluate the function on the same point for each call.
A hint: You can simplify:
if v(2)>=1.5*10^(-3)
v(2)=1.5*10^(-3);
end
if v(2)<=-1.5*10^(-3)
v(2)=-1.5*10^(-3);
end
to
v(2) = min(v(2), 1.5e-3);
v(2) = max(v(2), -1.5e-3);
Related Question