Why do I have differnt answers every time!

As title. This is my code. I don't understand why the answers will change each time I run it! Please tell me if there is any wrong! Thx!!
DelT = 0.1;
DelX = 0.2;
for i = 1:6
for j = 1:50
T(i,1) = 100;
T(6,j) = 100;
if j > 1
T(1,j) = 30;
else
T(1,j) = 100;
end
end
end
for i = 2:5
for j = 1:49
T(i,j+1) = T(i,j) + (0.1*DelT / (DelX^2) )* (T(i+1,j)-2*T(i,j)+T(i-1,j)) ;
end
end
T'

回答(2 个)

Start your code with
clear T
% Or better, pre-allocate
T=zeros(6,50)
CHANG CHUN
CHANG CHUN 2013-10-20
编辑:CHANG CHUN 2013-10-20
Thank u for your reply! It didn't change anymore,
but actually the answer was still wrong.
On the contrary, when the answers changed,
they could be correct after I had run them for several times.
The value at T(2,3) should be 82.5.
(I got this value to make sure the answers from the code are correct)
If I run it for several times, then I can get this value at T(2,3), and there should not be any value lower than 44.
(If I run it for several times, the valus lower than 44 would be higher and higher until they reach 44)
I still don't know how to solve it......@@

1 个评论

The value of T(2,3) is 57.5. It is that value the first time and every time - it does not change after repeated runs. I don't know why you think it should. It did before but you didn't like that so Azzi gave you code to correct that and now it will not change every time. We don't know what you want or why you think 82.5 is the correct value. By the way, your "Answer" is not an answer so it should have been a comment to Azzi.

此问题已关闭。

标签

尚未输入任何标签。

提问:

2013-10-20

关闭:

2021-8-20

Community Treasure Hunt

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

Start Hunting!

Translated by