Analyze data
Turn a large pile of numbers into results that are easier to understand.
It remembers numbers, works with number tables, draws graphs, and follows complete procedures. This lesson explains variables, vectors, and matrices from zero.
First understand what it can do, and then learn how to talk to it.
Turn a large pile of numbers into results that are easier to understand.
Write a clear set of steps so the computer can repeat a task.
Use mathematics and code to study engineering, physics, and science problems.
Combine calculations, graphics, and models into useful tools.
The name comes from MATLAB's original strength: working directly with matrices, which are simply tables of numbers arranged in rows and columns.
One way is good for trying one line now, and the other is good for saving a complete procedure.
Type one command, press Enter, and see the result immediately.
r = 4;
h = 12;
v = pi*r^2*h/3;Save many commands in one .m file and run them together.
Click each area below to see what job it performs.
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.
Most symbols look like an ordinary calculator, but a few small rules matter a lot.
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.
A variable is a named box that can store one number or a group of numbers.
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.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.A function is like a small machine: put in a value, and it returns a result using a fixed rule.
sqrt(54)square rootsin(0.5)sinecos(0.5)cosineexp(10)e to the power of 10log10(10)base-10 logarithmlog(10)natural logarithmsin and cos use radians by default, and π radians equals 180 degrees.sqrt(pi)date()calendar()clcclearformat longStart with one candy, then a row of candies, and finally a whole candy table.
One single value, which can be viewed as a 1×1 array.
A row or a column of numbers.
A two-dimensional table of numbers arranged in rows and columns.
The colon operator starts at the first value, repeatedly adds the step, and stops before the next value would pass the finish.
w = y ./ xNaN means “Not a Number,” because zero divided by zero has no single answer.
Every pair of x and y values becomes one point, and MATLAB then connects the points.
- solid: dotted-- dashed-. dash-doty yellowm magentac cyanr redg greenb bluew whitek black. pointo circlex x-mark+ plus* stars squared diamondv ^ < > trianglesp pentagramh hexagramIn 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.
A matrix is not mysterious; it is only a rectangular box of numbers with rows and columns.
A row is one horizontal line of numbers.
The first 3 means three rows, and the second 3 means three columns. Matrix size always says rows first, then columns.
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.
Spaces or commas separate columns in one row, and a semicolon starts a new row.
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.
This is not cell-by-cell multiplication. Matrix multiplication follows the rule “row times column.”
C(1,1) uses row 1 of A and column 1 of B.
The backslash is not cell-by-cell division. It searches for a matrix X that makes A*X equal B.
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.
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.
Row 1 of A comes from equation 1, and row 2 comes from equation 2. The numbers in b follow the same equation order.
The backslash means: find z so that A*z=b. The first item of z is x, and the second item is y.
1×2 + 2×1 = 42×2 − 1×1 = 3Both equations are true, so x=2 and y=1 is correct. In code, calculate A*z-b; a zero vector means the check passed.
syms and solve to solve the equations symbolically, but the backslash method is the most direct method for this lesson.syms x y
[x,y] = solve(x + 2*y - 4, 2*x - y - 3)
x = double(x), y = double(y)A script is a saved list of commands that MATLAB runs from top to bottom.
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.The script stores the radius and height first, then calculates the volume from the formula.
Good programmers do not memorize every command; they know how to look things up quickly.
help sinYou 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