Complete exercise 9.20 from the text. (a) Create a function called polygon that draws a polygon...

60.1K

Verified Solution

Question

Mechanical Engineering

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

Answer & Explanation Solved by verified expert
4.3 Ratings (617 Votes)
    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students