changing timestamp t and plotting the results.

조회 수: 4 (최근 30일)
Ellie Matlab
Ellie Matlab 2022년 6월 28일
답변: Dyuman Joshi 2022년 6월 29일
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