How can I get the value by choosing 't' and 'x'? ,How should I set the graph range?

조회 수: 1 (최근 30일)
Seung Ho Kim
Seung Ho Kim 2021년 5월 4일
답변: VBBV 2022년 9월 26일
L = 30;
n = 10;
T0=500;
T1s=300;
T2s=400;
dx=L/n;
alpha=0.000085;
t_final=4800;
dt=0.1;
x = dx/2:dx:L-dx/2;
T = ones(n,1)*T0;
dTdt=zeros(n,1);
t = 0:dt:t_final;
for j = 1:length(t)
for i = 2:n-1
dTdt(i)=alpha*(-(T(i)-T(i-1))/dx^2+(T(i+1)-T(i))/dx^2);
end
dTdt(1)=alpha*(-(T(1)-T1s)/dx^2+(T(2)-T(1))/dx^2);
dTdt(n)=alpha*(-(T(n)-T(n-1))/dx^2+(T2s-T(n))/dx^2);
T=T+dTdt*dt;
figure(1)
plot(x,T)
xlabel('distance(n)')
ylabel('Temperature(\circC)')
pause(1)
end

답변 (2개)

J Chen
J Chen 2021년 5월 4일
Move the plot command outside the loop. Store the calculation at each time step in a buffer. For example:
hist = nan(n,length(t));
for
.
.
hist(:,j) = T;
end
plot(:,400)

VBBV
VBBV 2022년 9월 26일
L = 30;
n = 10;
T0=500;
T1s=300;
T2s=400;
dx=L/n;
alpha=0.000085;
t_final=4800;
dt=300; % use a coarse timestep for faster loop execuetion
x = dx/2:dx:L-dx/2;
T = ones(n,1)*T0;
dTdt=zeros(n,1);
t = 0:dt:t_final;
for j = 1:length(t)
hold on % use a hold on command
for i = 2:n-1
dTdt(i)=alpha*(-(T(i)-T(i-1))/dx^2+(T(i+1)-T(i))/dx^2);
end
dTdt(1)=alpha*(-(T(1)-T1s)/dx^2+(T(2)-T(1))/dx^2);
dTdt(n)=alpha*(-(T(n)-T(n-1))/dx^2+(T2s-T(n))/dx^2);
T=T+dTdt*dt;
%figure(1)
plot(x,T)
end
xlabel('distance(n)'); ylabel('Temperature(\circC)')
Use a hold on command at the beginning of first for loop. Assume a coarser time step for faster loop execution.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by