changing timestamp t and plotting the results.

1 次查看(过去 30 天)
I managed to code when t=0, but i still need to code for when t=1000 and when t=2000, as wellas plotting each of the graphs.
In which part of the code do i add so that i change t?
n=5;
dt=1000;
k=15;
rho=8055;
Cp=480;
deltax=1.6;
x=linspace(-0.8,0.8,n);
T=zeros(1,5); T(1,1)=40; T(1,5)=40;
figure
plot(x,T)
title('Temperature distribution at t=0')
xlabel('x')
ylabel('Temperature')
hold on;
  2 个评论
Torsten
Torsten 2022-6-28
Proceed as described in your assignment:
n = 5;
dt = 1000;
L = 0.8;
k = 15;
rho = 8055;
Cp = 480;
x = linspace(-L,L,n);
deltax = x(2)-x(1);
T=zeros(1,5); T(1,1)=40; T(1,5)=40;
plot(x,T)
hold on
T_old = T;
T(2:end-1) = T_old(2:end-1) + k/(rho*Cp) * dt * (T_old(3:end)-2*T_old(2:end-1)+T_old(1:end-2))/(2*deltax);
plot(x,T)
T_old = T;
T(2:end-1) = T_old(2:end-1) + k/(rho*Cp) * dt * (T_old(3:end)-2*T_old(2:end-1)+T_old(1:end-2))/(2*deltax);
plot(x,T)
Ellie Matlab
Ellie Matlab 2022-6-28
编辑:Ellie Matlab 2022-6-28
I entered this code for the first 3 timestamps, how can i include a for loop that loops from t=0 to t=350000 to plot the temperature distribution at every timestep and use the pause command to observe the changes? The Temperaure is at 0degrees celsius everywhere else.
n=5;
dt=1000;
k=15;
rho=8055;
Cp=480;
deltax=1.6;
x=linspace(-0.8,0.8,n);
deltax = x(2)-x(1);
T=zeros(1,5); T(1,1)=40; T(1,5)=40;
figure
plot(x,T)
title('Temperature distribution for first 3 timestamps')
xlabel('x')
ylabel('Temperature')
hold on;
T_old = T;
T(2:end-1) = T_old(2:end-1) + k/(rho*Cp) * dt * (T_old(3:end)-2*T_old(2:end-1)+T_old(1:end-2))/(deltax.^2);
plot(x,T)
hold on
T_old = T;
T(2:end-1) = T_old(2:end-1) + k/(rho*Cp) * dt * (T_old(3:end)-2*T_old(2:end-1)+T_old(1:end-2))/(deltax.^2);
plot(x,T)
%%for multiple timestamps
n=5;
dt=1000;
k=15;
rho=8055;
Cp=480;
deltax=1.6;
x=linspace(-0.8,0.8,n);
deltax = x(2)-x(1);
T=zeros(1,5); T(1,1)=40; T(1,5)=40;
t = 0;
dt = 1000;
for c = 1:t
for r = 1:t
T(r,c) = 1/(r+c-1);
pause(1000);
end
end
figure
plot(x,T)
title('Temperature distribution for first multiple timestamps')
xlabel('x')
ylabel('Temperature')

请先登录,再进行评论。

回答(1 个)

Dyuman Joshi
Dyuman Joshi 2022-6-29
I am not sure if pause works here on the online/live editor, but it works nicely offline.
%building on Torsten's code
n=5;
dt=1000;
k=15;
rho=8055;
Cp=480;
x=linspace(-0.8,0.8,n);
deltax = x(2)-x(1);
T=zeros(1,5); T(1,1)=40; T(1,5)=40;
figure
plot(x,T)
xlabel('x')
ylabel('Temperature')
hold on;
for i=0:dt:35000
T_old = T;
T(2:end-1) = T_old(2:end-1) + k*dt/(rho*Cp)*(T_old(3:end)-2*T_old(2:end-1)+T_old(1:end-2))/(deltax.^2);
plot(x,T)
pause(0.1) %pausing 0.1 seconds between each plot
end

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by