In: Mechanical Engineering
Complete exercise 9.20 from the text.
(a) Create a function called polygon that draws a polygon in a polar 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…You should use the function you created in part (a) to draw each polygon. Use the index parameter from the for loop to specify the subplot in which each polygon is drawn, and in an expression to determine the number of sides used as input to the polygon fuction.
For help with the algorithm to draw regular polygons, consult problem 8.17 To be clear, the polygon() function should only draw one polygon, as specified by the number it receives as a parameter.
A second function or script should have the for loop, call subplot() to indicate where to create a plot, and call polygon() to actually create the plot.
Additional notes include:
1. There must be some weird junk on the internet because many previous students have tried to use some gibberish with rand() and cumsum() that does not create triangles, squares, etc. Do not repeat their mistakes.
2. Never clear variables at the start of a function!
3. A for loop with just one number is a waste of typed characters.
4. The instructions never mention prompting the user for a number.
5. You must call subplot before you plot anything that is supposed to go into a subplot.
6. Things that only need to be done once should not be inside a loop, such as set(gcf, …). The only things that should be inside a loop 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 to note number 4 and additional corrections
%% 9.20 (a) create a function called polygon in a polar
plot
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