MATLAB · LESSON 01Interactive teaching notes
STARTING FROM ZERO

Meet MATLAB: Your Super Calculator.

It remembers numbers, works with number tables, draws graphs, and follows complete procedures. This lesson explains variables, vectors, and matrices from zero.

Begin the lesson
中/EN Hover any sentence for Chinese.
01

What is MATLAB?

First understand what it can do, and then learn how to talk to it.

Analyze data

Turn a large pile of numbers into results that are easier to understand.

Develop algorithms

Write a clear set of steps so the computer can repeat a task.

ƒ

Solve technical problems

Use mathematics and code to study engineering, physics, and science problems.

Create new systems

Combine calculations, graphics, and models into useful tools.

MAT
+
LAB
=
MATrix LABoratory

The name comes from MATLAB's original strength: working directly with matrices, which are simply tables of numbers arranged in rows and columns.

02

Two ways to use MATLAB

One way is good for trying one line now, and the other is good for saving a complete procedure.

Command Window mode

>> 2 + 3ans = 5

Type one command, press Enter, and see the result immediately.

OR

Script mode

r = 4;
h = 12;
v = pi*r^2*h/3;

Save many commands in one .m file and run them together.

Simple analogy: the Command Window is like asking one question aloud, while a script is like writing down a full recipe that you can use again.
03

Meet the MATLAB desktop

Click each area below to see what job it performs.

HOMENEWOPENSAVERUN
Current Folder: It shows the files that you can open and run.

Command History remembers what you typed, and the Up Arrow key can bring back your previous command.

The Workspace is like a desk that holds the variables created in the current session.

04

Use MATLAB as a calculator

Most symbols look like an ordinary calculator, but a few small rules matter a lot.

+AddSubtract*Multiply/Divide^Power

Order of operations

MATLAB evaluates parentheses and powers first, multiplication and division next, and addition and subtraction last.

;

A semicolon runs the command but hides its printed result.

,

A comma can separate commands on one line and lets each result appear.

...

Three dots mean that one long command continues on the next line.

%

Text after a percent sign is a comment, so MATLAB ignores it.

The panel below is a MATLAB practice window. Change the code and run it yourself.
05

Variables and the Workspace

A variable is a named box that can store one number or a group of numbers.

a3
b2
ca × b = 6

The equal sign is not asking whether both sides are equal; it says, “calculate the right side, then store the answer in the named box on the left.”

whoLists only the variable names.
whosLists names, sizes, memory use, and classes.
clear aRemoves only the variable named a.
clear allRemoves all ordinary variables and makes the Workspace empty again.
clcClears the Command Window text but keeps the variables.

Three ways to show output

x + 5An expression is printed when it does not end with a semicolon.
disp(x + 5)disp can show a number, vector, or matrix.
disp('Hello')Text inside apostrophes is a string, and disp can show it.
06

Built-in math functions

A function is like a small machine: put in a value, and it returns a result using a fixed rule.

54
sqrt( )
7.3485
sqrt(54)square root
sin(0.5)sine
cos(0.5)cosine
exp(10)e to the power of 10
log10(10)base-10 logarithm
log(10)natural logarithm
Important: MATLAB's sin and cos use radians by default, and π radians equals 180 degrees.

A function returns a value.

sqrt(pi)date()calendar()

A command usually changes the environment.

clcclearformat long
07

Scalars, vectors, and arrays

Start with one candy, then a row of candies, and finally a whole candy table.

7

Scalar

One single value, which can be viewed as a 1×1 array.

1234

Vector

A row or a column of numbers.

123456

Matrix

A two-dimensional table of numbers arranged in rows and columns.

0 : 1 : 10

The colon operator starts at the first value, repeatedly adds the step, and stops before the next value would pass the finish.

w = y ./ x
0/0NaN

NaN means “Not a Number,” because zero divided by zero has no single answer.

08

Turn numbers into a plot

Every pair of x and y values becomes one point, and MATLAB then connects the points.

x00.51
y=x²00.251
(0, 0)
- solid-- dashed: dottedo circle* starr redb blueg green
Open the complete line, marker, and color reference
Lines- solid: dotted-- dashed-. dash-dot
Colorsy yellowm magentac cyanr redg greenb bluew whitek black
Markers. pointo circlex x-mark+ plus* stars squared diamondv ^ < > trianglesp pentagramh hexagram

In plot(x,y,'-or'), the dash means a solid line, o means circle markers, and r means red.

ezplot('sin(x)') is a shortcut that chooses an x range and draws the function automatically.

09

Matrix foundations: begin with a number table

A matrix is not mysterious; it is only a rectangular box of numbers with rows and columns.

218010736

A row is one horizontal line of numbers.

3 × 3

The first 3 means three rows, and the second 3 means three columns. Matrix size always says rows first, then columns.

A(2,3)

The first number is the row and the second is the column, so A(2,3) means row 2, column 3. MATLAB starts counting at 1.

[ row ; row ]

Spaces or commas separate columns in one row, and a semicolon starts a new row.

3×3 matrix addition: same place plus same place

123456789
+
987654321
=
101010101010101010

Top-left adds to top-left, centre adds to centre, and bottom-right adds to bottom-right. Every cell only adds to the cell in the same position.

The two matrices must have exactly the same number of rows and columns before they can be added.

3×3 matrix multiplication: pair one row with one column

  1. Choose one answer cell C(r,c).
  2. Take row r from A.
  3. Take column c from B.
  4. Multiply matching positions, then add the three products.

This is not cell-by-cell multiplication. Matrix multiplication follows the rule “row times column.”

A
218010736
×
B
104010532
=
C
422524010372140
2×1 + 1×0 + 8×5 = 42

C(1,1) uses row 1 of A and column 1 of B.

A × X = B
therefore
X = A \ B

The backslash is not cell-by-cell division. It searches for a matrix X that makes A*X equal B.

10

Solve linear equations with a matrix, step by step

The answer must make every equation true at the same time. With two unknowns, each equation can be seen as a line, and the crossing point is the shared answer.

x + 2y = 4
2x − y = 3
The two lines meet at (2,1), so x = 2 and y = 1.
(2, 1)
1

Read the coefficients

In the first equation, x has coefficient 1 and y has coefficient 2. In the second, x has coefficient 2 and y has coefficient −1. An unwritten 1 still counts.

2

Build A and b

A = [1 2; 2 -1]z = [x; y]b = [4; 3]

Row 1 of A comes from equation 1, and row 2 comes from equation 2. The numbers in b follow the same equation order.

3

Ask MATLAB for z

z = A \ b

The backslash means: find z so that A*z=b. The first item of z is x, and the second item is y.

4

Substitute and check

1×2 + 2×1 = 42×2 − 1×1 = 3

Both equations are true, so x=2 and y=1 is correct. In code, calculate A*z-b; a zero vector means the check passed.

The Symbolic Math Toolbox can also use syms and solve to solve the equations symbolically, but the backslash method is the most direct method for this lesson.
See the symbolic route from the lecture
syms x y
[x,y] = solve(x + 2*y - 4, 2*x - y - 3)
x = double(x), y = double(y)
11

Scripts and M-files

A script is a saved list of commands that MATLAB runs from top to bottom.

1Open a new script.
2Type several commands.
3Save it as an .m file.
4Type the filename in the Command Window, or press Run.

M-file naming rules

cone_volume.mGood: it starts with a letter and uses only letters, digits, and underscores.
1st cone.mBad: it starts with a digit and contains a space.
plot.mAvoid it: it conflicts with MATLAB's built-in plot function.
hr

Cone volume formula

V = πr²h / 3

The script stores the radius and height first, then calculates the volume from the formula.

12

Use Help and connect the ideas

Good programmers do not memorize every command; they know how to look things up quickly.

sin — Sine of an argument in radians.
helphelpwindochelp sinhelp exp
1Type a command.
2Store results in variables.
3Organize many numbers with vectors and matrices.
4Turn numbers into plots.
5Save complete procedures as M-files.

At the end of Lesson 1, you now have a complete map of the MATLAB basics.

You do not need to remember every piece of syntax at once; the important part is knowing what each tool does and where to look when you need it.

Return to the top