Reputation: 9190
I have an array in Matlab that is updated for every time step: each row corresponds to a time and each column represents a temperature at a certain radius from the center. It would also be handy if a color gradient could be applied to the plot using the meshgrid
and contourf
commands. So far, this is the Matlab code that I have, but I am not sure how to get the temperature into the plot and animate the change in temperature.
Tinf = 200; % ambient temperature
% where r1 = radius1, r2 = radius2, etc.
% t = time
% rows = time
% columns = radius
% r1 r2 r3 r4 r5
T = [98 105 110 118 128; % t=1
109 110 117 124 134; % t=2
110 118 120 130 144]; % t=3
r = 0.08; % radius of circle
rx = -r:0.01:r;
ry = r:-0.01:-r;
[x_coor, y_coor] = meshgrid(rx, ry);
radius = sqrt(x_coor.^2+y_coor.^2);
figure(1)
contourf(radius,'edgecolor','none')
I am trying to create a circular plot in Matlab that would show the temperature (color) at each radius and animate that temperature (change color) as it increases or decreases with time.
An example of such a plot at a certain time would be:
So column 1 in the T array corresponds to node 1 in the picture, column 2 corresponds to node 2, etc. Thus at time = 0 then node1 = 98, node2 = 105, node3 = 110, node4 = 118, node5 = 128; at time = 1 then node1 = 109, node2 = 110, node3 = 117, node4 = 124, node5 = 134; and so on.
Any suggestions to accomplish such a plot would be very helpful.
Upvotes: 2
Views: 2154
Reputation: 5073
Same as @Magla's nice answer but draws a single surface (not an overlay) allowing interpolation
T = [98 105 110 118 128;
109 110 117 124 134;
114 118 120 130 138];
Rmax = 30;
[x,y,z] = sphere(100);
x=x*Rmax;
y=y*Rmax;
rxy2 = x.^2+y.^2;
r = [0 10 20 30];
r2 = r.^2;
figure('Color', 'w');
for ind_t = 1:size(T,1)
for ii = 1:length(r2)-1
ir_find = find(rxy2<=r2(ii+1) & rxy2>r2(ii));
z(ir_find) = T(ind_t,ii);
end
hax = axes('Position',[0 0 1 1]);
h = surf(x,y,z) % sphere centered at origin
shading interp
set(h, 'EdgeColor', 'None');
view(0,90);
axis equal;
set(hax, 'Visible', 'Off', 'CLim', [min(T(:)) max(T(:))]);
pause(0.5);
end
edit
Rewrote to use meshgrid and to use the particular radii etc of interest. Make sure to adjust r_res to a value you find adequate.
T = [98 105 110 118 128;
109 110 117 124 134;
114 118 120 130 138];
%---------------------------------------
r = 0.08; % radius of circle
r_res = 0.0005;
rx = -r:r_res:r;
ry = rx;
[x, y] = meshgrid(rx, ry);
rxy2 = x.^2+y.^2;
z=ones(size(rxy2))*NaN;
%---------------------------------------
Nshells = size(T,2);
r = [0:1/Nshells:1]*r;
r2 = r.^2;
figure('Color', 'w');
colormap hot
for ind_t = 1:size(T,1)
for ii = 1:Nshells
ir_find = find(rxy2<=r2(ii+1) & rxy2>r2(ii));
z(ir_find) = T(ind_t,ii);
end
hax = axes('Position',[0 0 1 1]);
h = surf(x,y,z) % sphere centered at origin
shading interp
set(h, 'EdgeColor', 'None');
view(0,90);
axis equal;
set(hax, 'Visible', 'Off', 'CLim', [min(T(:)) max(T(:))]);
pause(0.5);
end
Upvotes: 2
Reputation: 7751
Here is a solution that makes use of sphere
. sphere
generates the matrices x
and y
that are multiply by a decreasing radius r
, and matrix z
that is reduced to a single value (a sphere becomes a disk). z
is multiplied by the temperature and disks are plotted on top of each other. Colors depend on the min
and max
of the whole input matrix. Animation is done with pause
.
T = [98 105 110 118 128;
109 110 117 124 134;
114 118 120 130 138];
[x,y,z] = sphere(100);
r = [50 40 30 20 10];
figure('Color', 'w');
for ind_t = 1:size(T,1)
hax = axes('Position',[0 0 1 1]);
for ii = 1:length(r)
h = surf(x*r(ii),y*r(ii),z*0+T(ind_t,ii)) % sphere centered at origin
set(h, 'EdgeColor', 'None');
hold on;
end
view(0,90);
axis equal;
set(hax, 'Visible', 'Off', 'CLim', [min(T(:)) max(T(:))]);
pause(0.5);
end
This gives
Upvotes: 1