Question about Forward time center space 1D matlab code

2 ビュー (過去 30 日間)
Murad Alzahrani
Murad Alzahrani 2020 年 6 月 9 日
コメント済み: darova 2020 年 6 月 12 日
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 件のコメント
darova
darova 2020 年 6 月 12 日
I don't understand the question. What lines you want to see?

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by