Plot temperature distribution within a sphere for 1D conduction problem

6 次查看(过去 30 天)
Hey there!
I've developed a MATLAB code using the PDEPE tool to simulate 1D conduction within a sphere. My aim is to visualize the temperature distribution within the sphere over time. To achieve this, I've put together a script that calculates the radius using Cartesian coordinates (r = sqrt(x^2 + y^2)) and plots the corresponding temperature. The resulting 2D plot resembles a disk within a square, representing the sphere. While the code runs smoothly, I'm facing an issue with the visualization: I'd like to exclude coloring the outer parts of the sphere, which represent the surrounding air.
Any suggestions or any other simple way on how to achieve this would be greatly appreciated!
Cooling_Time = 5;
nodes_r = 201;
r = linspace(0,r0,nodes_r); % Sets space nodes
nodes_t = 1+Cooling_Time*360;
t = linspace(0,Cooling_Time*3600, nodes_t); % Sets time steps
x = linspace(-r0, r0, nodes_r);
y = linspace(-r0, r0, nodes_r);
T = pdepe(m, @Heatpde, @HeatIC, @HeatBC,x,t)
temperature = zeros(length(y), length(x));
figure
for k = 1:nodes_t
for i = 1:length(x)
for j = 1:length(y)
r_calcule = sqrt(x(i)^2 + y(j)^2);
indice_r = find(r <= r_calcule, 1, 'last');
if isempty(indice_r)
temperature(j, i) = 0;
else
temperature(j, i) = T(k,indice_r);
% temperature(j, i) = T(nodes_t,indice_r);
end
end
end
% Displaying the temperature contour
contourf(x, y, temperature, 'linecolor','k');
xlabel('x');
ylabel('y');
title('Temperature Contour');
colormap jet;
colorbar;
axis equal;
set(gca, 'FontName', 'LM Roman 10', 'TickDir', 'out', 'box', 'on');
pause(1e-7)
end

采纳的回答

Zakaria
Zakaria 2024-4-16
编辑:Zakaria 2024-4-16
To solve the problem I have changed the colormap to hot and to add a condition that say if the calculated rcart is greater than the acual raduis r set temperature = 26 wich the max an is represented in white. However, it is not physicaly accurate.
So, I simply asssined a NaN to the temperatures of the rcart greater that the actual radius r, so that matlab ignores the NaN values.
figure
for k = 1:20:nodes_t
for i = 1:length(x)
for j = 1:length(y)
r_calcule = sqrt(x(i)^2 + y(j)^2);
indice_r = find(r <= r_calcule, 1, 'last');
if isempty(indice_r)
temperature(j, i) = 0;
elseif r_calcule > r
temperature(j, i) = NaN;
else
temperature(j, i) = T(k,indice_r);
% temperature(j, i) = T(nodes_t,indice_r);
end
end
end
% Displaying the temperature contour
contourf(x, y, temperature, 'linecolor','none');
xlabel('x');
ylabel('y');
title('Temperature Contour');
colormap jet;
colorbar;
axis equal;
set(gca, 'FontName', 'LM Roman 10', 'TickDir', 'out', 'box', 'on');
caxis([4 25])
pause(1e-7)
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by