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

조회 수: 1 (최근 30일)
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?

채택된 답변

VBBV
VBBV 2022년 11월 25일
M(n)=M(n)-(dM*dt);
  댓글 수: 3
VBBV
VBBV 2022년 11월 26일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by