Simple Tutorial for MATLAB
MAE 107 Spring
Professor: William M. McEneaney
TA: J.J. Gao
Outline
Overview
Software Layout / Interfaces
Useful commands and structures
Examples (hints to homework)
Overview
MATLAB
• Short for: Matrix Laboratory
• A numerical computing environment
• Non pre-compiled programming language
• It allows:
• matrix manipulations
• plotting of functions and data
• implementation of algorithms
• Creation of user interfaces
Interface 1 – Main Window
Interface 2 –Editor
Interface 3 – Help
Interface 4 – Figures
Syntax and Variables
Two ways to input command:
at the prompt >> in the Command Windows
Matlab Editor
Varible
matlab is a weakly typed language.
variables can be assigned without declaring their
type.
Vectors / Matrices
allvariables are treated as vectors/matrices.
It provides many convenient ways for creating
vectors, matrices, and multi-dimenstional arrays.
Useful Commands & Structures (1)
Basic:
help
clc (clear command window)
clear (clear all variables)
close all (close all figures windows)
Operators:
‘ (matrx transpose)
* (matrix multiplication )
.* (array multiplication (element-wise))
the same for other operations, like ^ (power),
/ (divide)
Useful Commands & Structures (2)
Vector/Matrix
: (colon, create vectors, array subscripting, and
for-loop iterators )
c = 1:10; d =1:3:10;
a = [1 2 3 4; 5 6 7 8;3 4 5 6];
zeros, ones; rand;
max, min, sum, mean, std, var, round, floor, ceil
size, cat(concatenate),
Useful Commands & Structures (3)
exp, log, log10, sqrt, abs, conj, i (j), real,
imag,
figure, plot, subplot
grid, hold, legend
xlabel, ylabel, title, xlim, ylim
meshgrid (Generate X and Y arrays for
3-D plots)
poly, roots,
Loops/Conditional Statement
for loop while loop
for i= 1:2:11 while i < 11
<execute stuff> <execute stuff>
end i= i+ 2;
end
if statement
if i < 11
<execute stuff>
else if i > 11
<execute other stuff>
end
CODE 2
Example 1
Example 2
Example 3 (Taylor polynomial)
Example 4
(textbook page40 algorithm 2.1)
• Evaluate this polynomial using Horner’s rule.
Example 5 (1)
(textbook page45 example 2.1)
Example 5 (2)
(textbook page40 algorithm 2.1)