1 • Programming in MATBLAB • Presented by: • Dr. Ebrahim Nemati-Kande • Urmia University • Fall 2023 2 • References Programming with MATLAB for Scientists A Beginner’s Introduction Eugeniy E. Mikhailov http://physics.wm.edu/programmin g_with_MATLAB_book. 3 4 1.2 Modern Computers • ENIAC (Electronic Numerical Integrator And Computer) 1946 • Weight: 30 tons • Cost: $500,000 ($6,000,000 adjusted for inflation) • Power consumption: 150 kW (averaged consumption of 500 households) • ENIAC could do the following in 1 s: • 5000 additions, • 357 multiplications, • 38 divisions. • Computing performance of: 100 FLOPS • Modern computer speed is measured in FLOPS • FLOPS (the number of floating-point operations per second) 5 1.2 Common features of a modern computer • CPU: Central processing Unit • GPU: Graphical processing Unit • Ram: Random-access memory • Memory: which holds data and programs, • (keyboards, hard drives, displays, printers, ...) 6 1.3 What Is Programming? Generate a list of instructions suitable for execution by a computer. • Coding • Debugging • Executing 7 1.4 Programming Languages low-level languages • assembler • C • C++, • Forth • LISP high-level languages •Fortran •Tcl •Java •JavaScript •PHP •Perl • Python • MATLAB 8 1.5 Numbers Representation in Computers and Its Potential Problems 1.5.1 Discretization—the main weakness of computers 1/6 = 0.1666666666666666 · · · • the computer has a finite memory size. • So, the computer truncates every number to a specified number of significant digits. 1/6 = 0.1667c “c” stands for computer representations. "round-off error": 1/6 = 0.1667 All of the following numbers are equal: 1/6 = 1/5.999 = 0.1667123 = 0.1667321 = 0.1667222 = 0.1667 Paradox: 20 × (1/6 ) − 20/6 = 20 × 0.1667 − 3.333 = 3.334 − 3.333 = 10−4 9 bit (binary digit) single unit of information with a value of either 0 or 1 (off or on, false or true, low or high) Byte 8 bits: 28 = 256 0 to 255 or −128 . . . 0 . . . 127 Example: Hope 4 byte 4x8 = 32 bit Other units: Kilobyte (8000 bit) Megabit (1x106 bit) Gigabit (1x106 bit) Terabit (1x106 bit) 10 processors (CPU) 16-bit CPU capable of transferring 16 bits of data at a time (decimal number up to 2^16-1 = 65535) 16-bit binary values Bit #: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Power: 215 214 213 212 211 210 29 28 27 26 25 24 23 22 21 20 Value: 326 78 162 84 819 2 409 6 204 8 102 4 512 256 128 64 32 16 8 4 2 1 11 32-bit CPU (x86) capable of transferring 32 bits of data at a time (decimal number up to 2^32-1 = 4,294,967,295) 12 64-bit CPU (x64 or x86-64) capable of transferring 64 bits of data at a time (decimal number up to 2^64-1 = 18,446,744,073,709,551,615 !!1.845E+019!!) 13 32-bit representation: 2^32= 4294967296 Int32: 32-bit (4-byte) integers: −2, 147, 483, 648 · · · 0 · · · 2, 147, 483, 647 >> intmin • 1.5.2 Binary representation ans = int32 -2147483648 MATLAB code: >> intmax ans = int32 2147483647 14 1.5.3 Floating-point number representation floating-point numbers: Numbers that have a decimal point i.e., 2.443 or 31.2 × 103 −123.765 × 1012 sm: sign bit of the mantissa (1 in our case) m: mantissa (1.23765) b: base of the exponent (10) se: the sign bit of the exponent (0 in our case) q: is the exponent (14). b=2 −1.23765 × 1014 15 >> realmax ans = MAX and MIN of the numbers in MATLAB 1.79769313486232e+308 >> realmin ans = 2.2250738585072e-308 • Institute of Electrical and Electronics Engineers (IEEE) 754 standard: mantissas take 52 bits + 1 bit for the sign equivalent to about 17 decimal digits The exponent takes 10 bits + 1 sign bit equivalent to roughly 10 ±308 >> realmax*10 overflow error ans = Inf >> realmin/1e17 underflow error ans = 0 >> 1.797693134862316 + 20 ans = 21.7976931348623 truncation error: >> 1.797693134862316 + 100 ans = 101.797693134862 16 MATLAB MATrix LABoratory capabilities • matrix manipulations • plotting of functions and data • implementation of algorithms • creation of user interfaces • interfacing with programs written in other languages Cleve Barry Moler https://en.wikipedia.org/wiki/MATLAB https://en.wikipedia.org/wiki/Cleve_Moler 17