Programming in MATLAB

Programming in MATLAB Marc E. Herniter Northern Arizona University TU Darmstadt FB Maschinenbau 60005141 BROOKS/COLE THOMSON LEARNING Australia •...
Author: Rolf Joseph
25 downloads 1 Views 248KB Size
Programming in MATLAB

Marc E. Herniter Northern Arizona University

TU Darmstadt FB Maschinenbau

60005141

BROOKS/COLE THOMSON LEARNING

Australia • Canada • Mexico • Singapore • Spain • United Kingdom • United States

Index

Symbols Jt, 9,12 %c, 308 %d, 308 %f, 77,109,117 %g, 33, 67, 77, 117 %B, 221, 308 &, 68, 70 . *, 255 . / , 256 .\255

.csv, 322 .doc file, 50 .m extension, 51 1,68 -,68 16,384 elements, 352

ABS, 15,17, 37,45, 218-220, 222, 224, 226 adaptive-recursive, 451 add new directory to path, 55 addition, 256 address, 203 address book, 263, 304, 315 addressing arrays, 208-210 AND, 68, 70 angle, 16 ANGLE, 16,17

ans, 12 array, 199, 207, 257 address, 203 addressing, 211,216 declare size, 267 dynamic, 362 element-by-element, 257 element-by-element operations, 255

growing, 263 index, 203 indexing, 217 individual element, 216 of inputs, 255 multidimensional, 210, 216 multiplication, 255 one-column, 202 one-dimensional, 201-202 one-row, 202 size limitation, 204 speed, 269 structure, 280 text strings, 206, 207 two-dimensional, 206-207 ascending order, 236, 244 ASCII text file, 310 a s k _ q , 167 ask_ques function, 276 average, 69-72 axes color, 423 range, 376 AXIS, 376, 392 axis command, 376 axis range, 376

B bank, 9 binary data, 312 binary data file, 312 binary file, 310 reading, 316 boundary conditions, 459 boundary-value problem first-order, 460 linear, 469 nonlinear, 461 second-order, 469

BREAK, 98

break up program, 157

c C, 76 calculator, 1 capacitor, 113 capital letters, 220 carriage return, 70 case sensitive, 6 cd, 58 CEIL, 65, 66 cell address, 208, 276 cell array, 209 cell arrays, 270, 275 , text strings, 275 cell indexing, 271 CELLPLOT, 271, 274 change the MATLAB search path, 54 char, 310 character strings, 24 CLEAR, 7 CLP, 413 CLOCK, 192, 335 CLOSE, 413

closing files, 293-294 colon, 209, 216, 217 color of plot, 371-373 column number, 216 column vector, 19 COMET, 404

comma, 308 comma-separated files, 317, 321-322 values, 322 command center, 84-86,142 commas, 18 comment lines, 159 complex numbers, 14,15 481

482

Index

complex roots, 72 computer time, 192 concatenating strings, 26 concatenation, 26,174,205,263, 272 condition, 127 content addressing, 271 CONV, 432 convert integer to text string, 110 cosine function, 362 counter, 116 CPUTIME, 333 crosshair, 383,385 CSV, 322 CSVREAD, 324 CSVWRITE, 321 cubic Spline, 442 curly brackets, 271,277 current-voltage characteristic, 120 curve fit, 434-435

D:\MATLAB\toolbox\local, 52 data, curve fit, 434 date, 333,341 format, 343 formatting codes, 343 serial date number, 343 DATE, 341 DATENUM, 343 DATESTR, 342, 344 December 31,1 BCE, 341 default plot properties, 423 degenerate, 72 degrees, 16,17 delimiter, 318 demos, 48 derivatives, 461 descending order, 238-239, 244 DET, 22 determinant, 22 differential equations, 459 nth-order, 463 numerical solution to, 461 DIR, 294 discriminant, 92 DISP, 76,78 display format, 8 display results, 31 doc file, 50 double, 310

double precision, 110,112 double quote, 25 draw line, 386 dynamic arrays, 262 speed, 269

editing script files, 50 elapsed time, 334, 336, 339 element by element, 37 element-by-element operations, 255 ELSE, 79-80 ELSEIF, 84, 88 end of a specified file, 308 end of file, 308 end of line, 309 eps, 12 equal to, 64 equations, 381 roots of, 449 solving, 348,449 equivalent resistance, 187 error, 54 error checking, 160 error code, 160, 162 error message, 134,162 estimate output, 441,443 ETIME, 192, 336 EVAL, 346 evaluate function, 348 a text string, 346 EXCEL, 318 exchange data with other programs, 293 exchanging data, 317-318 exclusive OR, 68 experiment trials, 439 exponential, 16-17 export data, 293 extension, 297 extension ".m", 51

fact function, 162 factorial, 108, 111,160 factorial function, 158, 160-161, 188 false, 64 f c l o s e , 221

PCLOSE, 294, 296 PEOP, 308 PEVAL, 348 /W.293 fid code, 294 fields, 280 FIGURE, 408 figure window, 408 CLP, 413 CLOSE, 413 make active, 410 SHG, 413 file comma separated, 317-318 reading, 307-308 structured, 307 writing, 303 file and path names, 294, 297 file identifier, 303 float, 310 floating-point operations per second, 344 FLOOR, 106-107 floppy drive, 58 PLOPS, 344 font change color, 425 change properties, 422 FOPEN, 221, 293, 294, 298, 303, 310 FOR, 123,163 nested, 115 special cases, 123 specific values, 123 sweep characters, 123 POR loops, 93 nested, 115 format character, 33 text strings, 354-355 types, 8 FORTRAN, 76 FPRINTP, 32,70, 76-77,112, 117,276,303,306 SPRINTF function, 354 FREAD, 312 frequency data, 251 FSCANF, 221, 308 function, 157 array inputs, 255 comments, 160

Index execution time, 334 LOOKFOR, 159,161 nargin and nargout, 185 passing parameters, 181 recursive, 188 return statement, 184 speed, 193 using, 161 FUNCTION, 158 function evaluate, 348 function that calls itself, 188 FWRITE, 310 FZERO, 348, 350,449

G GCA, 417,422 GINPUT, 383 GLOBAL, 182-184, 340 global variables, 181-184 grade calculation, 69, 81-83 greater than, 64 greater than or equal to, 64 Greek letters, 381 GRID, 375 GTEXT, 380,381 guessing game, 129-130 guessing program, 64,95-96

H handle, 408,413 axis, 417 figure, 413 figure window, 405 modifying objects, 417 PLOT, 414 text labels, 416 help facility, 39 function, 160 window, 45 higher precedence, 2 HIST, 221,250-251 histogram, 218, 221, 230, 234 HOLD, 364

i, 12 identity matrix, 22 IF, 63-64,67 IP-ELSE-END, 80-83, 85 IF-ELSEIF-ELSE-END, 87, 145

IMAG, 15 imaginary numbers, 14 indentation, 86 index, 25-26, 203,212, 215-216 indices, 217 inf, 12 infinite loop, 127 infinity, 13 initial conditions, 459 initial-value problems, 459 first-order, 645 INPUT, 31, 35,73-74,92,169 input arguments, 167, 186, 277 INT2STR, 110-111 integer, 106, 107 integration, 451 recursion limit, 456 singularity, 455 internal resistance, 124 INV, 21,23 invalid operation, 13 response, 134,166,276 string, 144 inverse, 21 inverse of matrix, 334 i s empty, 145 ISREAL, 74 l-V characteristic, 120

l'Hopital's rule, 262 largest number, 12 leap year, 343 LEGEND, 366 LENGTH, 27, 102, 213 less than, 64 less than or equal to, 64 l i n e , 386 drawing, 386 line color, 372 linear boundary-value problems, 469 LINSPACE, 37,114,198, 550 LOAD, 11 local directory, 52 logarithmic spiral, 404

483

logical not, 68, 96 logical operators, 68, 70 LOGLOG, 400,407 LOGSPACE, 393-394 'long', 310, 315 LOOKFOR, 48,106,159,161 LOWER, 28, 223,224 lower integer, 106-107 lower precedence, 2 lowercase, 220, 224

M magnitude, 15 markers, 372 math operators, 2 mathematical sysbols, 381 MATLAB

student version, 352 MATLAB path, 52

add directory, 55 changing, 54 MATLAB search path, 67 addition and subtraction, 20 inverse, 21, 334 multiplication, 19, 255 operation, 255-256 MAX, 126 maximum power transfer, 124 MEAN, 71 message box, 385 WAITFOR command, 385 M-File Editor, 50 m-files, 51 Microsoft Word, 50 MOSFET, 120 MSGBOX, 385 WAITFOR, 385 multiple commands on single line, 32 multiplication table, 116

N \ n , 32-33, 35, 90,168 names, 3 NaN, 12,259 nargin, 47 nargin and nargout, 185 negative step, 95 nested FOR loop, 115 new line character, 33,35,70,168 next lowest integer, 106

484

Index

nonlinear boundary-value problem, 472 not, 68 not a number, 13 not equal to, 64 NOW, 344 null string, 145-146 number of elements, 102 input arguments, 186 numeric value of 0,64 of 1,64 numerical integration, 451 numerical values from plot, 383

0 ODE23,460 0DE45, 460 open files, 293 OR, 68 OTHERWISE, 92 output estimate, 441,443

parallel resistors, 347 parameters, 181 parsed, 320 passing parameters, 181 path, 52 pathname, 294, 297 PAUSE,351 percentages, 253 period followed by an asterisk, 255 phasor, 16,17 pi, 9, 12, 38 PLOT, 37, 199, 260, 354, 361, 393, 397 axis - modifying, 376 axis range, 376 change axes color, 423 change font color, 425 CLOSE, 413 color, 371-373 COMET, 404 crosshair, 383, 385 default properties, 423 draw line, 386 erase, 364

figure windows, 408 font color, 425 font properties, 424 Greek letters, 381 grid, 375 handle, 414 line style, 371 log-log, 400 markers, 371 math equations, 381 message box, 385 modify axis, 376 modify objects, 417 modify text, 421 multiple-figure windows, 408 numerical values, 383 polar, 403 semilog y-dimension, 397 SEMILOGX, 393 SEMILOGY, 397 several traces, 365, 370 subplot, 396,405 text on window, 379 title, 362 two traces on plot, 365 WAITFOR command, 385 windows, 408 zoom, 377, 390 plot window, 370 plotting, 36, 361 POLAR, 403 polar plot, 403 POLYFIT, 435 polynomial, 429 CONV, 432 curve fit, 440,441,446 curve fitting, 434 evaluating, 430 multiplying, 432 plotting, 431 roots, 433 POLYVAL, 430 power absorbed by a resistor, 124 precedence, 2 predict output, 434-435 print buffer, 70 print to string, 354 PROD, 113 PWD, 294

Q QUAD, 451,456 QUAD8.451 quadratic equation, 72, 75 question-and-answer function, 116-167,276 R radians, 16, 17 RAND, 21, 65 random number, 64, 253 range of characters, 26 r a t , 10 RC circuit, 113 read a sound file, 353 reading binary files, 316 data from files, 293 formatted data, 304 REAL, 15 realmax, 12 realmin, 12 rectangular coordinates, 15,16 recursion limit, 456 recursive, 451 recursive function, 188,458 speed, 193 relation operator, 64 repeat statements, 127 repeats indefinitely, 139 RESIDUE, 185 resistor, 124, 347 parallel function, 347 standard values, 204 resistors in parallel, 187 resistors in parallel function, 165 RETURN, 184 ROOTS, 49, 433, 449 roots of polynomial, 433-434 row, 18 row number, 216 row vector, 18 run script file from floppy disk, 57 Runge-Kutta method, 460

's',36 sampling frequency, 352 SAVE, 11 Save Workspace As, 11

Index

scalar, 19 scope, 176 script file, 50, 67 run from floppy disk, 57 search path, 54 second-order boundary-value problem, 469 seconds, 333, 336 semicolon, 18, 31, 264 semilog plot, 393, 398 SEMILOGX, 393 SEMILOGY, 398 sequence, 253 serial date number, 341 convert to string, 342 date to serial number, 343 present time, 344 SET, 382, 417 set path, 54 SETSTR, 220,231,314 several commands on single line, 32 several traces on a single plot, 38 SHG, 413 'short', 310 Show Workspace, 6 simple math operators, 2 Simpson's rule, 451, 456 SIN, 47,197-198, 255 sine function, 259 single-character response, 168 single-quote character, 25 single quotes, 24-25 singularity, 455 SIZE, 74 smallest number, 12 solve equation, 348,449 solve the system of equations, 22 sorting, 236 SOUND, 353 sound file, 353-354 spiral, 403-404 Spline, 442,445 SPLINE, 442-444 Spline fit, 447 SPRINTF, 354 SQRT, 14,76 square root, 38, 76 of-1, 14 standard 5% resistors, 124

step size, 94-95 stopping a function, 351 STR2NUM, 27, 29, 30, 36,143-144 straight-line fit, 436,440 STRCMP, 27, 28,137,169 string 24, 32 creating with SPRINTF, 354 variable, 34 STRREP, 28, 30 structure, 280 structure array, 280,282 fields, 280 structured file, 307 student versions of MATLAB, 352 subarray, 212-213 SUBPLOT, 400,405 subscript, 380, 382 substring, 26, 213 subtraction, 256 sum, 251-252 sum < limit, 139-140 sum of factorials, 118 superscript, 380, 382 text on MATLAB plot, 436 suppressed results, 31 switch, 113 SWITCH-CASE, 88 system of equations, 22-23

T table, 208 TEX, 381 TEX command superscript, 436 TEXT, 355, 381,416,422 Greek letters, 381 modify properties, 421 text file, 310 text on a plot, 378 text string, 32, 89,206,207,275, 350,355 equations, 381 evaluate, 346 formatting, 356 TIC, 339 time, 333, 336 time difference, 336-337, 339 t i t l e , 362, 382,416 title of plot, 362 TOC, 339

485

tolerance, 452 trajectory, 404 transistor, 120 transpose, 338 trials, 439 troubleshooting, 135 true, 63-64 two single quotes in a row, 25 typsetting, 380

u

U IGETFILE, 297, 299-300 UIPUTFILE, 297 UISETCOLOR, 425 UISETFONT, 424 unknown variables, 22-23 unreadable, 312 untitled.m, 53 UPPER, 28, 223 uppercase, 219-220

V valid responses, 134 variable, 3 global, 181-182 inside function, 177-178 names, 3 same name, 177 scope, 176,179 unknown, 178 variable-precision arithmetic, 112 vectors, 18,19 voltage, 113 voltage source, 124 VPA, 112

W

WAITFOR,385 wav file playing, 353 reading, 351 WAVREAD, 351-352 WHICH, 49 WHILE, 127 white background, 373 WHITEBG, 373, 374 toggle, 374 WHO, 4, 309 WHOS, 4 wild card string, 297

486 I Index window close, 413 make active, 410 windows figure, 408 Windows wav file, 351 Windows Wordpad, 304 write to a file, 303-304 writing data to file, 293

X x-axis label, 362 XLABEL, 38, 362, 381,416 xor, 68 Y y-axis label, 362 YLABEL, 38, 362, 381,416

Z ZEROS, 100 zipcode, 304, 315 zoom, 390 zoom in to plot, 377

Suggest Documents