How do I get the script to loop to the beginning?

I have the rest of the program running just fine but I can't get this to work. The assignment asks "Write a f(x) that computes the area and perimeter for regular polygons. The f(x) should be named "Polygon." The F(x) should accept the number of sides for a given regular polygon and length of the side, and it should return the area, and perimeter of a Polygon with n sides. Create a new script and save this script as AreaPerimeter.m this script should ask the user to input the length of the side. The script should then make a call to the f(x) Polygon repeatedly to compute the area and perimeter for polygons with sides 3-10. The script should then plot area vs. number of sides, and perimeter vs. number of sides on two separate graphs. After plotting the graph, the script should prompt the user if he wants to run the script again. If the user says "yes","Yes","Y",or "y", the script should run again, else the script should terminate."
disp ('This program will compute the area and perimeter for regular polygons');
nsides = input ('Please enter the number of sides on the polygon: ');
lsides = input ('Please enter the length of the sides on the polygon: ');
n = nsides;
a = lsides;
for loopvarone = guyfieri
if guyfieri == 1
if n>2&&n<11
[Area, Perimeter] = Polygon(n,a);
%{Area = (1/4)*n*(a.^2)*cotd(180/n); %formula to calculate the area
%{Perimeter = n*a; %formula to calculate the perimeter
disp ('----------------------------------------------------------------------------');
fprintf ('The area of the polygon with %.1f units sides is %.1f units and the perimeter is %.1f units' ,n ,Area ,Perimeter);
figure(1)
x1 = Area;
y1 = nsides;
plot (x1,y1,'r*')
hold on
figure(2)
x2 = Perimeter;
y2 = nsides;
plot (x2,y2,'b*')
grid on
again = input ('Do you want to run this again?: ');
if again == Y||y||Yes||yes;
guyfieri = 1;
else
end
else
fprintf ('Error')
again = input ('Do you want to run this again?: ');
if again == Y||y||Yes||yes;
guyfieri = 1;
else
end
end
else
end
end
when run it spits out
Undefined function or variable 'guyfieri'.
Error in AreaPerimeter (line 8) for loopvarone = guyfieri
What do I need to change?

3 comentarios

Cole McDonald
Cole McDonald el 7 de Oct. de 2016
Adam
Adam el 7 de Oct. de 2016
Editada: Adam el 7 de Oct. de 2016
Well, clearly guyfieri is undefined. What is it supposed to be? Define it as something before you try looping over it.
Cole McDonald
Cole McDonald el 7 de Oct. de 2016
I know it's undefined but I'm not sure how to denfine it to run the loop

Iniciar sesión para comentar.

Respuestas (1)

James Tursa
James Tursa el 7 de Oct. de 2016
Editada: James Tursa el 7 de Oct. de 2016
Thanks for posting your work so far. Some hints:
"The script should then make a call to the f(x) Polygon repeatedly to compute the area and perimeter for polygons with sides 3-10."
I think this is meant to instruct you to write the loop as follows:
for n=3:10
[Area, Perimeter] = Polygon(n,a);
:
etc
"After plotting the graph, the script should prompt the user if he wants to run the script again. If the user says "yes","Yes","Y",or "y", the script should run again, else the script should terminate."
So the intent of this seems to be to wrap your entire script in an endless loop that only gets broken when the user wants to exit out of it. E.g., something like this:
while( true )
:
% All of your script code goes here
:
again = input ('Do you want to run this again?: ','s'); % <-- use 's' to input string
if( ~ismember(again,{'y','Y','yes','Yes','YES'}) )
break;
end
end
You don't need any of the guyfieri code at all. So get rid of that for-loop and the if-test associated with guyfieri. And get rid of the multiple "again" checks ... you only need that one check at the end that I have shown. Also, you don't need to input "nsides" since that is not used ... the variable "n" from the for-loop is what is used for this.

Categorías

Más información sobre Desktop en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 7 de Oct. de 2016

Editada:

el 7 de Oct. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by