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

1 回表示 (過去 30 日間)
Timothy Dunning
Timothy Dunning 2022 年 11 月 25 日
コメント済み: Timothy Dunning 2022 年 11 月 26 日
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.
Timothy Dunning
Timothy Dunning 2022 年 11 月 26 日
Thanks a bunch

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops 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