Linear plot to contour plot

4 views (last 30 days)
Asger
Asger on 18 Sep 2012
Hello, I'm creating a temperature profile of a cylinder, and got a range of temperatures to different radiuses, like shown in the picture below. Kelvin on the y-axis and radius [m] on the x-axis.
So i did this with the plot(r,T) command, r-vector having the radius and T-vector my corresponding temperatures. Now, i want to make this into a contour plot, so i can see it like circle, to show it like a real cross-cut of my cylinder. (I know it might not be up to much practical use, but i want to learn how to do it)
I've tried to read about contourplots, meshgrids and so on, but just can't seem to figure out a way to do this. Could anyone explain me how to transform a line plot into a contour plot with circular level curves.

Accepted Answer

Asger
Asger on 19 Sep 2012
Edited: Asger on 19 Sep 2012
OK, I found a solution, a bit inspired by José-Luis' answer, but i was not satisfied with the way he did it, as i didn't get any indepth knowledge of contourplots, which is what i will need to use in my further work with a more complex model. If anyone is interested here is my solution.
As a background story i got some different temperatures in a few layers, T1,T2,T3, and also different temperature gradients dT1,dT2 through each layer depending on their thermal conductivity.
Now i make a meshgrid for x and y coordinates, calculate the corresponding radius in each array-element. I have 2 for loops that goes through each r(m,n) and calculates the temperature. Basicly it's temperature, T, as a function of the radius, r.
% Make the coordinatesystem that fits with the radius.
[x_coor,y_coor] = meshgrid(-r3:0.001:r3,r3:-0.001:-r3);
r = sqrt(x_coor.^2+y_coor.^2); % Corresponding radius
% Get ready for the loop
m = 1;
n = 1;
T_matrix = zeros(size(r));
dT2 = (T2-T1)/(r2-r1);
dT3 = (T3-T2)/(r3-r2);
for m = 1:length(r)
for n = 1:length(r)
if r(m,n) <= r1 % layer 1
T_matrix(m,n) = T1;
elseif r(m,n) < r2 % layer 2
T_matrix(m,n) = T1 + (r(m,n)-r1)*dT2;
elseif r(m,n) <= r3 % layer 3
T_matrix(m,n) = T2 + (r(m,n)-r2)*dT3;
else % if none of these layers, it must be outside
T_matrix(m,n) = Te; % environmental temperature
end
end
end
% Draw the contour
contourf(x_coor,y_coor,T_matrix,80,... % 80 colours
'edgecolor','none'); % no edge
% Draw the cable boundaries
hold all
phi = 0:0.01:2*pi; x_circ = r1*cos(phi); y_circ = r1*sin(phi);
plot(x_circ,y_circ,'Color','black','LineWidth',2)
phi = 0:0.01:2*pi; x_circ = r2*cos(phi); y_circ = r2*sin(phi);
plot(x_circ,y_circ,'Color','black','LineWidth',2)
phi = 0:0.01:2*pi; x_circ = r3*cos(phi); y_circ = r3*sin(phi);
plot(x_circ,y_circ,'Color','black','LineWidth',2)
And this gives me a contourplot that looks like this: http://i.imgur.com/oZIq5.png
Thanks for the help.

More Answers (2)

Jürgen
Jürgen on 18 Sep 2012
Hi,
to make a contour plot you need two dimensions, since you only have r the radius it won't work
You need the (x,y) coordinates of the cilinder or (r,angle) is you want to use polar coord. Then you could try a polar plot
once you have the coord in two dimensions use meshgrid en contour
regardsJ
  1 Comment
Asger
Asger on 18 Sep 2012
Yes, i think i can transfer my radius into (x,y)-coordinates by polarcoordinates, but how do i get my temperature T (which will be the height z of the contour plot) to follow into the right element in the array?

Sign in to comment.


José-Luis
José-Luis on 18 Sep 2012
Edited: José-Luis on 18 Sep 2012
I don't think a polar plot would do the trick, you could use patches:
data_length = 10;
%The values you want to plot:
your_vals = rand(data_length,1);
x = (0:data_length - 1)'; %Coordinates, gotta start somewhere
y = 0.*x;
coord_mat = [x y];
numColors = 64; %How many colors you want
color_array = autumn(numColors); %Colormap, could be something else
%Getting color index:
minVal = min(your_vals);
maxVal = max(your_vals);
your_idx = (your_vals - minVal) ./ (maxVal - minVal); %Linearly mapping the colormap
your_idx = round((numColors - 1) * your_idx + 1);
R = @(x)[cos(x) -sin(x); sin(x) cos(x)]; %Rotation matrix
numVals = 0;
newVec = [];
%Rotating your data, do less intervals if you want less patches
for ii = 0:pi/20:2*pi
newVec = [newVec;coord_mat*R(ii)];
numVals = numVals + 1;
end
%Cushioning so as not to get out of bounds
newVec = [newVec repmat(color_array(your_idx,:),numVals,1)];
newVec = [newVec ; newVec(1:data_length,:)];
numVals = numVals + 1;
%Patching
for ii = 1:numVals-1
for jj = 1:data_length-1
idx_vec = [(ii-1)*data_length + jj;...
(ii-1)*data_length + jj + 1;...
ii*data_length + jj + 1;...
ii*data_length + jj;...
];
p = patch('Faces',idx_vec','Vertices',newVec(:,[1 2]));
set(p,'FaceColor','interp',...
'FaceVertexCData',newVec(:, [3 4 5]),...
'EdgeColor','none')
hold on;
end
end
You could probably simplify the loop...
Or reduce the number of patches, e.g. patching circle sections

Categories

Find more on Contour Plots 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!