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 Center および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by