tutorial 1 - basic matlab help and desktopu.math.biu.ac.il/~rianis/88151/tutorial 1.pdf · log2...

29
1 Matlab כניסה ל-

Upload: others

Post on 26-Oct-2019

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

1

Matlab -כניסה ל

Page 2: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

2

Matlab -כניסה ל

Page 3: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

3

Basic Matlab

Page 4: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

4

Matlab has extensive computational capabilities and a highly evolved Graphical User Interface

Before going very deeply into these, we’ll have a look at the most basic usage of Matlab – entering simple commands at the command line prompt, which appears in the command window as the characters: >>

The command window is the sub-window that looks like this:

Some opening words…

Page 5: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

5

Basic calculations in MATLABMatlab can be used as an interactive calculator

� All commands from the command line are executed immediately � Any variables defined on the command line are stored in memory � By default – all variables are of type double

(64 bits: 53 for the mantissa, 11 for the exponent) � Angles are always in radians

Basic math:

� + - * / ^

� sin, cos, tan, log, log10, log2, exp

� Complex numbers: 1+2j, 1+2i

� Constants: pi, i, j

Precedence:

� Left to right, with ̂ before * and / , before + and –

� Parentheses may be used.

Page 6: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

6

Some examples:>> 3*4+5*6 ans= 42

>> 3*(4+5)*6 ans= 162

>> log10(4)

ans =0.6021

>> log2(4)

ans =2

>> 1+2j ans= 1.0000 + 2.0000i

>> 1+2j*2 ans= 1.0000 + 4.0000i

>> (1+2j)*2 ans= 2.0000 + 4.0000i

>> sin(pi/4) ans= 0.7071

>> exp(1) ans= 2.7183

Page 7: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

7

Output precision:

The format command controls how many digits are present on the display line:

� format short : 5 digits

� format long : 15 digits

� NOTE: this does notaffect the internal representation!

Other options of this command exist for scientific notation etc.

>> format short

>> sin(pi/4) ans= 0.7071

>> format long

>> sin(pi/4) ans= 0.70710678118655

Page 8: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

8

Variables:Variables can be any name we wish

Some variables have predefined values (next slide)

� These can be overridden, e.g: pi=2;

� Don’t do this!

The ‘=‘ sign is used to define variables:

� e.g. students=20

� Such an expression will cause the variable to be displayed

� RULE: to avoid displaying a result – use a semicolon ‘;’:

Naming rules:

� Names are case sensitive

� Up to 31 characters

� Start with a letter

� Use meaningful names!

>> erasers=4; >> pads=6; >> tape=2; >> items=erasers+pads+tapeitems =

12 >> pi pi =

3.1416 >> pi=2 pi =

2 >> clear pi >> pi pi =

3.1416

Page 9: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

9

More examples:

>> 1/2/3 ans = 0.16667

>> 1/(2/3) ans = 1.5

>> radius=2;

>> 2*pi*radius

ans =

12.566

>> sin(pi/6) ans= 0.5

>> sqrt(3^2+4^2) ans =

5 >> (3^2+4^2)^1/2 ans =

12.5 >> (3^2+4^2)^(1/2) ans =

5

Page 10: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

10

Special variables:

Reserved words: � for end if while

function return elseif case otherwise switch continue else try catch global persistent break

Special variables:

Largest integerbitmax

Largest real numberrealmax

Smallest real numberrealmin

Number of output argsnargout

Number of input args.nargin

Sqrt(-1)i, j

Not a number – 0/0NaN, nan

Infinity - 1/0inf

Smallest detectable differenceeps

Pipi

Make a beep soundbeep

Default for resultans

Variable number of input args

Variable number of output argsvarargout

varargin

DescriptionSpecial variables

Page 11: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

11

The workspace:

Where do variables go? � They’re stored in the workspace � who and whos commands give

their names and details Workspaces can be loaded and

stored as entire entities, including all the variables, in one file.

� The save command creates a *.mat file

� More about this - later Useful keys:

� Up arrow – command recall � Down arrow – scrolls forward

through commands � Tab – variable name completion � Escape – all of current

command is deleted

>> who

Your variables are:

a b c d

>> whosName Size Bytes Class

Attributes

a 1x1 8 double b 1x3 24 double c 2x2 32 double d 1x5 10 char

Page 12: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

12

Comments and punctuation:

Comments:anything after the percent (%) sign

Multiple commands: separated by a comma or semicolon

Continued lines: ellipses (…)

�Not in the middle of a name!

�Comments can’t be continued

Interrupting execution:

Ctrl-C � This is useful for stopping a program that’s taking too long to finish (e.g. in an infinite loop)

>> cost=2, items=10cost =

2items =

10>> price=cost/itemsprice =

0.2000>> price=cost... %comment/itemsprice =

0.2000>> price=cost/item... %another comments??? s

|Error: Unexpected MATLAB expression.

Page 13: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

13

Dealing with complex numbers:

MATLAB deals with complex numbers like any other number

We can interchange:i, j, sqrt(-1)

Transforming between polar and cartesian forms:

� real, imag, abs, angle

Complex conjugate (change the sign of the imaginary part):

� conj()

>> c1=1-2i; >> c2=1+2j; >> c3=1+2*sqrt(-1); >> c4=1+sin(pi/6)*1j ans =

1.0000 + 0.5000i >> abs(c1) ans =

2.2361

>> angle(c1) ans =

1.1071 >> real(c1) ans =

1 >> imag(c1) ans =

-2

Page 14: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

14

Precision issues in floating point:

calculations are normally in double-precision floating point

largest positive number: realmax

smallest positive number: realmin

smallest number that can be added to 1 to give a result larger than 1: eps (useful for numerical errors estimation)

the largest integer that has an exact representation is bitmax , which is: 253-1

>> realmaxans =

1.797693134862316e+308 >> realminans =

2.225073858507201e-308 >> epsans =

2.220446049250313e-016 >> 0.42-0.5+.08 ans =

-1.387778780781446e-017 >> 0.08-0.5+0.42 ans =

0 >> sin(0) ans =

0 >> sin(pi) ans =

1.224646799147353e-016 >> bitmaxans =

9.007199254740991e+015

Page 15: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

15

Precision problems:

>> sin(pi) ans =

1.224646799147353e-016 >> 1+sin(pi) ans =

1.00000000000000

Limited precision leads to results such as these:

The reason for this “error” is that sin(pi) is smaller than eps

� Therefore – adding it to 1 gives a number that isn’t 1, but

� Matlab cannot represent such a small difference!

Page 16: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

16

DescriptionTrig functions

Inverse hyperbolic tangentatanh

Four quadrant inverse tangentatan2

Inverse tangentatan

Inverse sineasin

Inverse hyperbolic secantasech

Inverse secantasec

Inverse hyperbolic cosecantacsch

Inverse hyperbolic cotangentacoth

Inverse cotangentacot

Inverse hyperbolic cosineacosh

Inverse cosineacos

Inverse cosecantacsc

Hyperbolic tangenttanh

Tangenttan

Hyperbolic cosingcosh

Cosinecos

Hyperbolic cosecantcsch

Cosecantcsc

cotangentcot

Hyperbolic cotangentcoth

Hyperbolic sinesinh

Sinesin

Hyperbolic secantsech

Secantsec

DescriptionTrig functions

Math functions

Page 17: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

17

Math functions

Next higher power of 2nextpow2

Square rootsqrt

Base 2 powerpow2

Base 2 logarithmlog2

Base 10 logarithmlog10

Natural logarithmlog

Exponentialexp

Power^

DescriptionExp. Functs.

Form complex from real and imaginary parts

complex

Sort vector into complex pairs

cplxpair

True for real valuesisreal

Real partreal

Imaginary partimag

Complex conjugateconj

Phase angle [radians]angle

Magnitudeabs

DescriptionComplex Functs.

Page 18: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

18

Math functions

Signumsign

Remainder after divisionrem

Modulus – signed remaindermod

Round toward neaest int.round

Round towards positive inf.ceil

Round towards negative inf.floor

Round towards zerofix

DescriptionRound and remainder

All combinations of N elements taken K at a time

nchoosek

All possible combinationsperms

Rational outputrats

Rational approximationrat

Least common multiplelcm

Greatest common divisorgcd

True for prime numbersisprime

Prime factorsfactor

DescriptionNumber theory.

Spherical to cartesiansph2cart

Cylindrical or polar to cartesianpol2cart

Cartesian to cylindrical or polarcart2pol

Cartesian to sphericalcart2sph

DescriptionCoordinate transforms

Page 19: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

19

Math functions

Error functionErf

Complete elliptic integralEllipke

Jacobi elliptic functionEllipj

Log of beta functionBetaln

Incomplete beta functionBetainc

Beta functionBeta

Modified bessel functions of the second kind

Besselk

Modified bessel functions of the first kind

besseli

Bessel functions – third kindbesselh

Bessel functions – second kindbessely

Bessel functions – first kindbesselj

Airy functionsairy

DescriptionSpecial functions

Vector dot productdot

Vector cross productcross

Associated legendre functionlegendre

Log of gamma functiongammaln

Incomplete gamma functiongammainc

Gamma functiongamma

Exponential error functionexpint

Inverse error functionerfinv

Scaled complementary error function

erfcx

DescriptionSpecial functions

Page 20: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

20

Example – roots of quadratic equation

>> a=input('Input first coeff : ')

Input first coeff: 3

a =

3

>> b=input('Input second coeff : ')

Input second coeff: -4

b =

-4

>> c=input('Input third coeff : ')

Input third coeff: 7

c =

7

>> d = b^2 - 4*a*c;

>> r = sqrt(d);

>> x1 = (-b+r) / (2*a);

>> x2 = (-b-r) / (2*a);

>> disp(x1)

0.6667 + 1.3744i

>> disp(x2)

0.6667 - 1.3744i

>> a=input('Input first coeff: ')

Input first coeff: 2

a =

2

>> b=input('Input second coeff: ')

Input second coeff: 15

b =

15

>> c=input('Input third coeff: ')

Input third coeff: -25

c =

-25

>> x1 = ( -b + sqrt(b^2 - 4*a*c) ) / (2*a)

x1 =

1.4039

>> x2 = ( -b - sqrt(b^2 - 4*a*c) ) / (2*a)

x2 =

-8.9039

Page 21: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

21

Summary

MATLAB is easy to use for calculations with real and complex numbers

Using floating point representation, accuracy is high - but in certain cases accuracy issues can crop up

MATLAB has a very extensive list of built-in math functions which are useful for a large variety of applications

Page 22: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

22

Getting help

Page 23: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

23

Help Features in MATLAB

How do we find the syntax for using the following functions? � COS, ACOS, EXP, SIN, ASIN

In order to obtain assistance in MATLAB type any of the following options at the MATLAB Command Line:

helpfunction_name

helpwinfunction_name

docfunction_name

Page 24: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

24

Using Help Features in MATLAB

Page 25: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

25

Using Help Features in MATLAB

Page 26: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

26

Contents of the MATLAB Desktop –Current directory

Page 27: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

27

Contents of the MATLAB Desktop-Workspace

Page 28: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

28

Contents of the MATLAB Desktop -Command history

Page 29: Tutorial 1 - Basic Matlab Help and Desktopu.math.biu.ac.il/~rianis/88151/Tutorial 1.pdf · log2 Base 2 logarithm log10 Base 10 logarithm log Natural logarithm exp Exponential ^ Power

29

As we have just seen, the main panels of the MatlabDesktop are:

� Current directory

� Workspace (variables)

� Command history

� The command window

Some properties:

� All of the above can be docked and undocked

� Right clicking on any of them opens a context sensitive menu

Contents of the MATLAB Desktop