NUMERICAL METHODS
NUMERICAL METHODS
AND STATISTICS
AND STATISTICS
INTRODUCTION TO MATLAB
INTRODUCTION TO MATLAB
Joanna Iwaniec
WHAT IS MATLAB?
MATLAB: technical computing language for high-performance numerical
computations and data visualization.
It was originally designed for solving linear algebra type problems using
matrices. It s name is derived from MATrix LABoratory.
MATLAB has since been expanded and now has built-in functions for
solving problems requiring data analysis, signal processing, optimization,
and several other types of scientific computations.
The emphasis is put on visualization and has 2, 3 and 4-dimensional
(animated) graphing capabilities.
Matrix is a basic data element used in MATLAB.
Can be applied to solving problems in any field that involves mathematics:
engineering
physics
chemistry
finances
STRENGTHS OF MATLAB
Although the syntax of MATLAB is C-like (especially when
creating loops), learning MATLAB is much easier than learning C.
MATLAB code is optimized to be relatively quick when performing
matrix operations.
MATLAB may behave like a calculator or as a programming
language.
MATLAB is interpreted, errors are easier to fix.
Although primarily procedural, MATLAB does have some object-
oriented elements.
WEAKNESSES OF MATLAB
MATLAB is NOT a general purpose programming language.
MATLAB is an interpreted language (making it for the most part
slower than a compiled language such as C++).
PROGRAMMING LANGUAGE / MACHINE
LANGUAGE
Programming language (Modern Science): In computer technology, a set of
conventions in which instructions for the machine are written. There are many
languages that allow humans to communicate with computers; FORTRAN, BASIC,
and Pascal are some common ones .
Programming language (Computer Encyclopedia): A language used to write
instructions for the computer. It lets the programmer express data processing in a
symbolic manner without regard to machine-specific details .
Machine language: programmer has to write the sequences of digits that the
computer executed. E.g assume we want to compute the absolute value of A + B - C,
where A is the value at machine address 3012, B is the value at address 3013, and C is
the value at address 3014, and then store this value at address 3015. However:
this manner is difficult
explicit memory locations must be written, and it is not always obvious if
simple errors are present. For example, at location 02347, writing 101& instead
of 111& would compute |A + B + C| rather than what was desired. This is not
easy to detect.
ASSEMBLY LANGUAGE / HIGH-LEVEL LANGUAGE
Assembly language: by naming all locations with easy-to-remember names, and by
using symbolic names for machine instructions, some of the difficulties of machine
programming can be eliminated. A relatively simple program called an assembler
converts this symbolic notation into an equivalent machine language program. The
symbolic nature of assembly language greatly eased the programming but:
programs were still very hard to write,
mistakes were still common,
programmers were forced to think in terms of the computer's architecture
rather than in the domain of the problem being solved.
High-level language: the concept is that if we want to compute |A + B - C|, and store
the result in a memory location called D, all we have to do is write D = |A + B - C|
and let a computer program, the compiler, convert that into the sequences of numbers
that the computer could execute.
GETTING STARTED WITH MATLAB
A prompt appears on the screen and
Workspace
a MATLAB statement can be
entered. When the
key is
pressed, the statement is executed,
and another prompt appears.
If a statement is terminated with a
semicolon ( ; ), no results will be
displayed. Otherwise results will
appear before the next prompt.
Command History Command Window
MATLAB VARIABLES
To perform simple arithmetic, you can enter an expression or equation at the
MATLAB command prompt:
5*6
MATLAB computes the product and returns the result, which, unless you
create your own variable, is stored in the default variable ans:
ans =
30
To create your own variable simply type them. For instance if you type:
product=5*6
MATLAB responds
product =
30
MATLAB VARIABLES - EXAMPLES
MATLAB VARIABLE NAMES
Variable names ARE case sensitive
Variable names can contain up to 63 characters (MATLAB 6.5 and
newer).
MATLAB SPECIAL VARIABLES
ans Default variable name for results
pi Value of Ą
eps Smallest incremental number
inf Infinity
NaN Not a number e.g. 0/0
i and ji = j = square root of -1
realmin The smallest usable positive real number
realmax The largest usable positive real number
MATLAB MATH & ASSIGNMENT OPERATORS
Power ^ or .^ a^b or a.^b
Multiplication * or .* a*b or a.*b
Division / or ./ a/b or a./b
or \ or .\ b\a or b.\a
NOTE: 56/8 = 8\56
Addition +a + b
Subtraction -a - b
Assignment = a = b (assign b to a)
MATLAB MATRICES
MATLAB treats all variables as matrices. For our purposes a matrix can
be thought of as an array, in fact, that is how it is stored.
Vectors are special forms of matrices and contain only one row OR one
column.
Scalars are matrices with only one row AND one column.
A matrix with only one row AND one column is a scalar. A scalar can be
created in MATLAB as follows:
a_value=23
a_value =
23
A matrix with only one row is called a row vector. A row vector can be
created in MATLAB as follows (note the commas):
rowvec = [12 , 14 , 63]
rowvec =
12 14 63
MATLAB MATRICES
A matrix with only one column is called a column vector. A column
vector can be created in MATLAB as follows (note the semicolons):
colvec = [13 ; 45 ; -2]
colvec =
13
45
-2
A matrix can be created in MATLAB as follows (note the commas
AND semicolons):
matrix = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9]
matrix =
1 2 3
4 5 6
7 8 9
MATLAB MATRIX INDEXES
MATLAB CREATING MATRICES
RESHAPING MATRICES USING (:)
OTHER RESHAPING FUNCTIONS
STATISTICAL DATA MANIPULATION
MATRIX OPERATORS
LEFT DIVISION
COMPUTATIONAL TIME
Use TIC and TOC to find time that
an operation takes.
BOOLEAN OPERATORS
STRING ARRAYS
MATRIX
CONCATENATION
INDEX SUBSCRIPTING
LOGICAL SUBSCRIPTING
Task: extract density values that correspond to positive mass values.
SOME USEFUL MATLAB COMMANDS
who List known variables
whos List known variables plus their size
help Ex: >> help sqrt Help on using sqrt
lookfor Ex: >> lookfor sqrt Search for
keyword sqrt in m-files
what Ex:>> what a: List MATLAB files in a:
clear Clear all variables from work space
clear x y Clear variables x and y from work space
clc Clear the command window
SOME USEFUL MATLAB COMMANDS
what List all m-files in current directory
dir, ls List all files in current directory
cd a Change directory to a:
chdir a Same as cd
pwd Show current directory
which test Display current directory path to test.m
A USELESS, BUT INTERESTING, MATLAB COMMAND
why In case you ever needed a reason
MATLAB RELATIONAL OPERATORS
MATLAB supports six relational operators:
less than <
less than or equal <=
greater than >
greater than or equal >=
equal to ==
not equal to ~=
MATLAB LOGICAL OPERATORS
not ~ % highest precedence
and & % equal precedence with
or | % equal precedence with and
MATLAB DISPLAY FORMATS
format long 16 digits
format short e 5 digits plus exponent
format long e 16 digits plus exponent
format hex hexadecimal
format bank two decimal digits
format + positive, negative or zero
format rat rational number (215/6)
format short default display
GETTING HELP
INTRODUCTION TO PROGRAMMING
THE IF, ELSEIF, ELSE STATEMENTS
Works on Conditional statements.
Once a condition is true, the sequence
terminates.
THE FOR LOOP
Repeats loop a set number of times
(based on index).
Can be nested.
WHILE LOOP, SWITCH CONSTRUCTION
MATLAB SCRIPT
A SCRIPT file is an external file that contains a sequence of MATLAB
statements.
By typing the filename, subsequent MATLAB input is obtained from the
file.
SCRIPT files have a filename extension of ".m" and are often called "M-
files".
MATLAB FUNCTION
MATLAB FUNCTION
MATLAB SUBFUNCTIONS
THANK YOU FOR YOUR ATTENTION!
Wyszukiwarka
Podobne podstrony:
Matlab Introducing to Matlab and it s Graphics Capabilities
CSharp Introduction to C# Programming for the Microsoft NET Platform (Prerelease)
Introduction to multivariate calibration in analytical chemistry
Smirnov, A V Introduction to tensor calculus (2004)
Introduction to Lean for Poland
Introduction To Human?sign
Introduction to Microprocessors and Microcontrollers
An introduction to difference equation by Elaydi 259
introduction to riemannian geometry
Introduction to Probability Solutions Manual
Introduction to Network Self defense technical and judicial issues
An introduction to the Analytical Writing Section of the GRE
introduction to russia
Introduction to MEMS gyroscopes
IT 0550 US Army Introduction to the Intelligence Analyst
Introduction to Wind Energy Systems;Basics,Technology & Operation Wagner & Mathur (Springer 2009)(90
więcej podobnych podstron