Lab 1
Lab 1
LAB 1
An introduction to
MATLAB
Student Name
Student ID
Student Score
1
What is MATLAB?
MATLAB is a software package for high-performance mathematical
computation, visualization, and programming environment. It provides an
interactive environment with hundreds of built-in functions for technical
computing, graphics, and animations.
2
MATLAB Language
Graphics
MATLAB Desktop
1. Current folder: It is located on the left side; here, you can access your
files.
2. Command Window: It is located on the right side; it is the command
prompt, and here you can enter commands to operate the functions,
to assign variables, and for calculations.
3. Workspace: It is located on the left side right below the Current
Folder; here, all variables that you create are stored, and data from
other files can also be imported here.
3
Variable and Workspace Information
Commands Purpose
4
load Load variable from file
Commands Purpose
5
Working with Variables and Arrays in
MATLAB
The structural unit of data in any MATLAB program is the array. An array is a collection
of record values organized into rows and columns and known by a single name. Arrays
can be allocated as either vectors or matrices. The term "vector" is generally used to
define an array with only one dimension, while the term "matrix" is usually used to
determine an array with two or more dimensions.
The numbers of the row state the size of an array, and the numbers of the column in
the array, with the numbers of the row, mentioned first. The total number of items in
the array will be the product of the number of rows and the number of the column.
Individual items in an array are addressed by the array name followed by the row
and column of the particular item. If the array is a row or column vectors, then only
one subscripts are needed. For example, in the preceding array a(2,1) is 3 and c(2) = 2.
MATLAB variables name must start with a letter, followed by any sequence of
letters, numbers, and the underscore (_) character. Only the first 63 characters are
essential; if more than 63 are used, the remaining characters will be ignored. If two
variables are stated with names that only differ in the 64th character, MATLAB will treat
them as the same variable.
6
Initializing Variables in Assignment Statement
The simplest method to initialize a variable is to assign it one or more value in an
assignment statement.
var = expression;
where var is the name of the variables and expression is a scalar constant, an array, or a
combination of constants, other variables, and mathematical operations (+, -, etc.). The value
of the expression is computed using the standard rules of mathematics, and the resulting
values are saved in the named variable.
var = 40i;
var2 = var/5;
x = 1; y = 2;
array = [1 2 3 4];
7
Array Initialization
The following statements are all legal arrays that can be used to initialize a variable:
Arrays can also be initialize using built-in MATLAB function. For example:
a = zeros(2);
b = zeros(2,3);
c = [1 2; 3 4];
d = zeros(size(c));
8
Functions Purpose
9
ELEMENTARY MATH BUILT-IN FUNCTIONS
MATLAB has a very large library of built-in functions. A function has a name and an argument
in parentheses. For example, the function that calculates the square root of a number is sqrt(x).
Its name is sqrt, and the argument is x. When the function is used, the argument can be a
number, a variable that has been assigned a numerical value
10
11
Practice
12
13