Plot of nested for loop for thickness variable
1 view (last 30 days)
Show older comments
Hi MATLAB Community,
I am struggling to get my plot to include several different thicknesses in my plot. Any advice would be greatly appreciated. Like in the plot below, but with the thickness: 0.04 0.045 0.05 and one with 0.05 0.65 0.1 0.12.
clc
clearvars
close all
%--------------------------------------------------------------------------
k=55; %steel thermal conductivity (W/m/K)
dens=8000; %steel density (kg/m^3)
cp=500; % steel specific heat capacity (J/kg/K)
h_n=200;%Liquid Nitrogen convective heat transfer coefficient (W/m^2/K)
t_n=-150; %Nitrogen temperature (C)
t_a=20; % air temperature (C)
h_a=20; % not accurate (W/m^2/K)
t_i=t_a; % initial temperature (C)
t_target=-25; % (C)
%--------------------------------------------------------------------------
ck = 0;
b=.1; % full contact length
for thick=.04:.005:.05; %thickness
%--------------------------------------------------------------------------
del=.005;
dt=.4;
t_t=3000;
%--------------------------------------------------------------------------
results=[];
for len=.2:.2:2
l_LN=b/2;
l_a=(len-b)/2;
n1=round(l_LN/del+1);
n=round((l_LN+l_a)/del)+1;
m=round(thick/del)+1;
nt=round(t_t/dt)+1;
jb=sort([1,2:n1,n1+1:n-1,n,n+1:n:(m-2)*n+1,(m-1)*n+1,m*n,2*n:n:(m-1)*n,(m-1)*n+2:m*n-1]);
alfa=k/dens/cp;
t=alfa*dt/del^2;
%--------------------------------------------------------------------------
max_time_step=del^2/4/alfa/(1+h_n*del/k);
if dt>=max_time_step
disp(['Unstable! time step = ',num2str(dt), ' > max permissible time step =', num2str(max_time_step)])
return
end
t2t_target=t_t;
u=ones(nt,m*n)*t_i;
for i=1:nt-1
tinf=t_n;hlk=h_n*del/k;
u(i+1,1)=(1-4*t-2*t*hlk)*u(i,1)+2*t*(u(i,2)+u(i,n+1)+hlk*tinf);
for j=2:n1
u(i+1,j)=(1-4*t-2*t*hlk)*u(i,j)+t*(u(i,j-1)+u(i,j+1)+2*u(i,j+n)+2*hlk*tinf);
end
tinf=t_a;hlk=h_a*del/k;
for j=n1+1:n-1
u(i+1,j)=(1-4*t-2*t*hlk)*u(i,j)+t*(u(i,j-1)+u(i,j+1)+2*u(i,j+n)+2*hlk*tinf);
end
u(i+1,n)=(1-4*t-4*t*hlk)*u(i,n)+2*t*(u(i,n-1)+u(i,2*n)+2*hlk*tinf);
for j=n+1:n:(m-2)*n+1
u(i+1,j)=(1-4*t)*u(i,j)+t*(u(i,j-n)+2*u(i,j+1)+u(i,j+n));
end
j=(m-1)*n+1;
u(i+1,j)=(1-4*t-2*t*hlk)*u(i,j)+2*t*(u(i,j+1)+u(i,j-n)+hlk*tinf);
u(i+1,m*n)=(1-4*t-4*t*hlk)*u(i,m*n)+2*t*(u(i,m*n-1)+u(i,(m-1)*n)+2*hlk*tinf);
for j=2*n:n:(m-1)*n
u(i+1,j)=(1-4*t-2*t*hlk)*u(i,j)+t*(u(i,j-n)+u(i,j+n)+2*u(i,j-1)+2*hlk*tinf);
end
for j=(m-1)*n+2:m*n-1
u(i+1,j)=(1-4*t-2*t*hlk)*u(i,j)+t*(u(i,j-1)+u(i,j+1)+2*u(i,j-n)+2*hlk*tinf);
end
for j=1:m*n
if ~ismember(j,jb)
u(i+1,j)=(1-4*t)*u(i,j)+t*(u(i,j-1)+u(i,j+1)+u(i,j-n)+u(i,j+n));
end
end
if u(i+1,(m-1)*n+1)<=t_target
disp(['Time to reach @ T = ', num2str(t_target), ' C = ' num2str((i+1)*dt), 's'])
u(i+2:nt,:)=[];
break
end
end
results=[results;len,(i+1)*dt]
end
figure
plot(results(:,1),results(:,2))
hold on
plot(results(:,1),results(:,2))
title('Time to Reach vs. thickness')
ck = ck+1;
legend_string{ck} = ['b = ',num2str(b),' m; t = ',num2str(thick),' m' ];
legend(legend_string')
xlabel('b (m)')
ylabel('Time to T_{target} (s)')
grid minor
end
hold off
0 Comments
Accepted Answer
G A
on 1 Jul 2021
Place figure(1) with a number before for-loop and use plot(results(:,1),results(:,2)) only once.
If you use figure() without any number, then three different figures will be created
figure(1)
clf
hold on
for thick=.04:.005:.05; %thickness
...
for len=.2:.2:2
...
end
plot(results(:,1),results(:,2))
title('Time to Reach vs. thickness')
...
end
hold off
5 Comments
More Answers (0)
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!