Fem - Matlab Basics
Fem - Matlab Basics
MATLAB Basics
What is MATLAB?
1
MATLAB
INTRODUCTION TO MATLAB SCEGE
BASICS
- Development Environment
- The MATLAB Mathematical Function Library
- The MATLAB Language
- The MATLAB Application Program Interface (API)
2
MATLAB
INTRODUCTION TO MATLAB SCEGE
BASICS
Development Environment
3
MATLAB
INTRODUCTION TO MATLAB SCEGE
BASICS
4
MATLAB
INTRODUCTION TO MATLAB SCEGE
BASICS
MATLAB LANGUAGE
5
MATLAB
INTRODUCTION TO MATLAB SCEGE
BASICS
GRAPHICS
- MATLAB has extensive facilities for displaying vectors
and matrices as graphs, as well as annotating and
printing these graphs.
- It includes high-level functions for two-dimensional and
three-dimensional data visualization, image processing,
animation, and presentation graphics.
- It also includes low-level functions.
6
MATLAB
INTRODUCTION TO MATLAB SCEGE
BASICS
7
MATLAB
INTRODUCTION TO MATLAB SCEGE
BASICS
8
MATLAB
FUNDAMENTAL CONCEPTS SCEGE
BASICS
Commands for Managing a Session
- Removes all the variables from the
clear workspace.
- Frees up system memory
- Displays the list of variables currently in
who
the memory.
- Will display more details which include
whos size, space, allocation and class of
variables
exist - Checks for existence of the variable.
global - Declares variable to be global.
help - Searches for help topic
lookfor - Searches help entries for keyword
quit - Stops the MATLAB
9
MATLAB
FUNDAMENTAL CONCEPTS SCEGE
BASICS
10
MATLAB
FUNDAMENTAL CONCEPTS SCEGE
BASICS
11
MATLAB
FUNDAMENTAL CONCEPTS SCEGE
BASICS
Mathematical Functions
12
MATLAB
FUNDAMENTAL CONCEPTS SCEGE
BASICS
Mathematical Functions
Elementary Functions
cos(x) Cosine abs(x) Absolute value
sin(x) Sine ceil(x) Round towards + Inf
tan(x) Tangent floor(x) Round towards - Inf
acos(x) Arc cosine round(x) Round
asin(x) Arc sine rem(x) Remainder after division
atan(x) Arc tangent angle(x) Phase
exp(x) Exponential conj(x) Complex Conjugate
sqrt(x) Square root imag (x) imaginary
Natural
log(x) real (x) Real number
Logarithm
Common
log10(x) primes (x) Prime Number
Logarithm 13
MATLAB
FUNDAMENTAL CONCEPTS SCEGE
BASICS
Mathematical Functions
Scalar Arithmetic
Symbol Operation
+ Addition
- Subtraction
* Multiplication
/ Right Division
\ Left Division
^ Exponentiation
14
MATLAB
FUNDAMENTAL CONCEPTS SCEGE
BASICS
Mathematical Functions
15
MATLAB
ARRAYS AND MATRICES SCEGE
BASICS
16
MATLAB
ARRAYS AND MATRICES SCEGE
BASICS
17
MATLAB
ARRAYS AND MATRICES SCEGE
BASICS
18
MATLAB
ARRAYS AND MATRICES SCEGE
BASICS
CREATING ARRAYS
A. ROW VECTOR
Type the elements inside a pair of square
brackets, separating the elements inside with a
space or a comma.
Example:
>>A = [10 –3 4 6] or
>>A = [10, –3, 4, 6]
19
MATLAB
ARRAYS AND MATRICES SCEGE
BASICS
B. COLUMN VECTOR
20
MATLAB
ARRAYS AND MATRICES SCEGE
BASICS
C. MATRICES
22
MATLAB
ARRAYS AND MATRICES SCEGE
BASICS
APPENDING ARRAYS
23
MATLAB
ARRAYS AND MATRICES SCEGE
BASICS
24
MATLAB
ARRAYS AND MATRICES SCEGE
BASICS
Example:
>>x = [2: 2: 8]
>>y = [1: 3: 11]
>>z = [20: –2: 10]
If q is omitted, it is presumed to be 1.
Example:
>>w = [–3: 2]
25
MATLAB
ARRAYS AND MATRICES SCEGE
BASICS
2. Using linspace
Linspace command creates a linearly spaced
row vector by specifying the number of
elements rather than the increment. The syntax
>>x = [a, b, n] means creating a vector x
where a and b are the lower and upper limits,
respectively and n is the number of elements.
Example:
>>linspace (5, 8, 31).
It contain 31 elements. If n is omitted, the
default value is 100.
26
MATLAB
ARRAYS AND MATRICES SCEGE
BASICS
3. Using logspace
It creates an array of logarithmically spaced
elements. The syntax is
>>logspace (a, b, n)
where n is the number of elements between
10a and 10b.
Example:
>>x = logspace (–1, 1, 6)
If n is omitted, the number of elements defaults
to 50.
27
MATLAB
ARRAYS AND MATRICES SCEGE
BASICS
30
MATLAB
ARRAYS AND MATRICES SCEGE
BASICS
ARRAY OPERATIONS
(Element-by-element operations)
Symbols Operations
+ Scalar-array addition
– Scalar-array subtraction
+ Array addition
– Array subtraction
.* Array multiplication
./ Array right division
.\ Array left division
.^ Array exponentiation 31
MATLAB
ARRAYS AND MATRICES SCEGE
BASICS
MATRIX OPERATIONS
Applies the same rule in ordinary matrices
operations in Mathematics.
Note:
1. + and – apply the same rule as in element-
by-element operations.
2. A/B = A*B-1 , where B-1 is the inverse of B.
3. A^2 = A*A
4. AB is not defined .
32
MATLAB
ARRAYS AND MATRICES SCEGE
BASICS
Determinants
33
MATLAB
GRAPHICS-2D SCEGE
BASICS
FIGURE WINDOWS
MATLAB directs graphics output to a
window called figure that is separate
from the command window. The figure
function creates figure windows.
Example: >>figure
34
MATLAB
GRAPHICS-2D SCEGE
BASICS
2-D Plotting
The plot function is used to produce two-
dimensional curves, using x- and y-data
matricesspecified by the user.
plot(xdata,ydata,’cml’)
35
MATLAB
GRAPHICS-2D SCEGE
BASICS
Examples:
>> x = 0:10 ;
>> y = 2*x + 3;
>> plot(x,y)
>> y1 = 4*x – 2;
>> y2 = x + 2;
>> plot(x,y1,x,y2)
36
MATLAB
GRAPHICS-2D SCEGE
BASICS
Adding a Grid
grid on creates a
grid on the current
figure
37
MATLAB
GRAPHICS-2D SCEGE
BASICS
38
MATLAB
GRAPHICS-2D SCEGE
BASICS
39
MATLAB
GRAPHICS-2D SCEGE
BASICS
40
MATLAB
GRAPHICS-2D SCEGE
BASICS
Example:
subplot(2,2,i)
1 2
where i = 1 to 4
>> subplot(2,2,1)
>> subplot(2,2,2)
>> subplot(2,2,3) 3 4
>> subplot(2,2,4)
42
MATLAB
GRAPHICS-2D SCEGE
BASICS
ezplot
ezplot is an easy to use function plotter for algebraic and
transcendental functions, parametric equations, implicit and
explicit functions.
ezplot(f)
plots the expression f = f(x) over the default
domain -2 < x <
ezplot(f,[a,b])
plots f = f(x) over a < x < b
Examples:
>> ezplot(‘cos(x)’)
>> ezplot(‘1/y-log(y)+log(-1+y)+x-1’)
>> ezplot(‘x^2+y^2-4’,[-3,3])
43
MATLAB
LIVE SCRIPT SCEGE
BASICS
44
MATLAB
LIVE SCRIPT SCEGE
BASICS
• Code — This inserts a blank line of code into your live script. You can
insert a code line before, after, or between text lines.
• Text — This inserts a blank line of text into your live script. A text line
can contain formatted text, hyperlinks, images, or equations. You can
insert a text line before, after, or between code lines.
•Section Break — This inserts a section break into your live script. Insert
a section break to divide your live script into manageable sections that
you can evaluate individually. In live scripts, a section can consist of
code, text, and output. For more information, see
Run Sections in Live Scripts.
44
MATLAB
LIVE SCRIPT SCEGE
BASICS
• Equation — This inserts an equation into your live script. Equations can only be
added in text lines. If you insert an equation into a code line, MATLAB places the
equation in a new text line directly under the selected code line. For more
information, see Insert Equations into Live Scripts
• Hyperlink — This inserts a hyperlink into your live script. Hyperlinks can only be
added in text lines. If you insert a hyperlink into a code line, MATLAB places the
hyperlink in a new text line directly under the selected code line.
• Image — This inserts an image into your live script. Images can only be added in
text lines. If you insert an image into a code line, MATLAB places the image in a
new text line directly under the selected code line.
Format Text
You can further format text using any of the styles included in the Text
Style section. Use the down arrow to the right of the section to display all the
available text style options. Styles include Normal, Heading, Title, Bulleted List,
and Numbered List.
44
MATLAB
LIVE SCRIPT SCEGE
BASICS
44