al imam mohammad bin saud islamic university college of sciences department of mathematics

22
AL IMAM MOHAMMAD BIN SAUD ISLAMIC UNIVERSITY COLLEGE OF SCIENCES DEPARTMENT OF MATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB

Upload: phuoc

Post on 09-Feb-2016

27 views

Category:

Documents


0 download

DESCRIPTION

Al Imam Mohammad Bin Saud Islamic University College of Sciences Department of Mathematics. MATLAB 251 : MATH SOFTWARE Introduction to MATLAB. mrs. Asra Sultana. Introduction to MATLAB. 4. Graphics in MATLAB. Introduction to Matlab. 4.1 Main functions. Introduction to Matlab. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

AL IMAM MOHAMMAD BIN SAUD ISLAMIC UNIVERSITY

COLLEGE OF SCIENCESDEPARTMENT OF MATHEMATICS

MATLAB 251 : MATH SOFTWARE

Introduction to MATLAB

Page 2: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

4 .Graphics in MATLAB

Page 3: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

4.1 Main functions

Create a new figurefigureCreate Many axissubplotDisplay a gridgirddefine the limits of axisaxiskeep the plotsholdclose figuresclose

Page 4: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

To put mathematics in labels you can use matlab’s backslashnotation (familiar to users of the TEX typesetting system)

Insert a title in the graphictitlexlabelylabelzlabel

Insert commenttext,gtextDisplay alegendlegend

Label the axis

Page 5: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

4.2 2D Graphics

Plot symbols and colors may be obtained when we use one of the 4 first previous commands, by adding a string S in those commands.

PLOT(X,Y,S) or PLOT(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,…)

plotSketch curves or clouds of pointssemilogxsemilogy

Plot semi-logarithmic curves

loglogPlot semi-logarithmic curvespolarPlot graphs in polar coordinatesplotyyPlot two curves in the same graph with their own axis

systemezplotPlot functionsezpolarPlot functions in polar coordinateszoomGraphic zoom

Page 6: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

>>x= linspace(1,10,20);

>>y=sin(x);z=cos(x); >>plot(x,y,'-.',x,z,'r')

>>t = 0:.1:2*pi; >>subplot(2,2,1)

>>plot(cos(t),sin(t)) >>subplot(2,2,2)

>>plot(cos(t),sin(2*t))

>>subplot(2,2,3) >>plot(cos(t),sin(3*t))

>>subplot(2,2,4) >>plot(cos(t),sin(4*t))

Page 7: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

4.3 Graphic symbols

ColorSymbolyellowy

magentamcyancredr

greengbluebwhitewblackk

MarkerSymbolplus +circleostar*point.X-markXsquaresdimonddTriangle up^Triangle downvTriangle right>Triangle left>pentagramphexagramhno markernone

StyleSymbolsolid -

dotted:dashpot - .dashed - - no linenone

Page 8: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

4.4 Clearing the Figure Window

You can clear the plot window by typing clf, which stands for ‘clear figure’. To get rid of a figure window entirely, type close. To get rid of all the figure windows, type close all. New figure windows can be created by typing figure.

4.5 Axes

So far we have allowed matlab to choose the axes for our plots. You can change the axes in many ways:

axis([xmin xmax ymin ymax ]) sets the axes’ minimum and maximum values.

•axis square makes the axes the same length •axis equal makes the axes the same scale

•axis tight sets the axes limits to the range of the data •axis auto allows matlab to choose axes limits

•axis off removes the axes leaving only the plotted data •axis on puts the axes back again

Page 9: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB 4.6 3D graphics

1 .Elementary commands (using matrix)

3 .Tools commands

2 .Elementary commands (math functions)

plot3Sketch linear currves and clouds of pointsmeshSketch a matrix under a meshed curvesurfSketch a matrix under a surfaced curve

ezplot3Sketch 2D functions and parametric functionsezmeshSketch 2D functions and parametric functions under a meshed

curveezsurfSketch 2D functions and parametric functions under a surfaced

curve

hiddenTransparent meshingshadingColor smoothinglightLightingcolorbarAdd a bar of colorcolormapTable of color

Page 10: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

Examples:

>>t=0:.1:2*pi; >>plot3(cos(3*t),sin(3*t),t)

-1-0.5

00.5

1

-1

-0.5

0

0.510

2

4

6

8

[ >>X,Y=]meshgrid(-10:10); >>Z=sqrt(X.^2+Y.^2);

>>mesh(X,Y,Z)

-10-5

05

10

-10

-5

0

5

100

5

10

15

Page 11: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

An interesting function: peaks function12 2 3 5-x -(1+y) -10 x-x -y 2 2 2 215 -x -y -(x+1) -y2z = 3(1-x) e e - e

3

[ >>X,Y,Z=]peaks; >>mesh(X,Y,Z)

-4-2

02

4

-4

-2

0

2

4-10

-5

0

5

10

[ >>X,Y,Z=]peaks; >>surf(X,Y,Z)

-4-2

02

4

-4

-2

0

2

4-10

-5

0

5

10

Page 12: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

>>shading interp;

>>axis tight;

>>colorbar;

-4-2

02

4

-4

-2

0

2

4-10

-5

0

5

10

Page 13: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

>>[X,Y,Z=]peaks;

>>contour(X,Y,Z)

>>[X,Y,Z=]peaks; >>plot3(X,Y,Z)

>>[X,Y,Z=]peaks; >>surfc(X,Y,Z);>>shading flat;

Page 14: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

Suppose we have the set of(X,Y) coordinates

The (x,y) can be split into two matrices

>>x=[1:4];y=[1:4];[>>X,Y=]meshgrid(x,y)

This is meshgrid principle

1,1 1, 2 1,3 1,42,1 2,2 2,3 2, 4

( , )3,1 3, 2 3,3 3, 44,1 4,2 4,3 4, 4

X Y

1 2 3 4 1 1 1 11 2 3 4 2 2 2 2

1 2 3 4 3 3 3 31 2 3 4 4 4 4 4

X Y

Page 15: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

ColorBlueGreen RedBlack000White111Red001

Green010Blue100

Yellow011Magenta101

Cyan110Gray0.50.50.5

Dark red000.5

You can create your own colormaps or use any matlab predefined colormaps

4.7 Color Maps Matlab uses a matrix called colormap to color surfaces or images. The colormap is a list of triplets corresponding To the intensities between 0 and 1 of the red, green and blue

Page 16: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

hsv hot gray bone copper pink white flag lines colorcube jetprism cool autumn spring winter summer

Carry out the following commands and observe

>>peaks;>>colormap(gray(8))

>>m = gray(8);>>imagesc(colormap(m))

Page 17: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

Vertical barsbarHorizontal barsbarh3D Vertical barsbar33D Horizontal barsbar3hDisplay surfacesarea

Display a piepieDisplay a 3D piepie3

Display an histogram of a certain distributionhist

Display a sequence of discrete valuestem3D displaystem3Display stairs, echelonsstaris

Bars and Surfaces

Pies

Histograms

Discrete data

4.8 Specialized graphs

Page 18: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

Display vectors centered at 0compassDisplay equally spaced vectors alond an axis featherDisplay vectors in a planequiverDisplay vectors in a spacequiver3

Display level lines labels clabelDisplay level lines in a planecontourDisplay 3D level linescontour3Display level lines by filling the spacescontourf

Vectors

Level lines

>> t = [-2:2]’;>> y = [5 2 1; 8 7 3; 9 8 7; 5 5 5;4 3 2];

Page 19: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

>>bar(t,y)

>>bar(t,y,’stack’)

Page 20: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

>>bar3(t,y)

>>area(t,y)

Page 21: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

>>pie([31 15 8])

>>hist(randn(1000,3))

Page 22: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB