300 likes | 491 Views
Introduction to Matlab. Entering Commands. Constants and Functions. >> pi ans = 3.1416 >> eps ans = 2.2204e-016 >> sin(pi/2) ans = 1 >> log(1000) ans = 6.9078 >> log10(1000) ans = 3. Some Constants. bitmax: Largest usable positive integer
E N D
Constants and Functions >> pi ans = 3.1416 >> eps ans = 2.2204e-016 >> sin(pi/2) ans = 1 >> log(1000) ans = 6.9078 >> log10(1000) ans = 3
Some Constants • bitmax: Largest usable positive integer • eps: Smallest number that changes the value of 1 when added to it. • realmin:Smallest usable positive real number • realmax: Largest usable positive real number • ans:Default variable names used for results • i or j: sqrt(-1) • inf : Stands for infinity, e.g., 1/0 • NaN or nan :Stands for not a number, e.g., 0/0.
Constants … >> bitmax ans = 9.0072e+015 >> eps ans = 2.2204e-016 >> realmin ans = 2.2251e-308 >> realmax ans = 1.7977e+308 >> ans ans = 1.7977e+308 >> i ans = 0 + 1.0000i >> inf ans = Inf >> NaN ans = NaN
Elementary Math Functions 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. >> help elfun Elementary math functions. 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.
Elementary Math Functions Exponential. exp - Exponential (e^x). 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. realpow - Power that will error out on complex result. reallog - Natural logarithm of real number. realsqrt - Square root of number greater than or equal to zero. sqrt - Square root. nextpow2 - Next higher power of 2.
Exponential Function Examples >> exp(log(3)) ans = 3.0000 >> log(exp(3)) ans = 3 >> log2(32) ans = 5 >> log2(pow2(5)) ans = 5 >> nextpow2(27) ans = 5
Elementary Math Functions Complex. abs - Absolute value. angle - Phase angle. complex - Construct complex data from real and imaginary parts. conj - Complex conjugate. imag - Complex imaginary part. real - Complex real part. unwrap - Unwrap phase angle. isreal - True for real array. cplxpair - Sort numbers into complex conjugate pairs.
Elementary Math Functions 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.
Remainder Examples >> mod( 12,7) ans = 5 >> mod(-12, 7) ans = 2 >> rem(12,7) ans = 5 >> rem(-12, 7) ans = -5
Rounding Examples round(2.7) 3, round(2.3) 2 round(-2.7) -3, round(-2.3) -2 fix(2.3) 2, fix(2.7) 2 fix(-2.3) -2, fix(-2.7) -2 ceil(2.3) 3, ceil(2.7) 3 ceil(-2.3) -2, ceil(-2.7) -2 floor(2.3) 2, floor(2.7) 2 floor(-2.3) -3, floor(-2.7) -3
Getting Help >> cotangent(pi/2) ??? Undefined function or variable 'cotangent'. >> help cotangent cotangent.m not found. >> lookfor cotangent ACOT Inverse cotangent. ACOTH Inverse hyperbolic cotangent. COT Cotangent. COTH Hyperbolic cotangent. ACOT Symbolic inverse cotangent. ACOTH Symbolic inverse hyperbolic cotangent. COT Symbolic cotangent. COTH Symbolic hyperbolic cotangent. >> help cot COT Cotangent. COT(X) is the cotangent of the elements of X.
Variables Let’s evaluate the following expression in matlab : Now let’s do the following: What if we need to evaluate the same expression for 2.1, 2.2, 2.3, … and lots of other values?
Variables Lets evaluate: It looks like the term appears 3 times in our expression. It would be nice if we evaluated it once and “remembered” the result for other occurrences of the term in the formula.
Variables • Variables are named memory locations that can be assigned a value by the user or programmer • The system can retrieve , or “remember” the value of a variable. • Variables typically reside in main memory.
Variable Examples >> a ??? Undefined function or variable 'a'. >> a=2 a = 2 >> a+3 ans = 5 >> sqrt(a+14) ans = 4 >> a=a+12 a = 14
Variables • Given: Rather than : >>cot(3)*sqrt(log(3)^3) + cos(3)*sin(log(3)), >>cot(2.7)*sqrt(log(2.7)^3) + cos(2.7)*sin(log(2.7)), … Use a variable: >>x=3 >>cot(x)*sqrt(log(x)^3) + cos(x)*sin(log(x)) >>x=2.7 >>cot(x)*sqrt(log(x)^3) + cos(x)*sin(log(x))
Variables Rather than evaluating : >> x=0.5 x = 0.5000 >> term=sin(x)+cos(x)^2 term = 1.2496 >> log(term) + realpow(term, 0.25) - term^2 ans = -0.2814
Who and Clear >> who Your variables are: a ans term x >> whos Name Size Bytes Class a 1x1 8 double array ans 1x1 8 double array term 1x1 8 double array x 1x1 8 double array Grand total is 4 elements using 32 bytes >> clear a >> who Your variables are: ans term x >> clear >> who >>
Variable Names • Only first 31 characters significant • Matlab variables case sensitive • Use meaningful names rather than shorter ones ! • Avoid using existing function names !
Variable Name Examples >> Abc=123 Abc = 123 >> ABc ??? Undefined function or variable 'ABc'. >> log(5) ans = 1.6094 >> log = 4 log = 4 >> log(5) ??? Index exceeds matrix dimensions. >> clear log >> log(5) ans = 1.6094
Getting User Input >> age=input('Please enter your age : ') Please enter your age : 29 age = 29 >> age + 2 ans = 31
String variables >> a = 'This is a string' a =This is a string >> b= 'Another string' b =Another string >> a + b ??? Error using ==> + Array dimensions must match for binary array op. >> whos Name Size Bytes Class a 1x16 32 char array b 1x14 28 char array Grand total is 30 elements using 60 bytes >> c = [a b 'Yet another string'] c =This is a stringAnother stringYet another string
String variables • Variables can change types based on the values assigned to them : >> a a =This is a string >> a=3; >> whos Name Size Bytes Class a 1x1 8 double array b 1x14 28 char array c 1x48 96 char array Grand total is 63 elements using 132 bytes
Converting Numbers to Strings • You can use the function num2str and int2str: >> s = 'The number ' + 3 s = 87 107 104 35 113 120 112 101 104 117 35 >> s = ['The number ' 3] s =The number >> s = ['The number ' num2str(3)] s =The number 3 >> int2str(2.3) ans =2
Converting Strings to Numbers >> x = ['123' '789'] x =123789 >> x2 = str2num(x) x2 = 123789 >> x2 + 1 ans = 123790 >> str2int('1.2') ??? Undefined function or variable 'str2int'.
Display Format • The values displayed at the screen doesn’t necessarily include all the information. • It is possible to change the display format. >> 12345678901234567890.12345678901234567890 ans = 1.2346e+019 >> format long >> 12345678901234567890.12345678901234567890 ans = 1.234567890123457e+019
Operator Precedence 1 The contents of all bracketed expressions are evaluated, starting from the innermost brackets and working out. 2 All exponentials are evaluated, working from left to right. 3 All multiplications and divisions are evaluated, working from left to right. 4 All additions and subtractions are evaluated, working from left to right.
Operator Precedence >> 2 + 3 * 4 ans = 14 >> 2 * 3 ^ 2 ans = 18 >> (2 + 3) * 4 ans = 20 >> (2^(2 + 3)) * 4 ans = 128