Clear Filters
Clear Filters

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

1 view (last 30 days)
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);

Accepted Answer

Walter Roberson
Walter Roberson on 19 Oct 2023
Moved: Walter Roberson on 19 Oct 2023
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 Comments
Walter Roberson
Walter Roberson on 6 Nov 2023
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 on 7 Nov 2023
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.

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 12 Oct 2023
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 Comments
Ashwini nanjunda
Ashwini nanjunda on 17 Oct 2023
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 on 17 Oct 2023
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.

Sign in to comment.


Ashwini nanjunda
Ashwini nanjunda on 19 Oct 2023
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

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!