필터 지우기
필터 지우기

" Attempted to access t(2); index out of bounds because numel(t)=1."

조회 수: 3 (최근 30일)
Jose
Jose 2014년 8월 19일
편집: Matt J 2014년 8월 19일
Need your help once again :) I am getting this error which I dont know what it means. I am trying to write a code on Euler's method for my homework. " Attempted to access t(2); index out of bounds because numel(t)=1."
%%Tasks
clc, clear all
func = @(t,x) -t.*x;
t = 0; %Initial Value
x = 1; %Initial Value
h = 0.1; %Step size
N = 10;
tarrays = zeros(1,N)
xarrays = zeros(1,N)
for i = 1:N
x(i+1) = x(i) + h.*func(t(i),x(i));
tarrays = t(i)
xarrays = x(i+1)
end

답변 (3개)

Matt J
Matt J 2014년 8월 19일
It means you've done something like this,
>> t=1:5; a=t(6)
Attempted to access t(6); index out of bounds because numel(t)=5.
Use the dbstop command and its cousins to locate the error.

Jose
Jose 2014년 8월 19일
I still cant figure it out :(
  댓글 수: 5
Jose
Jose 2014년 8월 19일
Error in lab6 (line 15) x(i+1) = x(i) + h.*func(t(i),x(i));
Matt J
Matt J 2014년 8월 19일
편집: Matt J 2014년 8월 19일
It just says "index exceeds matrix dimensions" I dont know how and where
The error message tells you where the error is (line 15). It also tells you why (my Answer above).
But you clearly didn't use DBSTOP or refer to the other debugging commands I pointed you to if you haven't reached a K>> prompt.

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


Guillaume
Guillaume 2014년 8월 19일
편집: Guillaume 2014년 8월 19일
There are lots of problems with your loop:
x(i+1) = x(i) + h.*func(t(i),x(i));
is the cause of the error. You've defined t as a scalar (t=0; %Initial Value) and then access it as an array. t only has one element, therefore t(i) when i==2 is invalid.
tarrays = t(i)
Even if t was array, t(i) would be a scalar. You would then overwrite your predefined tarrays with that scalar. Each loop tarrays would just be a scalar containing t(i). At the end tarrays would just be t(N).
xarrays = x(i+1)
Same problem as with tarrays.
  댓글 수: 1
Jose
Jose 2014년 8월 19일
편집: Jose 2014년 8월 19일
so I rewrote
%%Tasks
clc, clear all
%func = @(t,x) -t.*x;
func = @(t,x) sinh(x ./ (t+1));
% func = @(t,x) (t*x.^2)*cos(x.^2);
t0 = 1; %Initial Value
x = 1; %Initial Value
h = 0.1; %Step size
N = 10;
tarrays = zeros(1,N)
xarrays = zeros(1,N)
for i = 1:N
t = t0
x(i+1) = x(i) + (h*func(t(i),x(i)))
xarrays = x
tarrays = t
end
I am not sure what to do about your first statement though.

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

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by