Académique Documents
Professionnel Documents
Culture Documents
Matlab_Basics(tutorial)
AU_SS_Matlab_Tutorial 1
1.Matlab
Matlab is an interactive system for doing numerical computations.
AU_SS_Matlab_Tutorial 2
2.Starting Up
You should have a directory reserved for saving files associated with
Matlab.
Create such a directory (mkdir) if you do not have one.
Change into this directory (cd).
After a short pause, the logo will be shown followed by where >> is the
Matlab prompt.
Type help help for help and quit to exit from Matlab
AU_SS_Matlab_Tutorial 3
3.Matlab as a Calculator
AU_SS_Matlab_Tutorial 4
AU_SS_Matlab_Tutorial 5
4.Numbers and Formats
AU_SS_Matlab_Tutorial 6
All computations in MATLAB are done in double precision, which means
about 15 significant figures.
The formathow Matlab prints numbersis controlled by the format
AU_SS_Matlab_Tutorial 7
5.Variables
AU_SS_Matlab_Tutorial 8
so that x has the value 13 and y = 65.
These can be used in subsequent calculations.
These are examples of assignment statements: values are assigned to
variables.
Each variable must be assigned a value before it may be used on the right
of an assignment statement.
AU_SS_Matlab_Tutorial 9
5.1.Variable Names
Legal names consist of any combination of letters and digits, starting with a
letter. These are allowable:
AU_SS_Matlab_Tutorial 10
6.Suppressing Output
One often does not want to see the result of intermediate calculations
terminate the assignment statement or expression with semicolon
AU_SS_Matlab_Tutorial 11
Exercise 6.1
In each case find the value of the expression in Matlab and explain precisely
the order in which the calculation was performed
AU_SS_Matlab_Tutorial 12
7.Built in Functions
7.1 Trigonometric Functions
AU_SS_Matlab_Tutorial 13
7.2 Other Elementary Functions
AU_SS_Matlab_Tutorial 14
8.Vectors
These come in two flavours and we shall first describe row vectors: they
are lists of numbers separated by either commas or spaces.
The number of entries is known as the length of the vector and the entries
are often referred to as elements or components of the vector.
The entries must be enclosed in square brackets.
AU_SS_Matlab_Tutorial 15
We can do certain arithmetic operations with vectors of the same length,
such as v and v3 in the previous section.
AU_SS_Matlab_Tutorial 16
A vector may be multiplied by a scalar (a numbersee v4 above), or
added/subtracted to another vector of the same length.
The operations are carried out element wise.
AU_SS_Matlab_Tutorial 17
AU_SS_Matlab_Tutorial 18
8.1.The Colon Notation
This is a shortcut for producing row vectors:
AU_SS_Matlab_Tutorial 19
AU_SS_Matlab_Tutorial 20
AU_SS_Matlab_Tutorial 21
8.3.Column Vectors
These have similar constructs to row vectors. When defining them, entries
are separated by ; or new lines
AU_SS_Matlab_Tutorial 22
8.4.Transposing
We can convert a row vector into a column vector (and vice versa) by a
process called transposingdenoted by .
AU_SS_Matlab_Tutorial 23
AU_SS_Matlab_Tutorial 24
AU_SS_Matlab_Tutorial 25
9.Keeping a record
AU_SS_Matlab_Tutorial 26
10.Ploting Functions
AU_SS_Matlab_Tutorial 27
AU_SS_Matlab_Tutorial 28
AU_SS_Matlab_Tutorial 29
10.1.Plotting-Titles and Labels
AU_SS_Matlab_Tutorial 30
10.Grids
>> grid
AU_SS_Matlab_Tutorial 31
10.3 Line Styles & Colours
AU_SS_Matlab_Tutorial 32
10.4 Multiplots
AU_SS_Matlab_Tutorial 33
AU_SS_Matlab_Tutorial 34
10.5 Hold
AU_SS_Matlab_Tutorial 35
10.6 Subplot
AU_SS_Matlab_Tutorial 36
AU_SS_Matlab_Tutorial 37
10.7 Controlling Axes
AU_SS_Matlab_Tutorial 38
AU_SS_Matlab_Tutorial 39
Flow Control
If Statement
If logical expression
statements
end
If - else - end
AU_SS_Matlab_Tutorial 40
For
While
AU_SS_Matlab_Tutorial 41
What Is the Symbolic Math Toolbox?
The Symbolic Math Toolbox incorporates symbolic computation into the
numeric environment of MATLAB.
AU_SS_Matlab_Tutorial 42
Symbolic Objects
The Symbolic Math Toolbox defines a new MATLAB data type called a
symbolic object. Internally, a symbolic object is a data structure that stores a
string representation of the symbol.
AU_SS_Matlab_Tutorial 43
AU_SS_Matlab_Tutorial 44
When you create a fraction involving symbolic objects, MATLAB records the
numerator and denominator. For example:
If you add the same fractions as symbolic objects, MATLAB finds their
common denominator and combines them by the usual procedure for adding
rational numbers:
The sym command lets you construct symbolic variables and expressions.
For example, the commands
Suppose you want to use a symbolic variable to represent the golden ratio
AU_SS_Matlab_Tutorial 46
AU_SS_Matlab_Tutorial 47
AU_SS_Matlab_Tutorial 48
Note To create a symbolic expression that is a constant, you must use the
sym command. For example, to create the expression whose value is 5,
enter f = sym('5'). Note that the command f = 5 does not define f as a
symbolic expression.
If you set a variable equal to a symbolic expression, and then apply the
syms command to the variable, MATLAB removes the previously defined
expression from the variable. For example,
AU_SS_Matlab_Tutorial 49
The findsym Command
AU_SS_Matlab_Tutorial 50
The subs Command
You can substitute a numerical value for a symbolic variable using the subs
command.
AU_SS_Matlab_Tutorial 51
Symbolic and Numeric Conversions
t = 0.1
The sym function has four options for returning a symbolic representation of
the numeric value stored in t. The 'f' option
sym(t,'f')
'1.999999999999a'*2^(-4)
sym(t,'r')
1/10
AU_SS_Matlab_Tutorial 52
This is the default setting for sym. That is, calling sym without a second
argument is the same as using sym with the 'r' option:
sym(t)
ans =
1/10
The third option 'e' returns the rational form of t plus the difference between
the theoretical rational expression for t and its actual (machine) floating-point
value in terms of eps (the floating-point relative accuracy):
sym(t,'e')
ans =
1/10+eps/40
AU_SS_Matlab_Tutorial 53
Creating Symbolic Math Functions
Create an M-file
AU_SS_Matlab_Tutorial 54
Using the Symbolic Math Toolbox
Differentiation
To illustrate how to take derivatives using the Symbolic Math Toolbox, first
create a symbolic expression:
AU_SS_Matlab_Tutorial 55
AU_SS_Matlab_Tutorial 56
In this example, MATLAB automatically simplifies the answer. However, in
some cases, MATLAB might not simply an answer, in which case you can
use the simplify command.
Note that to take the derivative of a constant, you must first define the
constant as a symbolic expression. For example, entering
AU_SS_Matlab_Tutorial 57
Derivatives of Expressions with Several Variables
The diff command then calculates the partial derivative of the expression
with respect to that variable.
AU_SS_Matlab_Tutorial 58
AU_SS_Matlab_Tutorial 59
If you do not specify a variable to differentiate with respect to, MATLAB
chooses a default variable.
For one-letter variables, the default variable is the letter closest to x in the
alphabet.
To determine the default variable that MATLAB differentiates with respect to,
use the findsym command:
AU_SS_Matlab_Tutorial 60
AU_SS_Matlab_Tutorial 61
More Examples
AU_SS_Matlab_Tutorial 62
Limits
AU_SS_Matlab_Tutorial 63
AU_SS_Matlab_Tutorial 64
Integration
int(f)
That is, int(f) returns the indefinite integral or antiderivative of f (provided one
exists in closed form).
Similar to differentiation,
int(f,v)
uses the symbolic object v as the variable of integration, rather than the
variable determined by findsym. See how int works by looking at this table.
AU_SS_Matlab_Tutorial 65
AU_SS_Matlab_Tutorial 66
In contrast to differentiation, symbolic integration is a more complicated task.
The antiderivative may exist, but the software cant find the it.
The software could find the antiderivative on a larger computer, but runs out
of time or memory on the available machine.
AU_SS_Matlab_Tutorial 67
AU_SS_Matlab_Tutorial 68
AU_SS_Matlab_Tutorial 69
AU_SS_Matlab_Tutorial 70
Symbolic Summation
You can compute symbolic summations, when they exist, by using the
symsum command. For example, the p-series
AU_SS_Matlab_Tutorial 71
Taylor Series
The statements
AU_SS_Matlab_Tutorial 72
AU_SS_Matlab_Tutorial 73
Calculus Example
The function in this example is
AU_SS_Matlab_Tutorial 74
AU_SS_Matlab_Tutorial 75
Finding the Asymptotes
AU_SS_Matlab_Tutorial 76
AU_SS_Matlab_Tutorial 77
AU_SS_Matlab_Tutorial 78
AU_SS_Matlab_Tutorial 79
Using MATLAB to Compute the Fourier Transform
MATLAB has the built-in fourier and ifourier functions to compute the Fourier
transform and its inverse.
Their descriptions and examples, can be displayed with the help fourier and
help ifourier commands.
In examples below present some Fourier transform pairs, and how they are
verified with MATLAB.
Example.1
AU_SS_Matlab_Tutorial 80
This time function, like the time function of Example , is its own Fourier
transform multiplied by the constant
AU_SS_Matlab_Tutorial 81
Example.2
Example.3
AU_SS_Matlab_Tutorial 82
Example.4
Ex.
AU_SS_Matlab_Tutorial 83
Solution
AU_SS_Matlab_Tutorial 84
Ex.
Solution
AU_SS_Matlab_Tutorial 85
AU_SS_Matlab_Tutorial 86