MATLAB Primer: Numerical Computation, Visualization, and Application Development, Thesis of Data Communication Systems and Computer Networks

Matlab Learning Guide Book

Typology: Thesis

2014/2015

Uploaded on 03/19/2015

syedjz
syedjz 🇵🇰

1 document

1 / 198

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MATLAB®
Primer
R2014b
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download MATLAB Primer: Numerical Computation, Visualization, and Application Development and more Thesis Data Communication Systems and Computer Networks in PDF only on Docsity!

MATLAB

Primer

R 2014 b

How to Contact MathWorks

Latest news: www.mathworks.com

Sales and services: www.mathworks.com/sales_and_services

User community: www.mathworks.com/matlabcentral

Technical support: www.mathworks.com/support/contact_us

Phone: 508-647-
The MathWorks, Inc.
3 Apple Hill Drive
Natick, MA 01760-

MATLAB®^ Primer

© COPYRIGHT 1984–2014 by The MathWorks, Inc.

The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc. FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through the federal government of the United States. By accepting delivery of the Program or Documentation, the government hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and Documentation by the federal government (or other entity acquiring for or through the federal government) and shall supersede any conflicting contractual terms or conditions. If this License fails to meet the government's needs or is inconsistent in any respect with federal procurement law, the government agrees to return the Program and Documentation, unused, to The MathWorks, Inc.

Trademarks

MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders.

Patents

MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for more information.

v

Contents

Quick Start

vii

Mathematics

Graphics

Programming

x

  • MATLAB Product Description 1-
    • Key Features 1-
  • Desktop Basics 1-
  • Matrices and Arrays 1-
    • Array Creation 1-
    • Matrix and Array Operations 1-
    • Concatenation 1-
    • Complex Numbers 1-
  • Array Indexing 1-
  • Workspace Variables 1-
  • Character Strings 1-
  • Calling Functions 1-
  • 2-D and 3-D Plots 1-
    • Line Plots 1-
    • 3-D Plots 1-
    • Subplots 1-
  • Programming and Scripts 1-
    • Sample Script 1-
    • Loops and Conditional Statements 1-
    • Script Locations 1-
  • Help and Documentation 1-
  • Matrices and Magic Squares 2-
    • About Matrices 2-
    • Entering Matrices 2-
    • sum, transpose, and diag 2-
    • The magic Function 2-
    • Generating Matrices 2-
  • Expressions 2-
    • Variables 2-
    • Numbers 2-
    • Matrix Operators 2-
    • Array Operators 2-
    • Functions 2-
    • Examples of Expressions 2-
  • Entering Commands 2-
    • The format Function 2-
    • Suppressing Output 2-
    • Entering Long Statements 2-
    • Command Line Editing 2-
  • Indexing 2-
    • Subscripts 2-
    • The Colon Operator 2-
    • Concatenation 2-
    • Deleting Rows and Columns 2-
    • Scalar Expansion 2-
    • Logical Subscripting 2-
    • The find Function 2-
  • Types of Arrays 2-
    • Multidimensional Arrays 2-
    • Cell Arrays 2-
    • Characters and Text 2-
    • Structures 2-
  • Linear Algebra 3-
    • Matrices in the MATLAB Environment 3-
    • Systems of Linear Equations 3-
    • Inverses and Determinants 3-
    • Factorizations 3-
    • Powers and Exponentials 3-
    • Eigenvalues 3-
    • Singular Values 3-
  • Operations on Nonlinear Functions 3-
    • Function Handles 3-
    • Function Functions 3-
  • Multivariate Data 3-
  • Data Analysis 3-
    • Introduction 3-
    • Preprocessing Data 3-
    • Summarizing Data 3-
    • Visualizing Data 3-
    • Modeling Data 3-
  • Basic Plotting Functions 4-
    • Creating a Plot 4-
    • Plotting Multiple Data Sets in One Graph 4-
    • Specifying Line Styles and Colors 4-
    • Plotting Lines and Markers 4-
    • Graphing Imaginary and Complex Data 4-
    • Adding Plots to an Existing Graph 4-
    • Figure Windows 4-
    • Displaying Multiple Plots in One Figure 4-
    • Controlling the Axes 4-
    • Adding Axis Labels and Titles 4-
    • Saving Figures 4- viii Contents
    • Saving Workspace Data 4-
  • Creating Mesh and Surface Plots 4-
    • About Mesh and Surface Plots 4-
    • Visualizing Functions of Two Variables 4-
  • Display Images 4-
    • Image Data 4-
    • Reading and Writing Images 4-
  • Printing Graphics 4-
    • Overview of Printing 4-
    • Printing from the File Menu 4-
    • Exporting the Figure to a Graphics File 4-
    • Using the Print Command 4-
  • Working with Graphics Objects 4-
    • Graphics Objects 4-
    • Setting Object Properties 4-
    • Functions for Working with Objects 4-
    • Passing Arguments 4-
    • Finding the Handles of Existing Objects 4-
  • Control Flow 5-
    • Conditional Control — if, else, switch 5-
    • Loop Control — for, while, continue, break 5-
    • Program Termination — return 5-
    • Vectorization 5-
    • Preallocation 5-
  • Scripts and Functions 5-
    • Overview 5-
    • Scripts 5-
    • Functions 5-
    • Types of Functions 5-
    • Global Variables 5-
  • • “MATLAB Product Description” on page 1- Quick Start
  • • “Desktop Basics” on page 1-
  • • “Matrices and Arrays” on page 1-
  • • “Array Indexing” on page 1-
  • • “Workspace Variables” on page 1-
  • • “Character Strings” on page 1-
  • • “Calling Functions” on page 1-
  • • “2-D and 3-D Plots” on page 1-
  • • “Programming and Scripts” on page 1-
  • • “Help and Documentation” on page 1-

Desktop Basics

Desktop Basics

When you start MATLAB, the desktop appears in its default layout.

The desktop includes these panels:

  • Current Folder — Access your files.
  • Command Window — Enter commands at the command line, indicated by the prompt (>>).
  • Workspace — Explore data that you create or import from files.

As you work in MATLAB, you issue commands that create variables and call functions. For example, create a variable named a by typing this statement at the command line:

a = 1

1 Quick Start

MATLAB adds variable a to the workspace and displays the result in the Command Window.

a =

1

Create a few more variables.

b = 2

b =

2

c = a + b

c =

3

d = cos(a)

d =

When you do not specify an output variable, MATLAB uses the variable ans, short for answer , to store the results of your calculation.

sin(a)

ans =

If you end a statement with a semicolon, MATLAB performs the computation, but suppresses the display of output in the Command Window.

e = a*b;

You can recall previous commands by pressing the up- and down-arrow keys, ↑ and ↓. Press the arrow keys either at an empty command line or after you type the first few characters of a command. For example, to recall the command b = 2 , type b, and then press the up-arrow key.

1 Quick Start

z = zeros(5,1)

z =

0 0 0 0 0

Matrix and Array Operations

MATLAB allows you to process all of the values in a matrix using a single arithmetic operator or function.

a + 10

ans =

11 12 13 14 15 16 17 18 20

sin(a)

ans =

0.8415 0.9093 0. -0.7568 -0.9589 -0. 0.6570 0.9894 -0.

To transpose a matrix, use a single quote ('):

a'

ans =

1 4 7 2 5 8 3 6 10

You can perform standard matrix multiplication, which computes the inner products between rows and columns, using the * operator. For example, confirm that a matrix times its inverse returns the identity matrix:

Matrices and Arrays 1-

p = a*inv(a)

p =

1.0000 0 -0. 0 1.0000 0 0 0 1.

Notice that p is not a matrix of integer values. MATLAB stores numbers as floating-point values, and arithmetic operations are sensitive to small differences between the actual value and its floating-point representation. You can display more decimal digits using the format command:

format long p = a*inv(a)

p =

1.000000000000000 0 -0. 0 1.000000000000000 0 0 0 0.

Reset the display to the shorter format using

format short

format affects only the display of numbers, not the way MATLAB computes or saves them.

To perform element-wise multiplication rather than matrix multiplication, use the .* operator:

p = a.*a

p =

1 4 9 16 25 36 49 64 100

The matrix operators for multiplication, division, and power each have a corresponding array operator that operates element-wise. For example, raise each element of a to the third power:

a.^

Matrices and Arrays

0.0000 + 1.0000i

To represent the imaginary part of complex numbers, use either i or j.

c = [3+4i, 4+3j; -i, 10j]

c =

3.0000 + 4.0000i 4.0000 + 3.0000i 0.0000 - 1.0000i 0.0000 +10.0000i

1 Quick Start

Array Indexing

Every variable in MATLAB is an array that can hold many numbers. When you want to access selected elements of an array, use indexing.

For example, consider the 4-by-4 magic square A:

A = magic(4)

A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1

There are two ways to refer to a particular element in an array. The most common way is to specify row and column subscripts, such as

A(4,2)

ans = 14

Less common, but sometimes useful, is to use a single subscript that traverses down each column in order:

A(8)

ans = 14

Using a single subscript to refer to a particular element in an array is called linear

Indexing 2-

If you try to refer to elements outside an array on the right side of an assignment statement, MATLAB throws an error.

test = A(4,5)

Attempted to access A(4,5); index out of bounds because size(A)=[4,4].

However, on the left side of an assignment statement, you can specify elements outside the current dimensions. The size of the array increases to accommodate the newcomers.

A(4,5) = 17