Experiment No 8
Experiment No 8
8- Introduction to MATLAB
Objective: To introduce the basic information on MATLAB
What Is MATLAB?
MATLAB is a high-performance language for technical computing. It integrates computation,
visualization, and programming in an easy-to-use environment where problems and solutions
are expressed in familiar mathematical notation. Typical uses include:
MATLAB is an interactive system whose basic data element is an array that does not require
dimensioning. This allows you to solve many technical computing problems, especially those
with matrix and vector formulations, in a fraction of the time it would take to write a program
in a scalar non interactive language such as C or Fortran.
The name MATLAB stands for matrix laboratory. MATLAB was originally written to
provide easy access to matrix software developed by the LINPACK and EISPACK projects,
which together represent the state-of-the-art in software for matrix computation.
MATLAB has evolved over a period of years with input from many users. In university
environments, it is the standard instructional tool for introductory and advanced courses in
mathematics, engineering, and science. In industry, MATLAB is the tool of choice for high-
productivity research, development, and analysis.
Handle Graphics.
This is the MATLAB graphics system. It includes high-level commands for two-dimensional
and three-dimensional data visualization, image processing, animation, and presentation
graphics. It also includes low-level commands that allow you to fully customize the
appearance of graphics as well as to build complete Graphical User Interfaces on your
MATLAB applications.
Note: From now on an instruction to press a certain key will be denoted by < >, e.g., pressing
the enter key will be denoted as <enter>. Commands that should be typed at the prompt, will
be written in courier font.
The MATLAB environment (on most computer systems) consists of menus, buttons and a
writing area similar to an ordinary word processor. There are plenty of help functions that you
are encouraged to use. The writing area that you will see when you start MATLAB, is called
the command window. In this window you give the commands to MATLAB. For example,
when you want to run a program you have written for MATLAB you start the program in the
command window by typing its name at the prompt. The command window is also useful if
you just want to use MATLAB as a scientific calculator or as a graphing tool. If you write
longer programs, you will find it more convenient to write the program code in a separate
window, and then run it in the command window.
In the command window you will see a prompt that looks like >> . You type your commands
immediately after this prompt. Once you have typed the command you wish MATLAB to
perform, press <enter>. If you want to interupt a commandthat MATLAB is running, type
<ctrl> + <c>.
The commands you type in the command window are stored by MATLAB and can be viewed
in the Command History window. To repeat a command you have already used, you can
simply double-click on the command in the history window, or use the <up arrow> at the
command prompt to iterate through the commands you have used until you reach the
command you desire to repeat.
at the prompt. Be careful with parantheses and don't forget to type * whenever you multiply!
Note that MATLAB is case sensitive. This means that MATLAB knows a difference between
letters written as lower and upper case letters. For example, MATLAB will
understand sin(2) but will not understand Sin(2).
Here is a table of useful operations, functions and constants in MATLAB.
MATLAB
Operation, function or constant
command
+ (addition) +
- (subtraction) -
� (multiplication) *
/ (division) /
ex exp(x)
sin x sin(x)
cos x cos(x)
tan x tan(x)
cot x cot(x)
arcsin x asin(x)
arccos x acos(x)
arctan x atan(x)
arccot x acot(x)
n! (n factorial) gamma(n+1)
e (2.71828...) exp(1)
(3.14159265...) pi
Exercises
3cos(pi)
1+1+1/2+1/6+1/24-e
ln (1000+2pi-2)
e i pi
The number of combinations in which 12 persons can stand in line. (Hint: Use
factorials.)
To obtain help on any of the MATLAB commands, you simply need to type
help <command>
at the command prompt. For example, to obtain help on the gamma function, we type at the
command prompt:
help gamma
Try this now. You may also get help about commands using the "Help Desk", which can be
accessed by selecting the MATLAB Help option under the Help menu.
Note that the description MATLAB returns about the command you requested help on
contains the command name in ALL CAPS. This does not mean that you use this command
by typing it in ALL CAPS. In MATLAB, you almost always use all lower case letters when
using a command.
Variables in MATLAB
We can easily define our own variables in MATLAB. Let's say we need to use the value of
3.5sin(2.9) repeatedly. Instead of typing 3.5*sin(2.9)over and over again, we can denote this
variable as x by typing the following:
x=3.5*sin(2.9)
and observe what happens. Note that we did not need to declare x as a variable that is
supposed to hold a floating point number as we would need to do in most programming
languages.
Often, we may not want to have the result of a calculation printed-out to the command
window. To supress this output, we put a semi-colon at the end of the command; MATLAB
still performs the command in "the background". If you defined x as above, now type
y=2*x;
In many cases we want to know what variables we have declared. We can do this by
typing whos. Alternatively, we can view the values by openning the "Workspace" window.
This is done by selecting the Workspace option from the View menu. If you want to erase all
variables from the MATLAB memory, type clear. To erase a specific variable, say x,
type clear x. To clear two specific variables, say x and y, type clear x y, that is separate the
different variables with a space. Variables can also be cleared by selecting them in the
Workspace window and selecting the delete option.
We can also create this vector by typing x=1:10. The vector (1 1.1 1.2 1.3 1.4 1.5) can be
created by typing x=[ 1 1.1 1.2 1.3 1.4 1.5 ] or by typing x=1:0.1:1.5.
A=[1 2 3 ; 4 5 6; 7 8 9],
i.e., rows are separated with semi-colons. If we want to use a specific element in a vector or a
matrix, study the following example:
Example:
x=[10 20 30]
A=[ 1 2 3 ; 4 5 6 ; 7 8 9]
x(2)
A(3,1)
Here we extracted the second element of the vector by typing the variable and the position
within parantheses. The same principle holds for matrices; the first number specifies the row
of the matrix, and the second number specifies the column of the matrix. Note that in
MATLAB the first index of a vector or matrix starts at 1, not 0 as is common with other
programming languages.
If the matrices (or vectors which are special cases of a matrices) are of the same dimensions
then matrix addition, matrix subtraction and scalar multiplication works just like we are used
to.
y=sin(x.^2);
plot(x,y)
Example 2: Plot exp(sin(x)) on the interval [-,]. To do this, type the following:
x=linspace(-pi,pi,101);
y=exp(sin(x));
plot(x,y)
and observe what happens. The command linspace creates a vector of 101 equally spaced
values between - and (inclusive).
Ocassionally, we need to plot values that vary quite differently in magnitude. In this case, the
regular plot command fails to give us an adequate graphical picture of our data. Instead, we
need a command that plots values on a log scale. MATLAB has 3 such
commands: loglog,semilogx, and semilogy. Use the help command to see a description of
each function. As an example of where we may want to use one of these plotting routines,
consider the following problem:
Example 3: Plot x5/2 for x = 10-5 to 105. To do this, type the following:
x=logspace(-5,5,101);
y=x.^(5/2);
plot(x,y)
The command logspace is similar to linspace, however it creates a vector of 101 points
lograthmically equally distributed between 10-5 and 105.
Note that all text must be put within ' '. The last two commands (hold on and hold off) are
best explained by trying them next time you plot.