The document provides an introduction to MATLAB, highlighting its capabilities as a high-performance language for technical computing, including its data structures, built-in tools, and applications in various fields. It outlines the basic features of the MATLAB desktop, such as the Command Window, Command History, and Workspace, as well as instructions for starting and quitting MATLAB. Additionally, it covers fundamental operations like variable creation, arithmetic operations, error handling, and the use of built-in mathematical functions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
18 views7 pages
Experiment 1
The document provides an introduction to MATLAB, highlighting its capabilities as a high-performance language for technical computing, including its data structures, built-in tools, and applications in various fields. It outlines the basic features of the MATLAB desktop, such as the Command Window, Command History, and Workspace, as well as instructions for starting and quitting MATLAB. Additionally, it covers fundamental operations like variable creation, arithmetic operations, error handling, and the use of built-in mathematical functions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
College of Electronics Eng .
Electronic Eng. Dep.
2nd Year
Introduction
Tn this introduction we will describe how MATLAB handles simple numerical
expressions and mathematical formulas.
MATLAB is a high-performance language for technical computing. It integrates
computation, visualization, and programming environment. Furthermore, MATLAB is a
modem programming language environment: it has sophisticated data structures, contains
built-in editing and debugging tools, and supports object-oriented programming. These
factors make MATLAB an excellent too! for teaching and research.
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.
‘The name MATLAB stands for matrix labrotary. MATLAB was written originally
to provide easy access to matrix software developed by the LINPACK (linear system
package) and EISPACK (Eigen system package) projects.
It has powerful built-in routines that enable a very wide variety of computations. It
also has easy to use graphies 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.
2- Starting sQuitting MATLAB
After logging into your account, you can enter MATLAB by double-clicking on the
MATLAB shortcut icon (MATLAB 7.0) on your Windows desktop. When you start
MATLAB, a special window called the MATLAB desktop appears. The desktop is a
window that contains other windows. The major tools within or accessible from the
desktop are:
1-The Command Window
2-The Command History
3-The Current Directory
4-The Workspace
5-The Help Browser
6-The Start button
To end your MATLAB session, type quit in the Command Window, or select File ;! ExitCollege of Electronics Eng .
Electronic Eng. Dep.
2nd Year
MATLAB LAB
MATLAB in the desktop main menu, or quit from the command window.
Menus chango :
sependingantha Use tab togo ‘View or Move Command Window outside of
{ool you are joWorkspaco Gat help. changscurrent desktop (undack)
currantly using browser, directory.
Copyright 1964-2005 The MathVorks, Inc.
Version 7.0.4 (2145P2]
To get started, select MATLAB Help or Demos from th
Gre== 2/23/04 3:59 PH =:
cd di/mynfiles/ses_t
elsar
Click Start Viow or axacute Drag the soparatarbar —Entar MATLAB functions at
button for quick —_pravicusly run functions to resize windows, ‘command-line prompt
accass to tools from the Command
and moro. History window.College of Electronics Eng .
Electronic Eng. Dep.
2nd Year
MATLAB LAB
1-Command Window
Use the Command Window to enter variables and to run functions and M-file scripts.
Press the up arrow key to recall a statement you previously typed. Edit the statement as
needed and then press Enter to run it,
2-Command History
Statements you enter in the Command Window are logged in the Command History.
From the Command History, you can view previously run statements, as well as copy and
execute selected statements. You can also create an M-file from selected statements. To
save the input and output from a MATLAB session to a file, use the diary function.
3-Current Directory
‘A quick way to view or change the current directory is by using the current directory field
n the desktop toolbar, shown here. To search for, view, open, and make changes to
MATLAB related directories and files use the MATLAB Current Directory browser.
Alternatively, you can use the functions dir, ed, and delete. Use the Visual Directory and
Directory Reports to help you.
4-Workspace Browser and Array Editor
‘The MATLAB workspace consists the set of variables (named arrays) built up during a
MATLAB session and stored in memory. You add variables to the workspace by using
functions, running M-files, and loading saved workspaces.
To delete variables from the workspace, select the variables and select Edit -> Delete.
Alternatively, use the clear function, The workspace is not maintained after you end the
MATLAB session, To save the workspace to a file that can be read during 2 later
MATLAB session, select File -> Save, or use the save function. This saves the workspace
to a binary file called a MAT-file, which has a mat extension. You can use options to
save to different formats. To read in a MAT-file, select File -> Import Data, or use the
Joad function.
5- Help Browser
Use the Help browser to search for and view documentation and demos for all your Math
Works products. The Help browser is an HTML viewer integrated into the MATLAB
desktop. To open the Help browser, click the help button in the desktop toolbar.
>> help
>>doe
>>helpwinCollege of Electronics Eng
Electronic Eng. Dep.
2nd Year
MATLAB LAB
3-Arranging the Desktop
‘These are some common ways to customize the desktop: Show or hide desktop tools via
the Desktop menu. Resize any tool by dragging one of its edges. Move a tool outside of
the desktop by clicking the undock button in the tool's title bar. Reposition a tool within
the desktop by dragging its title bar to the new location. Tools can occupy the same
position, as shown for the Current Directory and Workspace browser in the preceding
illustration, in which case, you access a tool via its tab. Change fonts and other options by
using File > Preferences.
4- Getting started and Simple Arithmetic in
MATLAB
After leaming the minimum MATLAB session, we will now learn to use some additional
operations,
4.1 Creating MATLAB variables
MATLAB variables ate created with an assignment statement. The syntax of variable
assignment is
variable name =a value (or an expression)
For example,
>> x= expression
where expression is a combination of numerical values, mathematical operators,
variables,
and function calls. On other words, expression can involve:
manual entry
built-in functions
user defined functions
Table 1.1 gives the partial list of arithmetic operators.
>> 142
me 5 Table 1.1: Basic arithmetic operators
as [SYMBOL OPERATION EXAMPLE |
6 + Addition
= Subtraction
co Multiplication
DivisionCollege of Electronics Eng .
Electronic Eng. Dep.
2nd Year
MATLAB LAB
4.2 Overwriting variable
‘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:
eo
>
t=
6
4.3 Error messages
If we enter an expression incorrectly, MATLAB will return an error message. For
example,
in the following, we left out the multiplication sign, *, in the following expression
>> x= 10;
>> Sx
2 5x
I
Error: Unexpected MATLAB expression.
4.4 Controlling the hierarchy of operations or precedence
Let’s consider the previous arithmetic operation, but now we will include parentheses.
For
example, 1 +2 *3 will become (1 +2) * 3
>> (14293
ans=
9
and, from previous example
6
D> 14293
ans =
io
By adding parentheses, these two expressions give di®erent results: 9 and 7
‘The order in which MATLAB performs arithmetic operations is exactly that taught
n high school algebra courses. Exponentiations are done first, followed by
multiplications and divisions, and “finally by additions and subtractions. However, the
standard order of precedence of arithmetic operations can be changed by inserting
parentheses. For example, the result of 142*3 is quite di@erent than the similar
expression with parentheses (1+2)*3.
The results are 7 and 9 respectively. Parentheses can always be used to overrule priority,
and their use is recommended in some complex expressions to avoid ambiguity.College of Electronics Eng .
Electronic Eng. Dep. ,
2nd Year
MATLAB LAB
‘Therefore, to make the evaluation of expressions unambiguous, MATLAB has estab-
lished a series of rules. The order in which the arithmetic operations are evaluated is
given
in Table 1.2. MATLAB arithmetic operators obey the same precedence rules as those in
Table 1.2: Hierarchy of arithmetic operations Precedence Mathematical operations
Table 1.2: Hierarchy of arithmetic operations
PRECEDENCE MATHEMATICAL OPERATIONS
First ‘The contents of all parentheses are evaluated first, starting
from
he innermost parentheses and working outward.
Second All exponentials are evaluated, working from left: to right
‘Third All multiplications and divisions are evaluated, working
from left to right
Fourth All ndditions and subtractions are evaluated, starting
from left to right
Exampl
1 4 6
—4+2x2
243 5 7
4-5 Basic Mathematical Function
MATLAB offers many predefined mathematical functions for technical computing which
contains a large set of mathematical functions. There is a long list of mathematical
functions that are built into MATLAB. These functions are called built-ins. Many
standard mathematical functions, such as sin(x), cos(3), tan(x), ex, In(x), are evaluated by
the functions sin, cos, tan, exp, and log respectively in MATLAB. Table.1 lists some
commonly used functions, where variables x and y can be numbers, vectors, or matrices.
Table 1: Elementary functions
cos(x) Cosine abs(x) Absolute value
sin(@z) Sine siga(e) Signum function
tan(x) Tangent nax(x) — Maxiomun value
acoa(x) Are cosine nin) Minimum value
asin(x) Are sine ceil(e — Round towards +00
atan(x) Are tangent floor(x) Round towards —oe
exp(x) Exponential round(x) Round to nearest integer
sqrt(x) Square root ren(x) Remainder after division
Log(x) Natural logarithm | angle(x) Phase angle
1ogi0(x) Common logarithm | conj(x) Complex conjugate
% cour. GCollege of Electronics Eng .
Electronic Eng. Dep. ‘
2nd Year
MATLAB LAB
5-General Function
>> clear
The command clear or clear all removes all variables from the workspace. This
frees up system memory. In order to display a list of the variables currently in the
memory,
>> whos
while, whos will give more details which include size, space allocation, and class of the
variables.
>>format
Set precision output values
FORMAT SHORT Scaled fixed point format with 5 digits
FORMAT LONG Scaled fixed point format with 15 digits.
FORMATHEX Hexadecimal format.
Sy % prints the contents of y
y=133
> format short % changes format of output
>y
1.3300
>> format long % changes format of output again
sy
1.33000000000000
8- Entering multiple statements per line
It is possible to enter multiple statements per line. Use commas (,) or semicolons (;) to
enter more than one statement at once. Commas (,) allow multiple statements per line
without suppressing output.
; b=at2, c=at6