how to plot a temperature distribution of a solid cylinder with heat generation

61 views (last 30 days)
so I am trying to plot the temperature distribution of a solid cylinder with heat generation in MATLAB. I have to use a for loop as per insctrutions of the assignment. I wrote this code based on the given information and hand calculated equations for the the surface temperature (Ts) and temperature destribution equation (Tr). The code is outputting a graph but its not graphing anything its just blank. Anyone know how to get it to plot the temperature distribution?
my code:
format long
Ti = 20; %temp infinity (C)
h = 75; %convection coeff (w/m2k)
q_g = 1200; %heat gen (w/m3)
k = 5.2; %thermal conduc (w/mk)
r0 = 0.1; %surfaceradius (m)
r1 = 0; %center (m)
Ts = Ti+((q_g*r0)/(2*h));
for i=0:0.01:r0
Tr = ((-q_g*(r1^2))/(4*k))+Ts+((q_g*(r0^2))/(4*k));
plot (i,Tr,'b')
title('Part 1: Original Temperature Distribution','fontsize',20)

Answers (2)

ChrisR on 29 Oct 2021
Here are a few comments:
  1. You are plotting one point at a time, but with that plot statement, you won't see any points. You could try plot(i,Tr,'bo').
  2. If you change the plot statement as in comment #1, then add "hold on". Otherwise, you will see only the last point.
  3. Your calculation of Tr does not depend on i. That means it will keep producing the same value (21.3769). Should the radius be involved in your calculation?
Another approach is to write
r = 0:0.01:r0;
for i = 1:length(r)
Tr(i) = ...
The equation for Tr will likely depend on the radius r(i).

Bradley Davis
Bradley Davis on 17 Jun 2022
I tried this, and got a nice plot:
% establish a radius array from core to surface
r_d = linspace(r0,rs,100);
% Preallocate an array for the temperature distribution at r
t_rad = zeros(size(r_d));
% Loop through r and solve for temperature
for i = 1:length(t_rad)
% The incremental radial change matters, hence (r_d(i) - r0)^2
t_rad(i) = ((-p_vol * ((r_d(i)-r0)^2))/(4*k)) + tsurf + ((p_vol * (rs^2))/(4*k));
hold on
% create second plot for comparison


Find more on Thermal Analysis 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!