To plot 1D temperature distribution plot versus lenght of the channel of the fin
1 回表示 (過去 30 日間)
古いコメントを表示
Ashwini nanjunda
2023 年 10 月 12 日
コメント済み: Ashwini nanjunda
2023 年 11 月 7 日
Hello,
I am using this piece of code to get the temperature distribution across Fin of varying channel length. Kindly help me with changes in code where i can vary the lenght of the fin and see the corresponding changes in temperature.
help me in including the dx parameter inside the loop.
clc
clear all
L=1000;
N=100;
dx=L/(N-1);
T=zeros(N,1);
Tb=300;
k=1;
for j=1;1;k
T(1,1)=Tb;
for i=2:1:N-1
T(i,1)=(T(i+1,1)+T(i-1,1))/2;
end
T(N,1)=T(N-1,1);
end
plot(T);
0 件のコメント
採用された回答
Walter Roberson
2023 年 10 月 19 日
移動済み: Walter Roberson
2023 年 10 月 19 日
L=0.1;
n=10;
T0=0;
T1s=40;
T2s=20;
dx=L/n;
alpha=0.0001;
t_final=60;
dt=0.1;
x = dx/2:dx:L-dx/2; %important change
T=ones(n,1)*T0;
dTdt=zeros(n,1);
t=0:dt:t_final;
for j=1:length(t)
for i=2:n-1
dTdt(i)=alpha*(-(T(i)-T(i-1))/dx^2+(T(i+1)-T(i))/dx^2);
end
dTdt(1)=alpha*(-(T(1)-T1s)/dx^2+(T(2)-T(1))/dx^2);
dTdt(n)=alpha*(-(T(n)-T(n-1))/dx^2+(T2s-T(n))/dx^2);
T=T+dTdt*dt;
plot(x,T,'LineWidth',3)
hold on
axis([0 L 0 50])
xlabel('Distance(m)')
ylabel('Temperature(\circC)')
%pause(0.1)
end
hold off
3 件のコメント
Walter Roberson
2023 年 11 月 6 日
I do not see much change.
L=0.1;
n=10;
T0=0;
T1s=50;
T2s=10;
dx=L/n;
alpha=0.0001;
t_final=60;
dt=0.1;
x = dx/2:dx:L-dx/2; %important change
T=ones(n,1)*T0;
dTdt=zeros(n,1);
t=0:dt:t_final;
for j=1:length(t)
for i=2:n-1
dTdt(i)=alpha*(-(T(i)-T(i-1))/dx^2+(T(i+1)-T(i))/dx^2);
end
dTdt(1)=alpha*(-(T(1)-T1s)/dx^2+(T(2)-T(1))/dx^2);
dTdt(n)=alpha*(-(T(n)-T(n-1))/dx^2+(T2s-T(n))/dx^2);
T=T+dTdt*dt;
plot(x,T,'LineWidth',3)
hold on
axis([0 L 0 50])
xlabel('Distance(m)')
ylabel('Temperature(\circC)')
%pause(0.1)
end
hold off
その他の回答 (2 件)
Walter Roberson
2023 年 10 月 12 日
Lvals = [500, 750, 900, 950, 1000, 1050, 1100, 1500];
numL = numel(Lvals);
N = 100;
dx = L/(N-1);
T = zeros(N,numL);
Tb = 300;
for Lidx = 1 : numL
L = Lvals(Lidx);
for j=1:1:k
T(1,Lidx) = Tb;
for i=2:1:N-1
T(i,Lidx) = (T(i+1,Lidx)+T(i-1,Lidx))/2;
end
T(N,Lidx) = T(N-1,Lidx);
end
end
surf(Lvals, 1:N, T);
xlabel('L');
ylabel('N');
zlabel('T');
However, the only point in the code in which you use L is to calculate dx and you never use dx, so the value of L does not affect the output in any way.
2 件のコメント
Walter Roberson
2023 年 10 月 17 日
I recommend that you study this pattern for iterating over values that are not consecutive integers (that start with 0 or 1) -- that is:
create a vector holding the values, count how many there were, pre-allocate the output based on the count of values, then loop an index from 1 to the number of values; inside the loop, pull out the "current" value from the list of values into a variable, and do computations based on the variable; store the result of the iteration into the pre-allocated variable indexed by the current loop index.
When you use this pattern, the values to be looped over do not need to be integers, do not need to be consecutive, do not need to be unique.
Do this even if you have something like
for L = 1:.1:5
out((L-.9)*10, :) = something
end
the calculation of the row index based upon current value is likely to go wrong, not producing exact integers for the indices.
参考
カテゴリ
Help Center および File Exchange で Particle & Nuclear Physics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!