How can I write a function that draws a regular polygon with n sides in a polar coordinate plot?
조회 수: 19 (최근 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
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
2013년 3월 7일
close
sides=5
degrees=2*pi/sides
theta=0:degrees:360-degrees
radius=ones(1,numel(theta))
polar(theta,radius)
댓글 수: 2
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
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
Anders Bray
2022년 7월 11일
theta includes 0 as the first step making it an 1x(sides+1) that is why have to accomidate.
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 Center 및 File Exchange에서 Cartesian Coordinate System Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!