How do implement the separation variable method of heat equation?

7 次查看(过去 30 天)
How to implement the separation variable method of heat equation by using the same value of L,alpha,t_final,n,t0,t1s and t2s?
L=20;
alpha=0.23;
t_final=60;
n=20;
T0=20;
T1s=100;
T2s=0;
dx=L/n;
dt=2;
x=dx/2:dx:L-dx/2;
t = 0:dt:t_final;
nt = length(t);
T = zeros(n, nt);
T(:,1) = T0;
for j=1:nt-1
dTdt=zeros(n,1);
for i=2:n-1
dTdt(i) = alpha*(T(i+1,j)+T(i-1,j)-2*T(i,j))/dx^2;
end
dTdt(1) = alpha*(T(2,j)+T1s-2*T(1,j))/dx^2;
dTdt(n) = alpha*(T2s+T(n-1,j)-2*T(n,j))/dx^2;
T(:,j+1) = T(:,j) + dTdt*dt;
end
disp(T)
figure(1)
mesh(x,t,T.')
xlabel('Position (x)')
ylabel('Time (seconds)')
zlabel('Temparature, U(x,t)')

回答(1 个)

Anurag Ojha
Anurag Ojha 2024-10-3
Hey Deck
The provided code is a finite difference method (explicit forward-time, centered-space) for solving the one-dimensional heat equation. As per my understanding you're asking how to implement the separation of variables method instead, while keeping the same values for parameters.
The method of separation of variables works by assuming that the solution can be written as a product of two functions, one dependent only on x and the other only on t:
Below is a MATLAB implementation of the separation of variables method, with the same parameters you've provided. Kindly make changes in the code as per your use case (if needed)
L = 20;
alpha = 0.23;
t_final = 60;
n = 20;
T0 = 20;
T1s = 100;
T2s = 0;
dx = L / n;
dt = 2;
x = linspace(0, L, n);
t = 0:dt:t_final;
nt = length(t);
% Precompute the Fourier modes
T = zeros(n, nt);
T(:, 1) = T0;
% Fourier expansion coefficients
for k = 1:n
lambda_k = (k * pi / L)^2;
Xk = sin(k * pi * x / L);
Theta_k = exp(-lambda_k * alpha * t);
% Superimpose the solutions
for j = 1:nt
T(:, j) = T(:, j) + (T1s - T2s) * Xk' * Theta_k(j);
end
end
% Plot the result
figure(1)
mesh(x, t, T.')
xlabel('Position (x)')
ylabel('Time (seconds)')
zlabel('Temperature, U(x,t)')

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by