2.2: From Calculator Commands to Programs

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.2: From Calculator Commands to Programs
    Note

    The ideal gas law is easy to remember and apply in solving problems, as long as you get the proper values a

    In the first MATLAB chapter, you probably used the Command Window like a powerful calculator. You typed a command, pressed Enter, and MATLAB immediately returned an answer. That is very useful when you want to try one quick calculation. However, most real problems require more than one command.

    For example, suppose you want to calculate the area of a rectangle. You need to know the width and height, multiply them, and then report the result. That is already a sequence of steps. If you later want to calculate the area of another rectangle, you would rather not retype everything from scratch. This is where scripts and functions become useful.

    What Is an Algorithm?

    An algorithm is the sequence of steps needed to solve a problem. Before writing code, it is helpful to first describe the algorithm in plain language. This helps us organize our thinking and avoid jumping into MATLAB syntax too early.

    For calculating the area of a rectangle, the algorithm could be:

    clipboard_e156f1fc6d007b6bd89fab8d2cad45d5b.png

    1. Get the input values: width and height.

    2. Calculate the result using area = width * height.

    3. Display the output: the area of the rectangle.

    Notice that this algorithm has three common parts: input, calculation, and output. This pattern appears in many engineering programs.

    Program part

    Meaning

    Rectangle example

    Input

    Information needed before the calculation can happen

    width and height

    Processing

    The calculation or decision-making step

    area = width * height

    Output

    The result shown to the user or saved for later

    the calculated area

    Note

    Everyday analogy: An algorithm is like a recipe. A recipe tells you what steps to follow and in what order. A MATLAB program is the recipe written in a language MATLAB can execute.


    This page titled 2.2: From Calculator Commands to Programs was last modified on Tue, 30 Jun 2026 02:11:44 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.