Вы находитесь на странице: 1из 20

IM 512 3.

0 (I) Mathematical Software

Dr. TGI Fernando

September 16, 2011

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 1 / 20
Introduction to MATLAB

Introduction to MATLAB

Mathematical software package for high-performance numerical


calculations and visualization
Has an interactive environment
Built-in functions for technical computation, graphics, and simulation
High-level programming language
MATLAB - MATrix LABoratory
Faster than with traditional programming languages, such as C,
C++, and Fortran

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 2 / 20
Introduction to MATLAB Main features

Main features

Built-in functions provide tools for


Linear algebra computations
Data analysis
Signal processing
Optimization
Numerical solutions of ODEs
Etc.
Numerous functions for 2D and 3D graphics
There is an internal interface to run programs that are written in C or
Fortran
Not limited to built-in functions, users can write their own functions
and run like built-in functions

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 3 / 20
Introduction to MATLAB Toolboxes

Toolboxes

There are “optional” toolboxes


Toolbox - collection of functions written for special applications
Symbolic computation
Image processing
Statistics
Control system design
Neural networks
Etc.

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 4 / 20
Introduction to MATLAB Symbolic calculations

Symbolic calculations

What is symbolic calculations?


Symbolic algebra means that computation is done in terms of symbols or
variables rather than numbers.
E.g. if you type the expression (x + y )2 on your computer and the
computer responds by saying that the expression is equal to x 2 + 2xy + y 2 ,
then your computer does symbolic algebra.
Matlab is primarily a numerical computation package.
There are some other packages for symbolic algebra (e.g. Maple,
Mathematica).
However, the Symbolic Math Toolbox in MATLAB can do symbolic
algebra.

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 5 / 20
Basics of MATLAB MATLAB Windows

MATLAB Windows

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 6 / 20
Basics of MATLAB MATLAB Windows

Subwindows

Command Window:
This is the main window.
It is characterized by the MATLAB command prompt ().
When you launch MATLAB program, MATLAB puts you in this
window.
All commands are typed in this window at the MATLAB prompt.

Current Directory pane:


Located on the left the Command Window.
You can do file navigations here.
The current working directory is reflected in the little window above
the Command Window marked Current Directory.

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 7 / 20
Basics of MATLAB MATLAB Windows

Subwindows (Cont.)

Workspace pane:

Lists all variables that you generated so far.


Shows their type and size.
By clicking on a variable and then right-clicking, you can do various
things such as plotting.

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 8 / 20
Basics of MATLAB MATLAB Windows

Subwindows (Cont.)

Command History pane:


All commands typed on the MATLAB prompt in the command
window get recorded even across multiple sessions.
You can select a previously typed command by double-clicking the
respective command in the Command History pane.
You can select multiple commands from the Command History pane
and save them in a M-file by right-clicking on the selected commands.

Figure Window
The output of all graphics commands typed in the command window
are flushed to the graphics or figure window.

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 9 / 20
Basics of MATLAB MATLAB Windows

Subwindows (Cont.)

Editor Window:
This is where you write, edit, create, and save your own programs in
files called M-files.
You can use any text editor to carry out these tasks (e.g. Notepad in
MS Windows, vi in Linux).
However, MATLAB provides its own editor.
To get this editor type ‘edit’ on the MATLAB prompt and press the
‘ENTER’ key.
To get the Notepad type ‘! notepad’ and press ‘ENTER’ key.

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 10 / 20
Basics of MATLAB Input-output

Input-output

Supports interactive computation


Take the input from the keyboard, and flush the output to the screen
immediately.
In addition, it can read input files and write results into output files.
Data type:
Arrays - fundamental data type in Matlab
Encompasses several distinct data objects - integers, double, matrices,
character strings, structures, and cells.
No need to declare a variable as real or complex.
Matlab automatically identifies the entered value.

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 11 / 20
Basics of MATLAB Input-output

Input-output (Cont.)

Dimensioning:
Automatic in Matlab.
No dimension statements are required for vectors or arrays.
size and length (for vectors only) commands - returns the dimension
of a vector or a matrix.
Case sensitivity:
Differentiates between lowercase and uppercase letters.
E.g. a and A are different variables.
Most Matlab commands and built-in function calls are typed in
lowercase letters.

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 12 / 20
Basics of MATLAB Input-output

Input-output (Cont.)

Output display:
The output of the every command is displayed.
A semicolon (;) at the end of the command suppresses the screen
output (except graphics and on-line help commands).
Paged output - Type more on at the Matlab prompt to show one
screen of output at a time.

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 13 / 20
Basics of MATLAB Input-output

Input-output (Cont.)

Output format: There are different screen output formats. The following
table shows the printed value of 10π in seven different formats.

format short 31.4159


format short e 3.1416e+001
format long 31.415926553589793
format long e 3.141592653589793e+001
format short g 31.416
format long g 31.4159265358979
format bank 31.42

Default format - format short


format compact and format loose - control the spacing above and below
the displayed lines.

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 14 / 20
Basics of MATLAB Input-output

Input-output (Cont.)

Command history:
Matlab saves previously typed commands in a buffer.
These commands can be recalled with the up-arrow key (↑).
Previous commands can be recalled by typing the first few characters
and then pressing the ↑ key.
Alternatively you can double click on a command in the Command
History pane.

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 15 / 20
Basics of MATLAB File Types

File types

M-files
ASCII text files, with a .m extension.
Two types - script files and function files.
Most programs are saved as M-files.
All built-in functions are M-files.
MAT-files
Binary data files, with a .mat extension.
Created when you save data with the save command.
Can read only Matlab (with the load command).

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 16 / 20
Basics of MATLAB File Types

File Types (Cont.)

Fig-files
Binary figure files, with a .fig extension.
Use Save or Save As options from the File menu or saveas
command.
Opened with the open filename.fig.
P-files
Compiled M-files with a .p extension.
Created with the pcode command.
Can run the P-file without the source code (M-file).

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 17 / 20
Basics of MATLAB Working Directory

Working Directory

Matlab creates a default folder called Matlab inside My Documents.


All your files save in this folder unless you specify any other folder.
If you need to store files somewhere else, you might have to specify
the path.
path command
Change the working directory with a few navigational clicks in the
Current Directory pane.

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 18 / 20
Basics of MATLAB General commands

General commands

On-line help
help - lists topics on which help is available
helpwin - opens the interactive help window
helpdesk - opens the web browser based help facility
help topic - provides help on topic
Workspace information
who - lists variables currently in the workspace
whos - lists variables currently in the workspace with their size
clear - clears the workspace, all variables are removed
clear x, y , z - clears only variables x, y and z
clear all - clears all variables and functions from workspace
mlock fun - locks fun so that clear cannot remove it

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 19 / 20
Basics of MATLAB General commands

General commands (Cont.)


Workspace information (Cont.)
munlock fun - unlocks function fun so that clear can remove it
clc - clears command window, cursor moves to the top
home - scrolls the command window to put the cursor on top
clf - clears figure window
Directory information
pwd - shows the current working directory
cd - changes the current working directory
dir - lists contents of the current directory
mkdir - creates a directory
Termination
Control-c - local abort, kills the current command execution
quit or exit - quits Matlab
Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 20 / 20

Вам также может понравиться