Complete exercise 9.20 from the text.
(a) Create a function called polygon that draws a polygon in apolar plot. Your function should have a single input parameter –the number of sides.
(b) Use a for loop to create a figure with four subplots…Youshould use the function you created in part (a) to draw eachpolygon. Use the index parameter from the for loop to specify thesubplot in which each polygon is drawn, and in an expression todetermine the number of sides used as input to the polygonfuction.
For help with the algorithm to draw regular polygons, consultproblem 8.17 To be clear, the polygon() function should only drawone polygon, as specified by the number it receives as aparameter.
A second function or script should have the for loop, callsubplot() to indicate where to create a plot, and call polygon() toactually create the plot.
Additional notes include:
1. There must be some weird junk on the internet because manyprevious students have tried to use some gibberish with rand() andcumsum() that does not create triangles, squares, etc. Do notrepeat their mistakes.
2. Never clear variables at the start of a function!
3. A for loop with just one number is a waste of typedcharacters.
4. The instructions never mention prompting the user for anumber.
5. You must call subplot before you plot anything that issupposed to go into a subplot.
6. Things that only need to be done once should not be inside aloop, such as set(gcf, …). The only things that should be inside aloop are the things that must be repeated.
7. If you copy and paste a few lines, especially more than once,there is probably a better way.
and here's what I did so far, it needs to be fixed according tonote number 4 and additional corrections
%% 9.20 (a) create a function called polygon in a polarplot
function [p] = polygon(n)
n = input('Enter the number of sides: ');
theta = 0: 2*pi/n : 2*pi;
r = ones(1, length(theta));
subplot(2, 2, n-2); polar(theta, r);
end
% (b) use a for loop to create figure with four subplots
function polygon
for n = 3: 1: 6
polar(theta, r)
end
end
Thanks