Vous êtes sur la page 1sur 86

School of Engineering and Information Technology

Department of Electrical Engineering

Matlab_Basics(tutorial)

AU_SS_Matlab_Tutorial 1
1.Matlab
Matlab is an interactive system for doing numerical computations.

It has since evolved into a successful commercial software package.


Matlab relieves you of a lot of the mundane tasks associated with solving
problems numerically.
This allows you to spend more time thinking, and encourages you to
experiment.
Matlab makes use of highly respected algorithms and hence you can be
confident about your results.
Powerful operations can be performed using just one or two commands.
You can build up your own set of functions for a particular application.

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

command. Type help format for full list.

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

A dotted grid may be added by

>> grid

This can be removed using either grid again, or grid off

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

There are many problems which need to execute different sets of


commands if a specified condition is true or false.

If Statement

Is a conditional selection statement, it is used to test a condition or ask a


question:

If logical expression

statements

end

To perform switching actions use If statement in combination with else or


elseif

If - else - end
AU_SS_Matlab_Tutorial 40
For

The for loop repeats a group of statements a fixed, predetermined number


of times.

While

The while loop repeats a group of statements an indefinite number of times


under control of a logical condition.

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.

The Symbolic Math Toolbox uses symbolic objects to represent symbolic


variables, expressions, and matrices.

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 Symbolic Math Toolbox enables you to perform a variety of symbolic


calculations that arise in mathematics and science.
AU_SS_Matlab_Tutorial 45
Creating Symbolic Variables and Expressions

The sym command lets you construct symbolic variables and expressions.
For example, the commands

create a symbolic variable x that prints as x and a symbolic variable a that


prints as alpha.

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

To determine what symbolic variables are present in an expression, use the


findsym command.

For example, given the symbolic expressions f and g defined by

AU_SS_Matlab_Tutorial 50
The subs Command

You can substitute a numerical value for a symbolic variable using the subs
command.

For example, to substitute the value x = 2 in the symbolic expression,

AU_SS_Matlab_Tutorial 51
Symbolic and Numeric Conversions

Consider the ordinary MATLAB quantity

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')

returns a symbolic floating-point representation

'1.999999999999a'*2^(-4)

The 'r' option

sym(t,'r')

returns the rational form

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

There are two ways to create functions:

Use symbolic expressions

Create an M-file

Using Symbolic Expressions

The sequence of commands

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

To differentiate an expression that contains more than one symbolic


variable, you must specify the variable that you want to differentiate with
respect to.

The diff command then calculates the partial derivative of the expression
with respect to that variable.

For example, given the symbolic expression

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.

In the preceding example, diff(f) takes the derivative of f with respect to t


because t is closer to x in the alphabet than s is.

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

To further illustrate the diff command, define a, b, x, n, t, and theta in the


MATLAB workspace by entering

AU_SS_Matlab_Tutorial 62
Limits

The fundamental idea in calculus is to make calculations on functions as a


variable gets close to or approaches a certain value.

Recall that the definition of the derivative is given by a limit

AU_SS_Matlab_Tutorial 63
AU_SS_Matlab_Tutorial 64
Integration

If f is a symbolic expression, then

int(f)

attempts to find another symbolic expression, F, so that diff(F) = 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.

A number of difficulties can arise in computing the integral:

The antiderivative, F, may not exist in closed form.

The antiderivative may define an unfamiliar function.

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.

Nevertheless, in many cases, MATLAB can perform symbolic integration


successfully. For example, create the symbolic variables

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

To find the horizontal asymptote of the graph of f, take the limit of f as x


approaches positive infinity:

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

Vous aimerez peut-être aussi