Question about Forward time center space 1D matlab code
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi. I tried make code I can solve this problem in Forward time center space 1D:

As you see, but my code naver show me completely linea when plot the steady state temperature.
Code:
clear variables
close all
%1 Function parameters
N = 10;
Lx = 1;
dx = Lx/(N-1);
x = 0:dx:Lx;
alpha = 0.5; % To insure stability alpha = dt/dx^2 < = 1/2
% 2. Time vector
M = 100;
tf = 100;
dt = tf/(M-1);
t = 0:dt:tf;
% 4. Initial and boundary conditions
f = @(x) x; % initial cond. f(x)
g1 = @(t) 100; % boundary conditions g1(t) and g2(t)
g2 = @(t) 0;
% 5. Inilization of the heat equation
u = zeros(N,M);
u(:,1) = f(x);
u(1,:) = g1(t);
% 6. Implementation of the explicit method
for j= 1:M-1 % Time Loop
for i= 2:N-1 % Space Loop
u(i,j+1) = alpha*(u(i-1,j))+(1-2*alpha)*u(i,j) + alpha*u(i+1,j);
end
% Vectorize the inner for loop with this line to gain some speed.
% u(2:N-1,j+1) = alpha*(u(1:N-2,j))+(1-2*alpha)*u(2:N-1,j) + alpha*u(3:N,j);
% Insert boundary conditions for i = 1 and i = N here.
u(2,j+1) = alpha*u(1,j) + (1-2*alpha)*u(2,j) + alpha*u(3,j);
u(N,j+1) = 2*alpha*u(N-1,j)+(1-2*alpha)*u(N,j)+ 2*alpha*dx*g2(t);
end
% Plot results
figure
plot(x,u(1:end,1:30),'linewidth',2);
a = ylabel('Temperature');
set(a,'Fontsize',14);
a = xlabel('x');
set(a,'Fontsize',14);
a=title(['Using The Explicit Method - alpha =' num2str(alpha)]);
legend('Explicit soln')
set(a,'Fontsize',16);
xlim([0 1]);
grid;
figure
1 Kommentar
Antworten (0)
Siehe auch
Kategorien
Mehr zu Graphics Objects finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!