how to fix Index exceeds matrix dimensions?

3 ビュー (過去 30 日間)
sing lai
sing lai 2014 年 4 月 23 日
コメント済み: Niklas Nylén 2014 年 4 月 24 日
L = 0.8;c=1;T=0.5;
a = 0 :120;
it0 = inline('0.2.*sin(x)','x');
M=80;N=50;
dx=L/M;dt=T/N;
for i = 1:M + 1, u(i,1) = it0(x(i));
end
r = c*(dt/dx); r1 = (r^2)/2; r2 = 2*(1 - (r^2));
u(2:M,2) = r1*u(1:M - 1,1) + (1 - (r^2))*u(2:M,1) + r1*u(3:M + 1,1);
for k = 3:N + 1
u(2:M,k) = (r^2)*u(1:M - 1,k - 1) + r2*u(2:M,k-1) + (r^2)*u(3:M + 1,k - 1)...
- u(2:M,k - 2);
end
for n=1:N;
figure
plot(a,u(:,n));
end
I want plot waveform graph that u(x,t) versus x, but come out error [??? Index exceeds matrix dimensions.
Error in ==> osc at 8 end ]
Can anyone help me fix the error?thanks..

採用された回答

Niklas Nylén
Niklas Nylén 2014 年 4 月 23 日
First, you should predefine the size of u instead of changing the size throughout your script.
Second, in the first for loop you have written
u(i,1) = it0(x(i));
But x is not defined, did you mean to use the variable a as input? Why do you even need a loop, the following should be fine (assuming u is predefined)
u(:,1) = it0(a);
When I run your code I do not get the "Index exceeds matrix dimensions" error but instead I get an error because the number of rows in u is not equal to the number of values in a. Because you did not predefine the size of u the size will be 81*81 and the vector a will have length 120.
  2 件のコメント
sing lai
sing lai 2014 年 4 月 23 日
Thanks for helping, but since I'm new user to Matlab, i was not able to understand your meaning..actually I want to generate the graph as below,can you help me fix the error?thanks..
Niklas Nylén
Niklas Nylén 2014 年 4 月 24 日
Can you please add the definition of u(x,t) also.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by