To plot 1D temperature distribution plot versus lenght of the channel of the fin

1 次查看(过去 30 天)
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);

采纳的回答

Walter Roberson
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
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
Ashwini nanjunda
Ashwini nanjunda 2023-11-7
Sorry for the confusion. Try this with this piece of code.
I have taken L=6.3*10^-5; and T1s = 0; and T2s= 608. Now characteristics are coming in Straight lines. I tried changing n value and stepsize. Have a look into the code by changing these parameters and help me with this.

请先登录,再进行评论。

更多回答(2 个)

Walter Roberson
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 个评论
Ashwini nanjunda
Ashwini nanjunda 2023-10-17
Thank you for the help sir. searching for a conceptual part to vary the lengh of a fin and observe the temperature distribution..This gave an insight to approach the problem.
Walter Roberson
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.

请先登录,再进行评论。


Ashwini nanjunda
Ashwini nanjunda 2023-10-19
Hello sir. This piece of code can be used to plot Temperature Vs Length.Help me with this
Finding it difficult to debug error and correct it.
clc
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;
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;
figure(1)
plot(x,T,'LineWidth',3)
axis([0 L 0 50])
xlabel('Distance(m)')
ylabel('Temperature(\circC)')
pause(0.1)
end

类别

Help CenterFile Exchange 中查找有关 Particle & Nuclear Physics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by