Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
אדמיניסטרציה • אתר הקורסhttp://math-wiki.com : – סילבוס ,צוות הקורס ,שעות קבלה ,אמצעי קשר ,חומר הקורס ,שיעורי הבית... • אתר קבוצת התרגול: http://u.math.biu.ac.il/~osharog/88151/ – חומר של תרגולים של הקבוצה ,חומרים נוספים. 1 Basic Matlab 2 Basic calculations in MATLAB Matlab 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. 3 Some examples: >> 3*4+5*6 ans= 42 >> 3*(4+5)*6 ans= 162 >> log10(4) ans = 0.6021 >> log2(4) >> 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 ans = 2 4 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 not affect 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 5 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 >> erasers=4; >> pads=6; >> tape=2; >> items=erasers+pads+tape items = 12 >> pi pi = 3.1416 >> pi=2 pi = 2 >> clear pi >> pi pi = 3.1416 6 More examples: >> 1/2/3 >> sqrt(3^2+4^2) ans = 0.16667 ans = 5 >> (3^2+4^2)^1/2 ans = 12.5 >> (3^2+4^2)^(1/2) ans = 5 >> 1/(2/3) ans = 1.5 >> radius=2; >> 2*pi*radius ans = 12.566 >> sin(pi/6) ans= 0.5 7 Special variables: Reserved words: for end if while function return elseif case otherwise switch continue else try catch global persistent break Special variables: Special variables ans Description Default for result beep Make a beep sound pi Pi eps Smallest detectable difference inf Infinity - 1/0 NaN, nan Not a number – 0/0 i, j Sqrt(-1) nargin Number of input args. nargout Number of output args realmin Smallest real number realmax Largest real number bitmax Largest integer varargin Variable number of input args varargout Variable number of output args 8 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 >> whos Name a b c d Size 1x1 1x3 2x2 1x5 Bytes Class 8 24 32 10 Attributes double double double char 9 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=10 cost = 2 items = 10 >> price=cost/items price = 0.2000 >> price=cost... %comment /items price = 0.2000 >> price=cost/item... %another comment s ??? s | Error: Unexpected MATLAB expression. 10 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 11 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 >> realmax ans = 1.797693134862316e+308 >> realmin ans = 2.225073858507201e-308 >> eps ans = 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 >> bitmax ans = 9.007199254740991e+015 12 Precision problems: Limited precision leads to results such as these: >> sin(pi) ans = 1.224646799147353e-016 >> 1+sin(pi) ans = 1.00000000000000 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! 13 Math functions Exp. Functs. ^ Description exp Exponential log Natural logarithm log10 Base 10 logarithm log2 Base 2 logarithm pow2 Base 2 power sqrt Square root Power Complex Functs. abs Description Magnitude angle Phase angle [radians] conj Complex conjugate imag Imaginary part real Real part isreal True for real values 14 Math functions Trig functions Description acos Inverse cosine coth Hyperbolic cotangent acosh Inverse hyperbolic cosine csc Cosecant acot Inverse cotangent csch Hyperbolic cosecant acoth Inverse hyperbolic cotangent sec Secant acsc Inverse cosecant sech Hyperbolic secant acsch Inverse hyperbolic cosecant sin Sine asec Inverse secant sinh Hyperbolic sine asech Inverse hyperbolic secant tan Tangent asin Inverse sine tanh Hyperbolic tangent atan Inverse tangent atan2 Four quadrant inverse tangent atanh Inverse hyperbolic tangent cos Cosine cosh Hyperbolic cosing cot cotangent Trig functions Description 15 Math functions Round and remainder Description Number theory. Description fix Round towards zero factor Prime factors floor Round towards negative inf. isprime True for prime numbers ceil Round towards positive inf. gcd Greatest common divisor round Round toward neaest int. lcm Least common multiple mod Modulus – signed remainder rat Rational approximation rem Remainder after division rats Rational output sign Signum perms All possible combinations nchoosek All combinations of N elements taken K at a time Coordinate transforms Description cart2sph Cartesian to spherical cart2pol Cartesian to cylindrical or polar pol2cart Cylindrical or polar to cartesian sph2cart Spherical to cartesian 16 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 17 Getting help 18 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: help function_name helpwin function_name doc function_name 19 Using Help Features in MATLAB 20 Using Help Features in MATLAB 21 Contents of the MATLAB Desktop As we have just seen, the main panels of the Matlab Desktop 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 22 Contents of the MATLAB Desktop – Current directory 23 Contents of the MATLAB DesktopWorkspace 24 Contents of the MATLAB Desktop Command history 25