2.11: Chapter Exercises

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    Read aloud

    How

    Speed: 1×
    Light up
    Highlight

    Symbols: Mulberry Symbols, © 2018–2026 Steve Lee, CC BY-SA 4.0 (licence, the set), the site's own copies.

    From LibreTexts, under CC BY-NC-SA, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    2.11: Chapter Exercises

    Exercise 1: Cylinder Volume Script

    Create a script named cylinderVolume.m that calculates the volume of a cylinder with diameter 1 meter and height 5 meters. Add comments at the top of the script explaining what the script does and who wrote it. Test the script using the commands cylinderVolume, type cylinderVolume, and help cylinderVolume.

    Exercise 2: Cylinder Volume with Input and Output

    Modify your cylinderVolume.m script so it prompts the user to enter the diameter and height of the cylinder. Then print the result using fprintf. Save the new script as cylinderVolumeInputOutput.m.

    Exercise 3: Hollow Sphere Function

    Write a function named massHollowSphere.m that calculates the mass of a hollow sphere. The function should take inside diameter, outside diameter, and density as input arguments and return the mass. Add help comments and test your function from the Command Window.

    Exercise 4: Script Calling a Function

    Write a script named calcMassHollowSphere.m that prompts the user to enter inside diameter, outside diameter, and density. The script should call massHollowSphere.m and use fprintf to display the result in a complete sentence.

    Exercise 5: Fahrenheit to Kelvin

    Write a script that prompts the user to enter a temperature in degrees Fahrenheit, converts it to kelvin, and displays the result using fprintf.

    Use the equation \(T_K = (5/9)(T_F - 32) + 273.15\).

    Exercise 6: Cone Geometry Program

    Write a program made of a script and a function. The script should prompt the user for the radius and height of a cone, pass those values to a function, and display the volume and surface area. The function should calculate and return both values.

    Exercise 7: A Reusable Geometry Calculator

    Create a small geometry calculator that includes one script and at least three functions. The script should ask the user which shape they want to calculate, ask for the needed dimensions, call the correct function, and display the result using fprintf. Possible functions include rectangle area, cylinder volume, cone volume, and sphere volume.

    Your final program should include:

    • A script with clear prompts and formatted output.
    • At least three user-defined functions.
    • Helpful comments at the top of every file.
    • Consistent units in all prompts and output messages.
    • Test cases showing that each function works correctly.

    This page titled 2.11: Chapter Exercises was last modified on Tue, 30 Jun 2026 02:11:06 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.