error with index and array elements
조회 수: 5 (최근 30일)
이전 댓글 표시
Hello all,
I am asking for your help with an error I keep getting with my code. First, here is my current code:
clear all;
close all;
clc;
% Analytical solution
t=0:600; % Makes time go from 0 to 600 sec
Tcoffee=(130*exp(-t/200)+70); % The function for the temp. of the coffee
% Numerical solution
X0=1; % Initial state
T0=0; % Initial time
T=600; % Final time of 600 sec
h=30; % Time step of 30 sec
g=70; % Forcing function (room temp)
N=T/h; % Number of time steps
Thist=[T0:h:T]; % Creates a vector of the solution
Xhist=zeros(1,N); % Creates a row vector of zeros
Xhist(1)=X0; % Assigns the first element
for i=2:1:N+1
X=Xhist(i-1); % "Old" X from step i-1
T=Thist(i-1); % "Old" time from step i-1
f=(130*exp(-T/200)+g); % Dynamics
Xhist=X+f*h; % Euler's method
end
and the error i keep getting:
Index exceeds the number of array elements (1).
Error in Untitled (line 22)
X=Xhist(i-1); % "Old" X from step i-1
Lastly, some things to note are that the value for Xhist when i type it into the command window is 6001 and not a table. What is also weird is that if i add a % infront of
g=70; % Forcing function (room temp)
becomes a 1x20 double table but then I get the error:
Unrecognized function or variable 'g'.
Error in Untitled (line 24)
f=(130*exp(-T/200)+g); % Dynamics
but if I change g in
f=(130*exp(-T/200)+g); % Dynamics
to be +70, Xhist goes back to being the number 6001 again. So if anyone can shed so light on why I'm having this issue, I would be most appreciative.
댓글 수: 0
채택된 답변
Star Strider
2021년 2월 6일
In this line:
Xhist=X+f*h; % Euler's method
‘XHist’ is re-defined as a scalar. That’s causing the problem. It probably needs to be subscripted. I leave that to you.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spline Postprocessing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!