Experiment 1_introduction to Matlab
Experiment 1_introduction to Matlab
Experiment: 1
Introduction to MATLAB
AIM:
Introduction
MATLAB, which stands for MATrix Laboratory, is a state-of-the-art mathematical software package that is
used extensively in both academia and industry. It is an interactive program for numerical computation and
data visualization, which along with its programming capabilities provides a very useful tool for almost all
areas of science and engineering.
MATLAB has many advantages compared to conventional computer languages (e.g., C, FORTRAN) for
solving technical problems. MATLAB is an interactive system whose basic data element is an array that does not
require dimensioning. The software package has been commercially available since 1984 and is now considered
as a standard tool at most universities and industries worldwide. It has powerful built-in routines that enable a very
wide variety of computations. It also has easy-to-use graphics commands that make the visualization of results
immediately available. Specific applications are collected in packages referred to as toolbox. There are toolboxes
for signal processing, symbolic computation, control theory, simulation, optimization, and several other fields of
applied science and engineering.
The main working window in MATLAB is called the desktop. When MATLAB is started, the desktop appears in
its default layout and it can be seen in the following figure:
Current Folder
(Access your files)
MATLAB executes the calculations according to the order of precedence displayed below. This order is the
same as used in most calculators.
In an expression that has several operations, higher-precedence operations are executed before lower-
precedence operations. If two or more operations have the same precedence, the expression is executed from
left to right. As illustrated in the next section, parentheses can be used to change the order of calculations.
MATLAB as a Calculator
The simplest way to use MATLAB is as a calculator. This is done in the Command Window by typing a
mathematical expression and pressing the Enter key. MATLAB calculates the expression and responds by
displaying ans = and the numerical result of the expression in the next line. This is demonstrated as follows.
Examples
11
ans =
7.5000
ans =
7.6667
ans =
62.5000
>> 27^(1/3)+32^0.2 1/3 is executed first, 27^(1/3) and 32^0.2 are executed next,
and + is executed last.
ans =
11
ans =
257.5551
It is seen that if we do not specify an output variable, MATLAB uses a default variable ans, short for answer,
to store the results of the current calculation. Note that the variable ans is created (or overwritten, if it is already
existed). To avoid this, we may assign a value to a variable or output argument name.
A variable is a name made of a letter or a combination of several letters (and digits) that is assigned a numerical
value. Once a variable is assigned a numerical value, it can be used in mathematical expressions, in functions,
and in any MATLAB statements and commands. A variable is actually a name of a memory location. When
a new variable is defined, MATLAB allocates an appropriate memory space where the variable’s assignment
is stored.
There are 20 words, called keywords, that are reserved by MATLAB for various purposes and cannot be used
as variable names. These words are:
When typed, these words appear in blue. An error message is displayed if the user tries to use a keyword as a
variable name. Some of the predefined variables are given as follows:
Assignment Operator
In MATLAB the = sign is called the assignment operator. The assignment operator assigns a value to a
variable.
Variable_name = A numerical value, or a computable expression
The left-hand side of the assignment operator can include only one variable name. The right-hand side can be
a number, or a computable expression that can include numbers and/or variables that were previously assigned
numerical values.
Example
>> x=7+8/2
x =
11
>> y=27^(1/3)+32^0.2
y =
5
Thus, the result is stored in y. This variable name can always be used to refer to the results of the previous
computations. Therefore, computing 4y will result in
Example
>> 4*x
ans =
44
Multiple Assignments
Multiple assignments can be done in the same line. The assignments must be separated with a comma
(spaces can be added after the comma). When the Enter key is pressed, the assignments are executed from
left to right and the variables and their assignments are displayed. For example, the assignments of the
variables a, B, and c above can all be done in the same line.
Example
An alternate command
An alternative command for assigning multiple variables is described in the following example.
Example
In the above command, the values a, B, and c are assigned to the variables x, y, and z respectively at a time.
Once a variable has been created, it can be reassigned. In addition, if you do not wish to see the intermediate
results, you can suppress the numerical output by putting a semicolon (;) at the end of the line. Then the
sequence of commands looks like this:
Example
>> t = 5;
>> t = t+1
t =
6
Quitting MATLAB
To end your MATLAB session, type quit/exit in the Command Window, or select File → Exit MATLAB in
the desktop main menu.
Example
>> quit
Comments (%)
The percentage sign is used to denote a comment. Comments are text in the program that has no effect on the
program itself. In MATLAB, everything on a line that appears after the % sign is ignored. Comments are very
useful for making code easier to understand
x =
4
Case Sensitivity
MATLAB is case sensitive so it distinguishes between upper and lower-case letters, which means that A + B
is not the same as a + b. For example, AA, Aa, aA, and aa are the names of four different variables. Variable
and function names are case sensitive.
Getting Help
To view the online documentation, select MATLAB Help from Help menu or MATLAB Help directly in the
Command Window. The preferred method is to use the Help Browser. The Help Browser can be started by
selecting the ? icon from the desktop toolbar. On the other hand, information about any command is available
by typing
Examples:
In addition to basic arithmetic operations, expressions in MATLAB can include 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, or a
computable expression that can be made up of numbers and/or variables. Functions can also be included in
arguments, as well as in expressions. Some commonly used elementary MATLAB mathematical built-in
functions are given as follows.
The inverse trigonometric functions are asin(x), acos(x), atan(x), acot(x) for the angle in radians;
and asind(x), acosd(x), atand(x), acotd(x) for the angle in degrees. The hyperbolic trigonometric
functions are sinh(x), cosh(x), tanh(x), and coth(x).
Rounding functions
Function Description Example
round(x) Round to the nearest integer. >> round(17/5)
ans =
3
fix(x) Round toward zero. >> fix(13/5)
ans =
2
ceil(x) Round toward infinity. >> ceil(11/5)
ans =
3
floor(x) Round toward minus infinity. >> floor(-9/4)
ans =
-3
rem(x,y) Returns the remainder after x is >> rem(13,5)
divided by y. ans =
3
a.
2 2
9
49
b.
14.82 6.52
55
2 14
2
3.8
c.
144 e10.5 5!
log(1000) 2
5. A cube has a side of 18 cm. Determine the radius of the sphere that has the same volume as the cube.
4
(Hint: Volume of the cube = s3 , Volume of the sphere = V r 3 )
3