changing timestamp t and plotting the results.

4 views (last 30 days)
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 Comments
Torsten
Torsten on 28 Jun 2022
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 on 28 Jun 2022
Edited: Ellie Matlab on 28 Jun 2022
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')

Sign in to comment.

Answers (1)

Dyuman Joshi
Dyuman Joshi on 29 Jun 2022
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

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!