Skip to main content
Logo image

Subsection Using and Reassigning Variables

Variables are typically used to build expressions that depend on previously defined values. This lets you update one input and quickly recompute the output.

🌌 Example 8.

Suppose a triangle has base length 4 and height 10. Define variables for the base and height, and then compute the area.
base = 4;
height = 10;
area = 0.5 * base * height;
The variable area is created with value 20.
Variables can be reassigned at any time. When this happens, the previous value is overwritten.
base = height;
This command copies the current value of height into base. The value of height itself is unchanged.
In MATLAB, the symbol = does not represent a mathematical equation. It represents an instruction to store a computed value:
VARIABLE_NAME = EXPRESSION

Checkpoint 9.

After computing a variable using other variables, why does changing one of those original variables not automatically update the result?
  • MATLAB stores the computed value, not the formula used to compute it.
  • MATLAB only updates variables when semicolons are omitted.
  • MATLAB prevents dependent variables from changing.
  • MATLAB assumes variables represent algebraic equations.

Checkpoint 10.