Vous êtes sur la page 1sur 13

MATLAB

lecture 1
Olawale B. Akinwale
Dept of Electronic & Electrical
Engineering
Obafemi Awolowo University, Ile-Ife
Introduction to MATLAB
http://scv.bu.edu/documentation/tutorials/MATLAB/

• What is MATLAB
• MATLAB Characteristics
• MATLAB Preliminaries
• Rules on Variable and Function Names
• Special Characters
• Basic Arithmetic Operators
• Elementary Math Intrinsic Functions
• File Types
• Defining matrices via the vector technique
• Graphics
What is MATLAB ?
• It stands for MATrix LABoratory
• It is developed by The Mathworks, Inc.
(http://www.mathworks.com)
• It is an interactive, integrated, environment
– for numerical computations
– for symbolic computations
– for scientific visualizations
• It is a high-level programming language
• Program runs in interpreted, as opposed to
compiled, mode
Characteristics of MATLAB
• Programming language based (principally) on matrices.
• Slow (compared with fortran or C) because it is an interpreted
language, i.e. not pre-compiled. Avoid for loops whenever possible.
• Automatic memory management, i.e., you don't have to declare
arrays in advance.
• Intuitive, easy to use.
• Shorter program development time than traditional programming
languages such as Fortran and C.
• Can be converted into C code via MATLAB compiler for better
efficiency.
• Many application-specific toolboxes available.
• Coupled with Maple for symbolic computations.
MATLAB Preliminaries
• If MATLAB is successfully invoked, prompt becomes ">>".
• Enter "quit" at MATLAB prompt to exit ( >> quit).
• Online help.
>> help help % instructions on how to get help.
>> help % lists available packages/toolboxes on system.
>> help package_name % lists available functions in named package.
>> help function_name % instructions on named function.
>> lookfor keywords % search for keywords that best describe the
function.
>> helpdesk % html format docs.
• More online help
• If you can't find what you need with the above help commands, you
can go to the Mathworks website to search further
(www.matlabcentral.com).
Rules on Variable and Function Names
Variable/Function name
• begins with a LETTER, e.g., A2z.
• can be a mix of letters, digits, and underscores (e.g., vector_A, but not
vector-A (since "-" is a reserved char).
• is case sensitive, e.g., NAME, Name, name are 3 distinct variables.
• must not be longer than 31 characters.
Suggestion: Since MATLAB distinguishes one function from the next by their
file names, name files the same as function names to avoid confusion. Use
only lowercase letter to be consistent with MATLAB's convention.
• File name
• Files that contain MATLAB commands should be named with
a suffix of ".m", e.g., something.m. These include, but not
restricted to, script m-files and function m-files. (See "Files
Types" for details)
Note: To use it, just refer to it by name, without the suffix,
e.g.,
>> something
Special Characters
There are a number of special reserved characters used in MATLAB for various purposes. Some of these
are used as arithmetic operators, namely, +, -, *, / and \. While others perform a multitude of purposes:
% -- anything after % (and until end of line) is treated as comments, e.g.,
>> x = 1:2:9; % x = [1 3 5 7 9];
•; -- delimits statements; suppresses screen output, e.g.,
>> x = 1:2:9; y = 2:10; % two statements on the same line
• ... -- statement continuation, e.g.,
>> x = [ 1 3 5 ...
7 9]; % x = [1 3 5 7 9] split into 2 lines
• : -- range delimiter, e.g.,
>> x = [1:2:9]; % x=[1,3,5,7,9]
• ' -- matrix transposition, e.g.,
>> x = [1:2:9]'; % x changed from row vector to column vector
If the vector/matrix is complex, "'" results in complex conjugation and matrix transposition.
• , -- command delimiter, e.g.,
>> x = [1:2:9], y = [1:9] % two statements on the same line
• . -- precede an arithmetic operator to perform an elemental operation, instead of matrix operation,
>> x = 3:3:9
x=
3 6 9
>> y = 3*ones(3,1)‘
y=
3 3 3
>> z =x./y
z=
1 2 3

Note that many of these characters have multiple functionalities


(function overloading) depending on the context, e.g., "*" is used
for scalar multiply, matrix multiply and "wild card" as seen above.
Elementary Math Intrinsic Functions
>> abs(x) % absolute value of x
>> exp(x) % e to the x-th power
>> fix(x) % rounds x to integer towards 0
>> log10(x) % common logarithm of x to the base 10
>> rem(x,y) % remainder of x/y
>> sqrt(x) % square root of x
>> sin(x) % sine of x; x in radians
>> acoth(x) % inversion hyperbolic cotangent of x
>> help elfun % get a list of all available elementary
functions
File Types
• script m-files ( variables global).
• mat-file
• fig-file
• mdl-file
Graphics
MATLAB is an interactive environment in which you can program as well as
visualize your computations. It includes a set of high-level graphical functions
for:
• Line plots (plot, plot3, polar)
• Bar graphs (bar, barh, bar3, bar3h, hist, rose, pie, pie3)
• Surface plots (surf, surfc)
• Mesh plots (mesh, meshc, meshgrid)
• Contour plots(contour, contourc, contourf)
• Animation (moviein, movie)
MATLAB also provides for low-level control in the event that the user wishes
to have better control of the graphical output.

In addition, MATLAB also provides a Graphical User Interface (GUI) toolbox


called Guide for the creation of push buttons, text boxes, etc. for more user-
friendly input to your m-files.
• Examples

Vous aimerez peut-être aussi