I would like to create a for-loop for a vector T1 consisting of 21 elements where all are zero except the first, in order to get T2, which also has 21 elements, (temperature elements).

This with the formula: Ti(t+0.28) = 0.5*(T(i+1)-2*Ti+T(i-1)). My code right now is:T1=zeros(21); T1(1)=22; T2=linspace(0,0.3,21); But I got no clue how to write the for-loop in this case. Greatful for help

4 件のコメント

sixwwwwww
sixwwwwww 2013 年 10 月 10 日
What is Ti(t+0.28) in your code? What does it mean in your scenario? Is Ti function of 't'? Also Is T2 a vector as you defined T2=linspace(0,0.3,21);
Gustaf
Gustaf 2013 年 10 月 10 日
The i should be underlined, and 0.28 is delta t, which is calculated. Yes exactly, ti is a function of t, and T2 is a vector, from 0 to 0.3 meter, (I'm studying the heat distribution of a 0.3m concrete wall). Thanks for answering!
sixwwwwww
sixwwwwww 2013 年 10 月 10 日
Dear still your question is not that clear can you please explain what is the relation between T1, T2, t, Ti?
Gustaf
Gustaf 2013 年 10 月 10 日
I'm sorry. T1 is the old temperature, storing the temperatures (22,0,...,0) of the 21 points, and T2 stores the new temperatures at the new time, t is the time, and i is the point 1:21. T is a function of t and the position i. So Ti is the temperature at point i.

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

 採用された回答

Dear Gustaf, still I have confusions in understanding the problem. However as I understood I wrote a small code which you can run and then tell me you need this or something else:
T1 = zeros(1,21);
T2 = T1;
T1(1) = 22;
for i = 1:length(T1)
if i == 1
T2(i) = 0.5 * (T1(i + 1) - 2 * T1(i));
else if i < length(T1)
T2(i) = 0.5 * (T1(i+1) - 2 * T1(i) + T1(i-1));
else
T2(i) = 0.5 * (-2 * T1(i) + T1(i-1));
end
end
end
disp(T1)
disp(T2)

2 件のコメント

Gustaf
Gustaf 2013 年 10 月 11 日
This was exactly what I looked for, I really appreciate your time, thanks!
sixwwwwww
sixwwwwww 2013 年 10 月 11 日
You are welcome

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2013 年 10 月 10 日

コメント済み:

2013 年 10 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by