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

MATLAB

Introduction course

Fanilo RAMASOMANANA

ramasoma@unistra.fr

MATLAB - Introduction course

Context

• What is MATLAB?

• Objectives

• Introduction to Chapter 1:

Starting and Quitting MATLAB

MATLAB Desktop

Desktop Tools

MATLAB - Introduction course

What is MATLAB?

MATLAB is a commercial software of interactive calculus. It makes it possible to carry out numerical simulations based on algorithms of numerical analysis. It can thus be used for solving numerical problems for the approximate resolution of differential equations, partial differential equations, linear and nonlinear systems, or scientific and engineering graphics, etc… and to write programs in a lower level language.

MATLAB provides a block diagram tool for modeling and simulating dynamical systems, including signal processing, controls, communications, and other complex systems ranging from engineering and mathematics to chemistry, biology, and economics.

MATLAB - Introduction course

Objectives

The objective of this MATLAB's course is double:

• the knowledge of this software is essential because it is more and more used for calculus in industries and banks, …

• the knowledge of algorithms existing in the software and to test new algorithms.

MATLAB - Introduction course

Introduction

Chapter 1:

This chapter provides a brief introduction to starting and quitting MATLAB, and the tools and functions that help you to work with MATLAB variables and files.

MATLAB - Introduction course

Starting and Quitting MATLAB

Starting MATLAB:

On a Microsoft Windows platform, to start MATLAB, double- click the MATLAB shortcut icon on your Windows desktop.

Quitting MATLAB:

To end your MATLAB session, select Exit MATLAB from the File menu in the desktop, or type quit in the Command Window.

MATLAB - Introduction course

MATLAB Desktop

• When you start MATLAB, the MATLAB desktop appears,

containing tools for managing files, variables, and applications

associated with MATLAB.

• The first time MATLAB starts, the desktop appears as shown in the following illustration.

MATLAB - Introduction course

MATLAB Desktop

You can change the way your desktop looks by opening, closing, moving, and resizing the tools in it. You can also move tools outside of the desktop or return them back inside the desktop.

MATLAB - Introduction course

Desktop Tools

This section provides an introduction to MATLAB’s desktop tools. The tools are:

• “Command Window”

• “Command History”

• “Help Browser”

• “Current Directory”

• “Workspace”

• “Array Editor”

• “Create M-Files”

MATLAB - Introduction course

Desktop Tools

Command Window:

Use the Command Window to make calculations and run functions which exist in MATLAB and M- files (programs) created by us.

MATLAB - Introduction course

Desktop tools

Command History:

Lines you enter in the Command Window are logged in the Command History window. In the Command History, you can view previously used functions, and copy and execute selected lines.

MATLAB - Introduction course

Desktop tools

Help Browser:

Use the Help browser to search and view documentation for all MathWorks products. The Help browser is a Web browser integrated into the MATLAB desktop that displays HTML documents.

To open the Help browser, click the help button type helpbrowser in the Command Window.

in the toolbar, or

MATLAB - Introduction course

Desktop tools

Help Browser:

MATLAB - Introduction course

Desktop tools

Current Directory Browser:

MATLAB file operations use the current directory as reference points. Any file you want to run must be in the current directory.

To search for, view, open, and make changes to MATLAB-related directories and files, use the MATLAB Current Directory browser.

MATLAB - Introduction course

Desktop tools

Workspace Browser:

The MATLAB workspace consists of the set of variables (named arrays) built up during a MATLAB session and stored in memory. You add variables to the workspace by using functions, running M-files, and loading saved workspaces. To view the workspace and information about each variable, use the Workspace browser.

MATLAB - Introduction course

Desktop tools

Workspace Browser:

To delete variables from the workspace, select the variable and select Delete from the Edit menu. Alternatively, use the clear all function. The workspace is not maintained after you end the MATLAB session.

To save the workspace to a file that can be read during a later MATLAB session, select Save Workspace As from the File menu, or use the save function. This saves the workspace to a file called a MAT-file, which has a .mat extension.

To read in a MAT-file, select Import Data from the File menu, or use the load function.

MATLAB - Introduction course

Desktop tools

Workspace Browser (Array Editor):

Double-click on a variable in the Workspace browser to see it in the Array Editor.

MATLAB - Introduction course

Desktop tools

Create M-Files:

• To create M-files, select New M-files from the File menu,

which allow to write programs and run MATLAB functions.

We can save the new algorithms in a any directory in a file

called a M-file, which has a .m extension.

• If you just need to view the contents of an M-file, you can display it in the Command Window by using the name of the function or running this function.

MATLAB - Introduction course

Chapter 2

Operators for numbers

Special functions for numbers

Operators for vectors

Special functions for vectors

Operators for matrices

Special functions for matrices

MATLAB - Introduction course

Expressions for numbers

Operators for numbers:

 + Addition - Subtraction * Multiplication
 / Division ^ Power

Special functions:

 pi 3.1415… sqrt Square root i Imaginary unit log Logarithm Inf Infinity exp Exponentiel NaN Not a number sign Signe of the number abs Absolute value (=1 if positif and 0 if negatif)
MATLAB - Introduction course

Special functions

Trigonometric functions:

 sin Sine in radians sind Sine in degrees asin Inverse of the function Sine in radians asind Inverse of the function Sine in degrees cos Cosine in radians cosd Cosine in degrees acos Inverse of the function Cosine in radians

acosd Inverse of the function Cosine in degrees

MATLAB - Introduction course

Complex numbers

special functions:

 abs the magnitude of the complex number angle the phase angle of the complex number real the real part of the complex number imag the imaginary part of the complex number conj the conjugate of the complex number
MATLAB - Introduction course

Vectors

Create a vector:

 V=[2,3,7] is a row vector V=[2;3;7] is a column vector

V’ column to line.

change the vector from line to column or from

Operators for vectors:

+

-

*

.*

./

Subtraction

Scalar product Element by element multiplication Element by element division

MATLAB - Introduction course

Vectors

Colon operator (:)

X=1:10

is a row vector containing the integers from 1 to

10. By default the increment here is equal to 1.

For a nonunit spacing, we can specify an increment. For example:

X=100:-7:50 or X=0:pi/4:pi

MATLAB - Introduction course

Vectors

Special functions

ones(1,3) is a row vector all components is equal to 1.

ones(3,1) is a column vector all components is equal to 1.

zeros(1,3) is a row vector all components is equal to 0.

zeros(3,1) is a column vector all components is equal to 0.

rand(1,3) is a row vector with random components between

0 and 1.

sum(X) computes the sum of the elements of X.

find(X<3) gives the indexes of the elements of X < 3.

MATLAB - Introduction course

Matrices

Create a Matrix:

A=[1 2 3;4 5 6;7 8 9] A’

is a matrix 3x3 is the transpose of the matrix A

Operators for matrices:

 + Addition Matrix product - Subtraction * .* Element by element multiplication ./ Element by element division \ division of a matrix by a vector A\b.
MATLAB - Introduction course

Matrices

Special functions

ones(3,3) is a 3x3 matrix all components is equal to 1.

zeros(3,3) is a 3x3 matrix all components is equal to 0.

eye(3,3) is the identity matrix 3x3.

rand(3,3) is a 3x3 matrix with random components between

0 and 1.

sum(A(1,:)) computes the sum of the elements of the first row of A.

sum(A(:,2)) computes the sum of the elements of the second column of A.

MATLAB - Introduction course

Matrices

Special functions

sum(A(end,:)) computes the sum of the elements in the last row of A.

sum(A(:,end)) computes the sum of the elements in the last column of A.

find(A<3) gives the indexes of the elements of A < 3.

MATLAB - Introduction course

Matrices

Special functions

diag(A) computes the diagonal of A.

det(A) computes the determinant of A.

inv(A) computes the inverse of A.

eig(A) computes the eigenvalues of A.

poly(A) computes the coefficients of the characteristic polynomial of A.

MATLAB - Introduction course

Graphics

Plot of the function Sine

x=0:pi/100:2*pi;

y=sin(x);

plot(x,y)

We can label the axes and add a title:

xlabel(‘x’)

ylabel(‘Sine of x’)

title(‘Plot of the Sine function’)

MATLAB - Introduction course

Graphics

Multiple data sets in one graph

x=0:pi/100:2*pi;

y1=sin(x); y2=sin(x-0.25); y3=sin(x-0.5);

plot(x,y1,x,y2,x,y3)

The legend command provides an easy way to identify the individual plots:

legend(‘sin(x)’,’sin(x-0.25)’,’sin(x-0.5)’)

MATLAB - Introduction course

Graphics

Multiple data sets in one graph

We can also use this format:

figure(1);

plot(x,y1);

hold on;

plot(x,y2);

hold on;

plot(x,y3);

legend(‘sin(x)’,’sin(x-0.25)’,’sin(x-0.5)’)

hold off;

MATLAB - Introduction course

Graphics

Controlling the axes

By default, MATLAB finds the maxima and the minima of the data to choose the axis limits. The axis command enables you to specify your own limits:

axis([xmin xmax ymin ymax])

Or for three-dimensional graph:

axis([xmin xmax ymin ymax zmin zmax])

MATLAB - Introduction course

Graphics

Multiple plots in one figure

The subplot command enables you to display multiple plots in the same window or print them on the same piece of paper. Typing: subplot(m,n,p)

partitions the figure window into an m-by-n matrix of small subplots and selects the pth subplot for the current plot. The plots are numbered along first the top row of the figure window, then the second row, and so on.

MATLAB - Introduction course

Graphics

Multiple plots in one figure

Example:

subplot(2,2,1)

plot(x,y1);

hold off;

subplot(2,2,2)

plot(x,y2);

hold off;

Subplot(2,2,3)

Plot(x,y3);

hold off;

subplot(2,2,4);

plot(x,y4);

hold off;

MATLAB - Introduction course

Graphics

Saving a figure

To save a figure, select save from the file menu or click on the save button of the figure.

The figure is then saved as a fig file.

MATLAB - Introduction course

Chapter 3

Programming with MATLAB

if statements

for loops

while loops

MATLAB - Introduction course

Chapter 3

if

The if statement evaluates a logical expression and executes a group of statements when the expression is true. The optional elseif and else keywords provide for the execution of alternate groups of statements.

An end keyword, which matches the if, terminates the last group of statements. The groups of statements are delineated by the four keywords – no braces or brackets are involved.

MATLAB - Introduction course

Chapter 3

if (example):

if a < 0 a = a+1; elseif a == 0

a=0;

else

a=a;

end

MATLAB - Introduction course

Chapter 3

for

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

A matching end delineates the loop.

MATLAB - Introduction course

Chapter 3

for (example):

for i = 1:10 a(i) = i; end

MATLAB - Introduction course

Chapter 3

while

The while loop repeats a group of statements an indefinite number of times under control of a logical conditions.

A matching end delineates the loop.

MATLAB - Introduction course

Chapter 3

while (example):

while i < 0 a = i +1; end

MATLAB - Introduction course

Chapter 3

The logical operators in the tests:

 Logical operator MATLAB Symbol and & or | equal == not equal ~= Less than < Greater than > Less or equal to <= Greater or equal to >=
MATLAB - Introduction course