フィルターのクリア

" 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.

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by