MATLAB function creation and application

Click For Summary

Discussion Overview

The discussion revolves around the creation and application of MATLAB functions for calculating the surface area and volume of various geometric shapes. Participants are working through a homework assignment that involves using functions, conditional statements, and user input in MATLAB.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant expresses confusion about how to structure their MATLAB functions, particularly regarding the use of parameters and the correct formatting of function files.
  • Another participant updates their code and attempts to implement the function for calculating surface area and volume, but encounters an error related to executing a script as a function.
  • A later post humorously acknowledges a mistake regarding the need to define "function" in the function file.
  • Further updates show attempts to correct the code, but errors persist, including issues with global variables and output arguments not being assigned.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus on the correct implementation of the functions, as multiple errors and misunderstandings are expressed throughout the discussion.

Contextual Notes

Limitations include unresolved issues with the use of global variables, the correct assignment of output arguments, and the overall structure of the function files. The discussion reflects ongoing attempts to clarify these points without reaching definitive solutions.

GE2014
Messages
11
Reaction score
0

Homework Statement



*see attachment*

Homework Equations





The Attempt at a Solution



Alright, so we just began messing with functions in our class and I'm kinda struggling to grasp this assignment. I believe it should begin like this:


shape = menu('Choose a shape','1: Sphere','2: Isosceles Triangular Pyramid','3: cylinder','4: Cone','5: Rectangular Prisim','-1: Exit');
if (shape<-1||shape>5||shape==0)
disp('Invalid Entry')
end
while shape~=-1
switch shape
case 1
r=input('Enter the radius of the sphere (Inches): ');
if (r<0)
disp('Invalid Entry')

end


sphereArea = surfaceAreaShapes(r);
sphereVolume = volumeShapes(r)
%calling the functions^^^^
%note: I am only showing 1 case of the switch statement and I realize the while loop is . %missing an end.


now that I called the functions, now I have to make em...(surfaceAreaShapes.m and volumeShapes.m) This is where I get super confused. I have a general idea that I'll perform an if/switch statement in order to call up the right formula that corresponds with the shape the user inputs. Just not real sure on the correct formatting of a function.

...Heres what I got for surfaceAreaShapes.m

area=surfaceAreaShapes(radius);

the parameter radius I set doesn't seem like it would make sense considering I will have other things that will need to be put in in order to determine area.

*sorry in advance its not indented properly. That bugs the hell outta me
 

Attachments

  • RRRRRRRRRRRRRRRR.PNG
    RRRRRRRRRRRRRRRR.PNG
    43.9 KB · Views: 588
Last edited:
Physics news on Phys.org
Updated attempt since class this morning


shapesFunc.m

clc
clear
shape = menu('Choose a shape','1: Sphere','2: Isosceles Triangular Pyramid','3: cylinder','4: Cone','5: Rectangular Prisim','-1: Exit');
if (shape<-1||shape>5||shape==0)
disp('Invalid Entry')
end
while shape~=-1
switch shape
case 1

r=input('Enter the radius of the sphere (Inches): ');
sphereArea = surfaceAreaShapes(r)




sphereArea=round(sphereArea*100)/100;
sphereVolume=round(sphereVolume*100)/100;
fprintf('For a radius of %g\n',r)
fprintf('Surface Area = %g square inches\n',sphereArea)
fprintf('Volume = %g square inches\n',sphereVolume)


end
end



surfaceAreaShapes.m (function)​
area=surfaceAreaShapes(radius);
global r b h l s w shape
switch shape
case 1

if (r<0)
disp('Invalid Entry')
break
end
%calculate surface area and volume
sphereArea = 4*pi*(r^2);
sphereVolume = (4/3)*pi*(r^3);
end

and here's the error statement I keep getting
Attempt to execute SCRIPT surfaceAreaShapes as a function:
C:\Users\Micheal\Documents\MATLAB\surfaceAreaShapes.m

Error in shapesFunc (line 12)
sphereArea = surfaceAreaShapes(r)
 
lolwow I am a dumbass.

might want to put Function in the function file huhh?
 
Update:

clc
clear
shape = menu('Choose a shape','1: Sphere','2: Isosceles Triangular Pyramid','3: cylinder','4: Cone','5: Rectangular Prisim','-1: Exit');
if (shape<-1||shape>5||shape==0)
disp('Invalid Entry')
end
while shape~=-1
switch shape
case 1

r=input('Enter the radius of the sphere (Inches): ');
area = surfaceAreaShapes()




sphereArea=round(sphereArea*100)/100;
sphereVolume=round(sphereVolume*100)/100;
fprintf('For a radius of %g\n',r)
fprintf('Surface Area = %g square inches\n',sphereArea)
fprintf('Volume = %g square inches\n',sphereVolume)


end
end



Function
function area=surfaceAreaShapes(radius);
global r b h l s w
shape = menu('Choose a shape','1: Sphere','2: Isosceles Triangular Pyramid','3: cylinder','4: Cone','5: Rectangular Prisim','-1: Exit');
switch shape
case 1

if (r<0)
disp('Invalid Entry')
return
end
%calculate surface area and volume
sphereArea = 4*pi*(r^2);
sphereVolume = (4/3)*pi*(r^3);
end
end


Error message: Error in surfaceAreaShapes (line 2)
global r b h l s w

Output argument "area" (and maybe others) not assigned during call to
"C:\Users\Micheal\Documents\MATLAB\surfaceAreaShapes.m>surfaceAreaShapes".

Error in shapesFunc (line 12)
area = surfaceAreaShapes()
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
31
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
7K
Replies
3
Views
3K