How can I write a function that draws a regular polygon with n sides in a polar coordinate plot?

9 次查看(过去 30 天)
I am unfamiliar with plotting with polar coordinates. Here is what I have so far, which does not work:
function polygon(sides) % Name number of sides of the polygon
degrees=360/sides; % Find the angle between corners in degrees
radius=ones(1,sides) % Array of ones
theta=0:degrees:360 % Theta changes by the internal angle of the polygon
polar(theta, radius) % Plot
end
Thanks!
  1 个评论
Danielle Wojeski
Danielle Wojeski 2016-12-31
编辑:Walter Roberson 2016-12-31
%First you need to define the sides variable.
sides=input('input the number of sides you want;, ')
Then you need to make sure the radius and the theta match in size. If your theta starts at 0 it will always be one size bigger then your radius. So instead make it a 1.
It should look like this...
function polygon(sides) % Name number of sides of the polygon
sides = input('input the number of sides you want;, ');
degrees = 360./sides; % Find the angle between corners in degrees
r = ones(1,sides) % Array of ones
theta = 1:degrees:360 % Theta changes by the internal angle of the polygon
polar(theta, r) % Plot
end

请先登录,再进行评论。

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-3-7
close
sides=5
degrees=2*pi/sides
theta=0:degrees:360-degrees
radius=ones(1,numel(theta))
polar(theta,radius)
  2 个评论
gm76
gm76 2013-3-7
Thanks so much, this works! Why is it that degrees seems to be defined in radians, (2*pi/sides) but is then subtracted from 360?
Azzi Abdelmalek
Azzi Abdelmalek 2013-3-7
I can't for the moment explain this, It was an error, it should be
sides=9
degrees=2*pi/sides
theta=0:degrees:2*pi
radius=ones(1,numel(theta))
polar(theta,radius)

请先登录,再进行评论。

更多回答(1 个)

Carson Cooper
Carson Cooper 2017-2-13
编辑:Carson Cooper 2017-2-13
This gives a better output than those above
function polygon(sides)
sides = input('input the number of sides you want;, ');
radians = (2*pi)./sides;
r = ones(1, sides);
theta = 1:radians:2*pi;
polar(theta, r)
end
  4 个评论
debashish panda
debashish panda 2022-8-29
for n=3:1:6
subplot(2,2,n-2)
polygon(n)
end
function polygon(sides)
radians = (2*pi)./sides;
r = ones(1, sides+1);
theta = 0:radians:2*pi;
polar(theta, r)
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Elementary Polygons 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by