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 CenterFile Exchange에서 Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by