Plot change in heat for a x y position as a function of time

10 views (last 30 days)
Hello,
I recently created a code to solve 2-d transient conduction for a plate with length = 1 and width = 1. To do so I created a 3-d matrix to account for x and y spatial steps as well as temporal timesteps. I now want to plot temperature as a function of time for a specific x y position, as a 2-d plot, so I can see how temperature varies across time for a specific position on the plate. I've tried using certain functions including the plot3 function:
plot3(Temp_new(.3,.3,:))
but I get the following error message:Not enough input arguments. Since the plate is 1x1 the locations will also be non-integers. Is there a function that will allow me to plot my data and deal with the non-integer issue? I've inlcuded the code i used to generate the data I wish to plot.
clc
clear
%Declare the domain
Lx=1; %plate length
Ly=1; %plate width
x_nodes=10;
y_nodes=10;
dx=Lx/x_nodes;%x direction step 0.1
dy=Ly/y_nodes;%y direction step 0.1
t_total=1000;
t_nodes=1000;
dt=t_total/t_nodes; % time step 0.1 seconds
%delcare constants and define alpha and fourier number
k=385;
C=377;
d=8960;
alpha=(k/(d*C)); %alpha= 1.139754642e-4
F=(alpha*dt)/(dx*dy); %stability criterion 1-4*F>0 , F<.25
%Initialize temperature
Temp=zeros(x_nodes+1,y_nodes+1,t_nodes+1);
Temp_new=zeros(x_nodes+1,y_nodes+1,t_nodes+1);
%Initial condition- Temperature
Temp(:,:,1)=225;
Temp_new(:,:,1)=225;
%Declare the boundary condition
T_bot = 500;
T_top = 0;
T_left = 0;
T_right = 0;
%top
Temp(x_nodes+1,:,:)=T_top;
Temp_new(x_nodes+1,:,:)=T_top;
%left
Temp(:,1,:)=T_left;
Temp_new(:,1,:)=T_left;
%right
Temp(:,y_nodes+1,:)=T_right;
Temp_new(:,y_nodes+1,:)=T_right;
%bottom
Temp(1,:,:)=T_bot;
Temp_new(1,:,:)=T_bot;
%Declare other variables
p=1; %counter
tolerence=0.001;
MaxError(p)=1;
MinError(p)=1;
while MaxError(p)>=tolerence || MinError(p)>=tolerence
for i=2:x_nodes % looping
for j=2:y_nodes
for n=1:t_total
Temp_new(i,j,n+1)=(F*(Temp(i+1,j,n)+Temp(i-1,j,n)+Temp(i,j+1,n)+Temp(i,j-1,n)))+((1-4*F)*Temp(i,j,n));
end
end
end
p=p+1; % update p
% calculate error
MaxError(p)=max(abs(Temp_new(:)-Temp(:)));
MinError(p)=min(abs(Temp_new(:)-Temp(:)));
Temp=Temp_new; % update T
end
figure(1)
contourf(Temp_new(:,:,10))
xlabel('X')
ylabel('Y')
zlabel('t')
h = colorbar;
set(get(h,'title'),'string','Temperature (C))');
hold on
figure(2)
contourf(Temp_new(:,:,100))
xlabel('X')
ylabel('Y')
zlabel('t')
h = colorbar;
set(get(h,'title'),'string','Temperature (C))');
hold on
figure(4)
contourf(Temp_new(:,:,500))
xlabel('X')
ylabel('Y')
zlabel('t')
h = colorbar;
set(get(h,'title'),'string','Temperature (C))');
hold on
figure(5)
contourf(Temp_new(:,:,1000))
xlabel('X')
ylabel('Y')
zlabel('t')
h = colorbar;
set(get(h,'title'),'string','Temperature (C))');
figure(7)
plot3(Temp_new(4,4,:))
hold on
%set(gca, 'YDir','reverse')
% xlabel('X')
% ylabel('Y')
% zlabel('t')
% h = colorbar;
% set(get(h,'title'),'string','Temperature (C))');
figure(6)
semilogx(MaxError)
hold on
semilogx(MinError)
xlabel('Iterations')
ylabel('Error')
  3 Comments
AbeSal
AbeSal on 15 Mar 2020
Is anyone able to provide the appropriate function and guidance on how to set it up in Matlab?
darova
darova on 15 Mar 2020
Please attach orginal equation and the difference scheme you used

Sign in to comment.

Answers (1)

Sindar
Sindar on 15 Mar 2020
Look at your Temp variable:
Temp=zeros(x_nodes+1,y_nodes+1,t_nodes+1);
if you want to know the temperature for a specific location, you can extract it easily (here, the bottom left):
temp00 = Temp(1,1,:);
you also need the time:
t=dt*[0:t_nodes-1];
then, to plot it, simply use plot():
plot(t,temp00)
does that give you the desired plot?
  2 Comments
AbeSal
AbeSal on 16 Mar 2020
Unfortunately, it returns the following error message:
Error using plot.
Data cannot have more than 2 dimensions.
I read some literature regarding the squeeze function and entered the following into matlab:
plot(squeeze(Temp_new(3,3,:)))
It allowed me to create a 2d plot form a 3d matrix, but I'm curious to confirm if this is the appropriate choice to resolve my issue.
Sindar
Sindar on 16 Mar 2020
Ah, yes, sorry I always forget the need to squeeze until I get that error. That is the correct usage. You could add that either to my definition of temp00, or to the final plot. If you want to plot vs actual time (you're currently plotting vs index), just add t:
t=dt*[0:t_nodes-1];
plot(t,squeeze(Temp_new(3,3,:)))

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!