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

1 次查看(过去 30 天)
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 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by