Index exceeds the number of array elements. Index must not exceed 751.

2 views (last 30 days)
Trying to create a modelling script that will call a variety of modelling techniques to approximate a (relatively simple) ODE. z is the matrix that will store each value for the approximation as the simulation runs.
Here is the code that calls the modelling technique functions from other scripts:
function [t,z] = ivpSolver(t0,z0,dt,tend,dM)
% ivpSolver Solve an initial value problem (IVP) and plot the result
%
% [T,Z] = ivpSolver(T0,Z0,DT,TE) computes the IVP solution using a step
% size DT, beginning at time T0 and initial state Z0 and ending at time
% TEND. The solution is output as a time vector T and a matrix of state
% vectors Z.
% Set initial conditions
t(1) = t0;
z(:,1) = z0;
M(1)=2;
% Continue stepping until the end time is exceeded
n = 1;
while t(n) <= tend
% Increment the time vector by one time step
t(n+1) = t(n) + dt;
% Apply Euler's method for one time step
%[z] = stepEuler(z(:,n), dt, M, dM);
%Apply RK4 method for one time step
z(:,n+1) = stepRungeKutta(z(:,n), dt, M(n), dM);
if M(n)>0.5
M(n+1)=M(n)-(dM*dt);
end
n = n + 1;
end
running the code with the command: [t,z] = ivpSolver(0,[10;0],0.1,100,0.02); gives the aforementioned error arrising from line 14: z(:,n+1) = stepRungeKutta(z(:,n), dt, M(n), dM);. Is it not possible to create an array that is longer than 751 columns or is something else causing this error to be displayed?

Accepted Answer

VBBV
VBBV on 25 Nov 2022
M(n)=M(n)-(dM*dt);
  3 Comments
VBBV
VBBV on 26 Nov 2022
When the if-end condition is not satisfied, variable M have number of elements less by 1, Following the while loop statements, when n is incremented by 1, it will result in error.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!