Filter löschen
Filter löschen

How to plot the function below in cylindrical coordinates?

1 Ansicht (letzte 30 Tage)
Kien Tah Goh
Kien Tah Goh am 30 Jan. 2015
Hello guys, I am new here. Thanks for sacrificing your valuable timing to answer my question =)
I have the following code:
function main = cylindrical()
clc; clear all; close all; clf;
global after_integration
syms k(r) r1 r2 T T_inner T_outer term
syms after_integration_value after_integration_r1 after_integration_r2
k(r) = r.^2; %user defined:thermal conductivity as a function of r
r1 = 1; %user defined:inner radius in meter
r2 = 2; %user defined:outer radius in meter
T_inner = 100; %user defined:temperature at inner radius,T(r1)
T_outer = 200; %user defined:temperature at outer radius,T(r2)
after_integration = int(1/(k(r).*r),r); %function of r(f(r)) obtained from integration
after_integration_r1 = subs (after_integration,r1); %value of f(r) when r=r1=inner radius
after_integration_r2 = subs (after_integration,r2); %value of f(r) when r=r2=outer radius
term = (T_outer - T_inner)/(after_integration_r2 - after_integration_r1);
r = r1:0.1:r2;
after_integration_value = subs (after_integration,r);
T = term.* after_integration_value + T_inner - term.*after_integration_r1
end
My question is:
I need to plot function T(bolded above) in cylindrical coordinates, sth like this
, the blue colour should be Tr1,and the red one be Tr2.
How should write write the rest of the code for the plotting part?
Thanks in advanced !!!

Antworten (3)

Michael Haderlein
Michael Haderlein am 30 Jan. 2015
You need to convert your polar coordinates to cartesian coordinates:
r=1:.1:2; %radius
theta=0:pi/32:2*pi; %some angle scale, change the increment for smoother plot
[R,THETA]=meshgrid(r,theta); %create the mesh
[x,y]=pol2cart(THETA,R); %transform to cartesian
TEMP=ones(length(theta),1)*T; %mesh of temperatures
figure, surf(x,y,TEMP,)
However, I'm pretty sure you should have a look on your code itself. I'm a bit too lazy to check your integration exactly, but I'm rather sure there's no need for symbolic variables here. Also, the ambiguous use of r makes the code more difficult to understand than necessary. Global variables should only be used if there's no other way around (at least that's my strategy to avoid problems in debugging) and I honestly doubt there's no way here (most likely, you can use function arguments or return it in the variable main which is not even used in your code). Also the line
clc; clear all; close all; clf;
is something you should think about (clf after close all?).
Hope these tips will help you to develop a good programming style.
Best regards,
Michael

Kien Tah Goh
Kien Tah Goh am 30 Jan. 2015
Sorry to bother again but there is error when i tried to run the code, the errors are:
Error using graph3d.surfaceplot>localConstructor (line 92)
Invalid input arguments
Error in graph3d.surfaceplot (line 7)
h = localConstructor(varargin{:});
Error in surf (line 101)
hh = double(graph3d.surfaceplot(args{:},'parent',parax));
Error in cylindrical2 (line 23)
figure, surf(x,y,TEMP) Best regards, Kien Tah
  4 Kommentare
Michael Haderlein
Michael Haderlein am 2 Feb. 2015
Can you please post your revised code? If the sizes differ from each other, the error message will be about the Data dimensions.
Michael Haderlein
Michael Haderlein am 2 Feb. 2015
Please don't create new answers during this discussion - just reply here in the comment section. Otherwise, the discussion will be unreadable for later users.
So, your code is:
function main = cylindrical()
clc; clear all; close all; clf;
global after_integration
k(r) = r.^2; %user defined:thermal conductivity as a function of r
r1 = 1; %user defined:inner radius in meter
r2 = 2; %user defined:outer radius in meter
T_inner = 100; %user defined:temperature at inner radius,T(r1)
T_outer = 200; %user defined:temperature at outer radius,T(r2)
after_integration = int(1/(k(r).*r),r); %function of r(f(r)) obtained from integration
after_integration_r1 = subs (after_integration,r1); %value of f(r) when r=r1=inner radius
after_integration_r2 = subs (after_integration,r2); %value of f(r) when r=r2=outer radius
term = (T_outer - T_inner)/(after_integration_r2 - after_integration_r1);
r = r1:0.1:r2;
after_integration_value = subs (after_integration,r);
T = term.* after_integration_value + T_inner - term.*after_integration_r1
r=1:.1:2; %radius
theta=0:pi/32:2*pi; %some angle scale, change the increment for smoother plot
[R,THETA]=meshgrid(r,theta); %create the mesh
[x,y]=pol2cart(THETA,R); %transform to cartesian
TEMP=ones(length(theta),1)*T; %mesh of temperatures
figure, surf(x,y,TEMP)
end
I wonder how you get the error you have posted. Already the line k(r)=... should fail as you have just removed the syms command. Now, k and r are just normal numerical variables, so this syntax is not allowed in this context. Replace all the "k(r)" by "k" and put the line r=r1:0.1:r2 before the r.^2 line (of course, r1 and r2 have to be defined even before).
Anyway, all the integration things are still done by symbolic methods. If you want to keep working with these functions, you need to put back the syms line although I'd recommend to replace it all by numerical functions instead.
In any case, if you somehow end up having the radial temperature in a line array T, the lines from theta=0:... will produce such a figure.

Melden Sie sich an, um zu kommentieren.


Kien Tah Goh
Kien Tah Goh am 2 Feb. 2015
function main = cylindrical()
clc; clear all; close all; clf;
global after_integration
k(r) = r.^2; %user defined:thermal conductivity as a function of r
r1 = 1; %user defined:inner radius in meter
r2 = 2; %user defined:outer radius in meter
T_inner = 100; %user defined:temperature at inner radius,T(r1)
T_outer = 200; %user defined:temperature at outer radius,T(r2)
after_integration = int(1/(k(r).*r),r); %function of r(f(r)) obtained from integration
after_integration_r1 = subs (after_integration,r1); %value of f(r) when r=r1=inner radius
after_integration_r2 = subs (after_integration,r2); %value of f(r) when r=r2=outer radius
term = (T_outer - T_inner)/(after_integration_r2 - after_integration_r1);
r = r1:0.1:r2;
after_integration_value = subs (after_integration,r);
T = term.* after_integration_value + T_inner - term.*after_integration_r1
r=1:.1:2; %radius
theta=0:pi/32:2*pi; %some angle scale, change the increment for smoother plot
[R,THETA]=meshgrid(r,theta); %create the mesh
[x,y]=pol2cart(THETA,R); %transform to cartesian
TEMP=ones(length(theta),1)*T; %mesh of temperatures
figure, surf(x,y,TEMP)
end

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by