What is Matlab? - Clarkson University

PROGRAMMING IN MATLAB What is Matlab? Matlab is an interactive environment that allows us to easily enter data, numerically solve problems and it provides graphing capabilities so we can visualize the results. In this document, green text shows Matlab commands and blue text shows Matlab respond. Starting Matlab To start Matlab on a Windows machine, click Start\Programs\Matlab\Matlab or click on the Matlab icon on your desktop. To start Matlab from a UNIX machine, type `matlab' at a command prompt. The Matlab command window and command line should appear similar to that shown in Figure 1

Figure 1 - The Matlab command line as viewed on a Windows machine. Need help? Getting help: ? help example: getting help for if command. ? help if Keyword search in the descriptions of the command plot: ? lookfor plot ? help elfun

Elementary math functions.

1

Trigonometric. sin - Sine. sinh - Hyperbolic sine. asin - Inverse sine. asinh - Inverse hyperbolic sine. cos - Cosine. cosh - Hyperbolic cosine. acos - Inverse cosine. acosh - Inverse hyperbolic cosine. tan - Tangent. tanh - Hyperbolic tangent. atan - Inverse tangent. atan2 - Four quadrant inverse tangent. atanh - Inverse hyperbolic tangent. sec - Secant. sech - Hyperbolic secant. asec - Inverse secant. asech - Inverse hyperbolic secant. csc - Cosecant. csch - Hyperbolic cosecant. acsc - Inverse cosecant. acsch - Inverse hyperbolic cosecant. cot - Cotangent. coth - Hyperbolic cotangent. acot - Inverse cotangent. acoth - Inverse hyperbolic cotangent.

Exponential. exp - Exponential. log - Natural logarithm. log10 - Common (base 10) logarithm. log2 - Base 2 logarithm and dissect floating point number. pow2 - Base 2 power and scale floating point number. sqrt - Square root. nextpow2 - Next higher power of 2.

Complex. abs - Absolute value. angle - Phase angle. complex - Construct complex data from real and imaginary parts. conj - Complex conjugate. imag - Complex imaginary part.

2

real - Complex real part. unwrap - Unwrap phase angle. isreal - True for real array. cplxpair - Sort numbers into complex conjugate pairs.

Rounding and remainder. fix - Round towards zero. floor - Round towards minus infinity. ceil - Round towards plus infinity. round - Round towards nearest integer. mod - Modulus (signed remainder after division). rem - Remainder after division. sign - Signum.

Getting started Here is an example in Matlab.

? 3*4

ans =

12

? 2^3 ans = 8

? d=[11 12 13 ; 21 22 23 ; 31 32 33]

d =

11 12 13

21 22 23

31 32 33

Using Matlab built-in functions

? sqrt(64)

ans =

8

? e = ones(3,3)

e =

1 1 1

1 1 1

1 1 1

3

? f = d + e

f =

12 13 14

22 23 24

32 33 34

? f' ans =

12 22 32 13 23 33 14 24 34

complex numbers:

? a=[ 1+i 2] a = 1.0000 + 1.0000i 2.0000

? b=[4-2i 5+10i]; >> the_product = a * b

??? Error using ==> * Inner matrix dimensions must agree.

? the_product = a' * b

the_product = 2.0000 - 6.0000i 15.0000 + 5.0000i 8.0000 - 4.0000i 10.0000 +20.0000i

The "colon" (:) operator:

? numbers1 = 2:2:8 numbers1 = 2 4 6 8 ? numbers2 = 2:8 numbers2 = 2 3 4 5 6 7 8

Element-by-element operations:

? elem_prod = a' .* b ??? Error using ==> .* Matrix dimensions must agree. ? elem_prod = a .* b elem_prod = 6.0000 + 2.0000i 10.0000 +20.0000i

4

Basic Operations The following matrix operations are available in MATLAB:

+ addition - subtraction * multiplication ^ Power ' transpose \ left division / right division

x = A \ b is the solution of A * x = b and, resp., x = b / A is the solution of x * A = b.

Saving and Restoring Workspace

At this point you may be bored with our little tutorial and wish to spend a few hours with your friends in the village. If you leave your computer unattended you run the risk of someone (an evil roommate perhaps) closing Matlab in which case you might loose the data you tediously entered. Fortunately we may save the Matlab workspace to disk for retrieval at a later time. First let's obtain a summary of the variables we have so far, use the `whos' command.

? whos Name Size

Bytes Class

A 3x4 B 3x3 ans 3x3 x 1x1

96 double array 72 double array 96 double array 16 double array (complex)

To save the workspace we can type

? save practicedata

which saves the data in the file `practicedata.mat' in the current directory. To verify that the file is there, we can actually execute shell commands from Matlab. On a Windows machine we can type `dir' to get a directory listing. On a UNIX machine we can type `ls' to get a directory listing. We must precede the shell command with an exclamation point (actually, some commands work without it).

5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download