I need to change the boundary condition in the code below

4 views (last 30 days)
% Heat diffusion in one dimensional wire within the explicit FTCS method
clear;
% Parameters to define the heat equation and the range in space and time
D = 1*10^-4; % Heat Conductivity parameter in m2/s
K = 1*10^-4; % Heat Conductivity parameter in s^-1
L = 5.; % Total length in m
T = 43200.; % Final Time
% Parameters needed to solve the equation within the explicit method
Nt = 7200; % Number of time steps
Dt = T/Nt; % Time step
Nx = 51; % Number of space steps in m
Dx = L/(Nx-1); % Space step
b = 1-(2*D*Dt/Dx^2)- K*Dt; % beta parameter in the finite- difference implementation
% Remember that the FTCS method is stable for b=<1
disp(b);
% The initial condition: the initial temperature of the pipe
for i = 1:Nx+1
x(i) = (i-1)*Dx; % we also define vector x, due to the space discretization
u(i,1) = sin (pi*x(i));
end
% Boundary conditions: the temperature of the pipe at the boundaries at any time
for k =1:Nt+1
u(1,k) = 1.;
u(Nx+1,k) = 1.;
t(k) = (k-1)*Dt; % we also define vector t, due to the time discretization
end
% Implementation of the explicit FTCS method
for k =1:Nt % Time Loop
for i=2:Nx; % Space Loop
u(i,k+1) =u(i,k) + D*Dt/(Dx)^2*(u(i+1,k)-2*u(i,k)+u(i-1,k)); % Implementation of the FTCS Method
end
end
%Graphical representation of the temperature at different selected times
figure(1)
plot(x,u(:,1),'-b',x,u(:,round(Nt/100)),'--g',x,u(:,round(Nt/10)),':b',x,u(:,Nt),'-.r')
Title('Temperature of the pipe within the explicit FTCS method')
xlabel('X')
ylabel('T')
figure(2)
mesh(t,x,u)
title('temperature of the pipe within the explicit FTCS method')
xlabel('t')
ylabel('x')
Boundary and Initial Conditions: 𝐶|𝑥=0 = 1 mg/L, 𝑑𝐶 𝑑𝑥 |𝑥=𝐿 = 0, 𝐶|𝑡=0 = 1 mg/L
  3 Comments
Naveen Krish
Naveen Krish on 3 Apr 2022
So the above one is right? I don't need to change anything right?
Torsten
Torsten on 4 Apr 2022
If you want to solve
dC/dt = D * d^2C/dx^2
C(0,t) = 1
dc/dx(L,t) = 0
C(x,0) = 1
then the solution is C(x,t) = 1.
No need to simulate anything.

Sign in to comment.

Answers (1)

Kayemba Luwaga
Kayemba Luwaga on 22 Jul 2023
if true
  % code
% Heat diffusion in one dimensional wire within the explicit FTCS method
clear;
% Parameters to define the heat equation and the range in space and time
D = 1*10^-4;               % Heat Conductivity parameter in m2/s
K = 1*10^-4;               % Heat Conductivity parameter in s^-1
L = 5.;                    % Total length in m
T = 43200.;                   % Final Time
% Parameters needed to solve the equation within the explicit method
Nt = 7200;              % Number of time steps
Dt = T/Nt;              % Time step
Nx = 51;               % Number of space steps in m
Dx = L/(Nx-1);              % Space step
b = 1-(2*D*Dt/Dx^2)- K*Dt; % beta parameter in the finite- difference implementation
% Remember that the FTCS method is stable for b=<1
disp(b);
% The initial condition: the initial temperature of the pipe
for i = 1:Nx+1
      x(i) = (i-1)*Dx; % we also define vector x, due to the space discretization
      u(i,1) = sin (pi*x(i));
end
% Boundary conditions: the temperature of the pipe at the boundaries at any time
for k =1:Nt+1
    u(1,k) = 1.;  %% Edit: Lower BC at Temp = 1
    u(Nx+1,k) = 100.;   %% Edit: Upper BC at Temp = 100
    t(k) = (k-1)*Dt; % we also define vector t, due to the time discretization
end
% Implementation of the explicit FTCS method
for k =1:Nt             % Time Loop
  for i=2:Nx;         % Space Loop
      u(i,k+1) =u(i,k) + D*Dt/(Dx)^2*(u(i+1,k)-2*u(i,k)+u(i-1,k)); % Implementation of the FTCS Method
  end
end
%Graphical representation of the temperature at different selected times
figure(1)
plot(x,u(:,1),'-b',x,u(:,round(Nt/100)),'--g',x,u(:,round(Nt/10)),':b',x,u(:,Nt),'-.r')
title('Temperature of the pipe within the explicit FTCS method')
xlabel('X')
ylabel('T')
figure(2)
mesh(t,x,u)
% plot(t,u)
title('temperature of the pipe within the explicit FTCS method')
xlabel('t')
ylabel('x')
end
  1 Comment
Kayemba Luwaga
Kayemba Luwaga on 22 Jul 2023
Check lines 23 and 24 for editing the temperature boundary conditions/values, Hope it's what you want Cheers!

Sign in to comment.

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!