what is error actually in my code, any one solve this?

1 回表示 (過去 30 日間)
Dhines
Dhines 2014 年 8 月 5 日
コメント済み: Hikaru 2014 年 8 月 5 日
xdim=50;
ydim=50;
v_now=zeros(xdim,ydim);
v_prev=zeros(xdim,ydim);
for i=1:1:ydim
v_now(i,xdim)=20;
end
x=0.01;
for k=[0.01,0.001,0.0001,0.00001]
iter=0;
iter_error=max(max(abs(v_now-v_prev)));
while(iter_error>k)
iter=iter+1;
for i= 2:1:xdim-1
for j=2:1:ydim-1
v_now(i,j)=(v_now(i-1,j)+v_now(i+1,j)+v_now(i,j-1)+v_now(i,j+1))/4;
end
end
iter_error=max(max(abs(v_now-v_prev)));
v_prev=v_now;
end
h(k)=iter;
end
plot(k,h(k));

回答 (2 件)

Julia
Julia 2014 年 8 月 5 日
Your problem is that k is not an integer. Matlab considers h as an array and you must use integers to access the entries of arrays.

Hikaru
Hikaru 2014 年 8 月 5 日
編集済み: Hikaru 2014 年 8 月 5 日
A for loop will repeat a statement for a specified number of times. The line
for k=[0.01,0.001,0.0001,0.00001]
should be fixed. You can use decimal as increments in for loop, but for indexing, you need to use other variables.
h(k)=iter; %the elements in k are not integers, so you cannot use them as index.
  3 件のコメント
Dhines
Dhines 2014 年 8 月 5 日
i modified it.. but the graph comes blank
Hikaru
Hikaru 2014 年 8 月 5 日
Thanks Julia, I edited my statement to make it clearer. I meant to say it's not okay for indexing.
OP, can you update the modified code? We can try to see where the problem is.

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

カテゴリ

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