Question

Write a user-defined MATLAB function that will calculate the area/volume of a shape whose dimensions are...

Write a user-defined MATLAB function that will calculate the area/volume of a shape whose dimensions are given by the user. This function will be able to calculate the area for 2D objects like a triangle or a rectangle and 3D objects such as a box. The name of the function will be findArea and it will have four input arguments. The first input argument will be the length. The second input argument will be the width. The third input argument will be the height if it is a 3D object. If not, the user will need to input 0. The fourth argument is to differentiate between 2D objects. It will be a string that is either triangle' or 'rectangle'. If it is a 3D object, the user will need to input 'none' for this argument. There is only one output argument which is the area/volume of the shape given. Make sure to define three subfunctions that will calculate the area of a triangle, rectangle, and box respectively. Call those subfunctions from within findArea().

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have attached screenshots and code below. Please check them.

MATLAB CODE:

function findArea()
a=input('enter the length=');
b=input('enter the width=');
c=input('enter the height if the object is 3D(else enter 0)=');
d=input('enter the type of the object as a string(triangle/rectangle/none):- ');
switch d
case 'triangle'
areaTri(b,a);
case 'rectangle'
areaRect(a,b);
case 'none'
volBox(a,b,c);
otherwise
fprintf('error: You have entered undetermined object type');
end

end

function areaTri(base,height)
Area_of_the_Triangle=(0.5)*base*height
end

function areaRect(length,breadth)
Area_of_the_Rectangle=length*breadth
end

function volBox(length,breadth,height)
Volume_of_the_Box=length*breadth*height
end

***

Please give rating and comment if you have any queries. Thank You:)

Add a comment
Know the answer?
Add Answer to:
Write a user-defined MATLAB function that will calculate the area/volume of a shape whose dimensions are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem: Given information about a circle, rectangle and a triangle, calculate the area of the shape...

    Problem: Given information about a circle, rectangle and a triangle, calculate the area of the shape from the information supplied. Write pseudocode to solve this problem and write a Python program that asks the user to input the required information for a shape and then calculates the area of the shape. Geometry Calculator 1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle 3. Calculate the Area of a Triangle 4. Quit Enter your choice (1...

  •    (MATLAB QUESTION) function [ volume , area ] = cylinderInfo( height , radius ) %...

       (MATLAB QUESTION) function [ volume , area ] = cylinderInfo( height , radius ) % % end function [ volume ] = calcVolume( height , radius ) % % function [ area ] = calcSurfArea( height , radius ) % % end 4.3 Subfunctions D Modify cylinderInfo.m, so that the function returns the volume and area of a cylinder, given the height and radius of the base. Volume x radius2 x height Surface Area 2 x radius2 +height x...

  • Solve in Matlab by using Script file: Problem 4: Write a user-defined MATLAB unction that calculates...

    Solve in Matlab by using Script file: Problem 4: Write a user-defined MATLAB unction that calculates the determinant of a 3x3 matrix by using the formula: A22 A23 11 A32 A33 A23 A12 A31 A33 21 A For the function name and arguments use d3-det3by3 (A), where the input argument A is the matrix and the output argument d3 is the value of the determinant. Write the code of det3by3 such that it has a subfunction that calculates the 2x2...

  • Q2) Interface Create a program that calculates the perimeter and the area of any given 2D...

    Q2) Interface Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamically assign the appropriate calculation for the given shape. The type of shapes are the following: • Quadrilateral 0 Square . Perimeter: 4xL • Area:LXL O Rectangle • Perimeter: 2(L+W) • Area:LxW Circle Circumference: I x Diameter (TT = 3.14) Area: (TT xD')/4 Triangle (assume right triangle) o Perimeter: a+b+c O Area: 0.5 x base x height (hint: the base...

  • Write a matlab script(using only matlab) for this. Write a script to solve the following problem:...

    Write a matlab script(using only matlab) for this. Write a script to solve the following problem: Ask the user for the length and the width of a rectangle. These need to be passed to a function. The function is to calculate and return the area and the perimeter of the rectangle. The area is the length times the width and the perimeter is 2 times the length and 2 times the width. Make sure to suppress all output from the...

  • Write a user-defined MATLAB function that gives a random integer number within a range between two...

    Write a user-defined MATLAB function that gives a random integer number within a range between two numbers. For the function name and arguments, use n = randint(a,b) where the two input arguments a and b are the two numbers and the output argument n is the random number. Use the function in the command window for the following: (a) Generate a random number between 1 and 49. (b) Generate a random number between -35 and -2

  • Using C++, Write a program that will provide the user a menu from which the user...

    Using C++, Write a program that will provide the user a menu from which the user may select to calculate the area of one of four geometric shapes: a circle, a rectangle, a triangle, and a trapezoid. You should use the most appropriate SWITCH block for this program. The user will input all data needed to calculate the area. The program should output all data input by the user, the calculated area, and the geometric shape selected. Run this program...

  • Question 1: Creating a user-defined function Write a user-defined MATLAB function named PBTask4pl_f.m for the following...

    Question 1: Creating a user-defined function Write a user-defined MATLAB function named PBTask4pl_f.m for the following math function with x the input argument and y the output y(x)=0.8x4-13x2-5x The function should work for x being a scalar or a vector. Write a script file named PBTask4pl.m to a) Use the function to calculate y(3) and y(5) and display the results in command window b) Use the function to make a plot of the function y(x) for -5:5: x 5:5. Label...

  • Question 4.4: Write an overloaded function of function area with 3 (float) parameters. This function should...

    Question 4.4: Write an overloaded function of function area with 3 (float) parameters. This function should calculate and print out the product of the 3 parameters. Question 4.4: Write the main function to test question 4.1, 4.2, 4.3, 4.4. Your main function should ask if the user want to calculate the area of a rectangle or a circle or a triangle then print out the result accordingly. (Use switch structure). For example: Please enter (r) for rectangle, (t) for triangle,...

  • Python 3: Please follow the respective rubric for the following function definition. Rubric: Rectangle Shape You...

    Python 3: Please follow the respective rubric for the following function definition. Rubric: Rectangle Shape You should implement the following functions to print a rectangle shape. • print_rectangle(length, width, fillChar, hollow). This function will use draw_shape_line() function to print a rectangle shape with the given length and width, If the hollow is true, the shape will be hollow rectangle, otherwise a filled rectangle. This function must not interact with the user. For example, the call print_rectangle(5, 10, '*', False) should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT