Sie sind auf Seite 1von 8

ECE120L INTRODUCTION TO MATLAB

LABORATORY ACTIVITY #2
MATRICES
Name: Date:
Section: Laboratory Instructor:

I. Learning Outcomes:
At the end of the laboratory activity, the students should be able to:
1. Use MATLAB in creating and manipulating matrices.
2. Solve linear systems of equation using MATLAB

II. Laboratory Activity:


1. Problem: Create the following matrices using matrix shortcuts.
1A. a. Problem: 5 by 4 identity matrix
b. MATLAB Codes: >> A= eye(5,4)
c. Answer:
A=

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
0 0 0 0

1B. a. Problem: 6 by 3 ones matrix


b. MATLAB Codes: >> A= ones(6,3)
c. Answer:
A=

1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1C. a. Problem: 4 by 5 zeros
b. MATLAB Codes: >> A= zeros(4,5)
c. Answer:
A=

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1D. a. Problem: 4 by 4 uniformly distributed pseudorandom numbers
b. MATLAB Codes: >> A= rand(4,4)
c. Answer:
A=

0.8147 0.6324 0.9575 0.9572


0.9058 0.0975 0.9649 0.4854
0.1270 0.2785 0.1576 0.8003
0.9134 0.5469 0.9706 0.1419

1E. a. Problem: 3 by 5 normally distributed pseudorandom numbers


b. MATLAB Codes: >> A= randn(3,5)
c. Answer:
A=

-0.4326 0.2877 1.1892 0.1746 -0.5883


-1.6656 -1.1465 -0.0376 -0.1867 2.1832
0.1253 1.1909 0.3273 0.7258 -0.1364

2. a. Problem: If A = magic (24), what is the value in row 21, column 22?
b. MATLAB Codes: >> A= magic(24)
A=

Columns 1 through 12

576 2 3 573 572 6 7 569 568 10 11 565


25 551 550 28 29 547 546 32 33 543 542 36
49 527 526 52 53 523 522 56 57 519 518 60
504 74 75 501 500 78 79 497 496 82 83 493
480 98 99 477 476 102 103 473 472 106 107 469
121 455 454 124 125 451 450 128 129 447 446 132
145 431 430 148 149 427 426 152 153 423 422 156
408 170 171 405 404 174 175 401 400 178 179 397
384 194 195 381 380 198 199 377 376 202 203 373
217 359 358 220 221 355 354 224 225 351 350 228
241 335 334 244 245 331 330 248 249 327 326 252
312 266 267 309 308 270 271 305 304 274 275 301
288 290 291 285 284 294 295 281 280 298 299 277
313 263 262 316 317 259 258 320 321 255 254 324
337 239 238 340 341 235 234 344 345 231 230 348
216 362 363 213 212 366 367 209 208 370 371 205
192 386 387 189 188 390 391 185 184 394 395 181
409 167 166 412 413 163 162 416 417 159 158 420
433 143 142 436 437 139 138 440 441 135 134 444
120 458 459 117 116 462 463 113 112 466 467 109
96 482 483 93 92 486 487 89 88 490 491 85
505 71 70 508 509 67 66 512 513 63 62 516
529 47 46 532 533 43 42 536 537 39 38 540
24 554 555 21 20 558 559 17 16 562 563 13

Columns 13 through 24

564 14 15 561 560 18 19 557 556 22 23 553


37 539 538 40 41 535 534 44 45 531 530 48
61 515 514 64 65 511 510 68 69 507 506 72
492 86 87 489 488 90 91 485 484 94 95 481
468 110 111 465 464 114 115 461 460 118 119 457
133 443 442 136 137 439 438 140 141 435 434 144
157 419 418 160 161 415 414 164 165 411 410 168
396 182 183 393 392 186 187 389 388 190 191 385
372 206 207 369 368 210 211 365 364 214 215 361
229 347 346 232 233 343 342 236 237 339 338 240
253 323 322 256 257 319 318 260 261 315 314 264
300 278 279 297 296 282 283 293 292 286 287 289
276 302 303 273 272 306 307 269 268 310 311 265
325 251 250 328 329 247 246 332 333 243 242 336
349 227 226 352 353 223 222 356 357 219 218 360
204 374 375 201 200 378 379 197 196 382 383 193
180 398 399 177 176 402 403 173 172 406 407 169
421 155 154 424 425 151 150 428 429 147 146 432
445 131 130 448 449 127 126 452 453 123 122 456
108 470 471 105 104 474 475 101 100 478 479 97
84 494 495 81 80 498 499 77 76 502 503 73
517 59 58 520 521 55 54 524 525 51 50 528
541 35 34 544 545 31 30 548 549 27 26 552
12 566 567 9 8 570 571 5 4 574 575 1
c. Answer: 502

3. a. Problem: Create the matrix X = diag ([3:3:15]).


b. MATLAB Codes: >> X = diag ([3:3:15])
c. Answer:
X=

3 0 0 0 0
0 6 0 0 0
0 0 9 0 0
0 0 0 12 0
1 0 0 0 15
4. a. Problem: Using magic square of 5, write the values in lower triangular.
b. MATLAB Codes: >> M= magic(5)
M=

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

c. Answer: M=

17
23 5
4 6 13
10 12 19 21
11 18 25 2 9

5. a. Problem: Using magic square of 4, write the values in upper triangular.


b. MATLAB Codes: >> M= magic(4)

M=

16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
c. Answer: M=

16 2 3 13
11 10 8
6 12
1
6. a. Problem: Determine the determinant of the matrix.

[ ]
1 3 6
2 5 9
7 2 23
b. MATLAB Codes: >> A= [1,3,6;-2,5,9;7,-2,23]

A=
1 3 6
-2 5 9
7 -2 23

>> C= det(A)
c. Answer: C = 274

7. a. Problem: Determine the determinant and coefficients w, x, y, z


Given: w 12x +13y 7z = 23
2w + 15x + 16y + 12z = 19
4w 10x + y + 17z = 24
3w + 2x 3y + 2z = 14
b. MATLAB Codes: >> A=[1,-12,13,-7;2,15,16,12;4,-10,1,17;3,2,-3,2]

A=

1 -12 13 -7
2 15 16 12
4 -10 1 17
3 2 -3 2
>> C=det(A)

c. Answer: C = 28495

8. a. Problem: Define the matrices below: (use shortcuts if applicable)


1 2 3 4 1 1 1 1 16 2 3 13 1 0 0 0 1 0 0 0 3 6 9 12
1 5 11 10 8 18 24 3 15
6 8 1 5 1 1 1 0 3 0 0 0 1 0 0
A B C D E F
9 4 2 7 1 1 1 1 9 7 6 12 0 0 5 0 0 0 0 1 27 12 6 21

9 4 7 2 1 1 1 1 4 14 15 1 0 0 0 7 1 0 0 0 27 12 21 6

b. MATLAB Codes: >> A=[1,2,3,4;6,8,1,5;9,4,2,7;9,4,7,2]


>> B=ones(4)
>> C=magic(4)
>> D=diag([1,3,5,7])
>> E=[1,0,0,0;0,1,0,0;0,0,0,1;1,0,0,0]
>> F=[3,6,9,12;18,24,3,15;27,12,6,21;27,12,21,6]
c. Answer: A =

1 2 3 4
6 8 1 5
9 4 2 7
9 4 7 2

B=

1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
C=

16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
D=

1 0 0 0
0 3 0 0
0 0 5 0
0 0 0 7
E=

1 0 0 0
0 1 0 0
0 0 0 1
1 0 0 0
F=

3 6 9 12
18 24 3 15
27 12 6 21
27 12 21 6
9. a. Problem: Using the matrices of #8, solve the following:
9A. 3A+4F
9B. ABC/4F
9C. 2DEF+4B
9D. FEB/2CD
9E. (2B 3D)(2ABC)
b. MATLAB Codes: >> 3*A+4*F
>> (A*B*C)/(4*F)
>> ((2*D)*E*F)+(4*B)
>> (F*E*B)/(2*C*D)
>> ((2*B)-(3*D))*(2*A*B*C)
c. Answer: 9A =

15 30 45 60
90 120 15 75
135 60 30 105
135 60 105 30
9B =

4.7222 1.5741 -0.3148 1.8889


9.4444 3.1481 -0.6296 3.7778
10.3889 3.4630 -0.6926 4.1556
10.3889 3.4630 -0.6926 4.1556
9C =

10 16 22 28
112 148 22 94
274 124 214 64
46 88 130 172
9D =

1.0e+016 *

-0.3539 -1.0616 1.0616 0.3539


-0.7077 -2.1231 2.1231 0.7077
-0.7785 -2.3354 2.3354 0.7785
-0.7785 -2.3354 2.3354 0.7785
9E =

Columns 1 through 3

8024 8024 8024


-2176 -2176 -2176
-12376 -12376 -12376
-21352 -21352 -21352

Column 4

8024
-2176
-12376
-21352
10. a. Problem: Create the matrix below using the matrices of #8.
1 2 3 4 2 2 2 2 4 3 3 3
6 8 1 5 2 2 2 2 3 4 3 3
9 4 2 7 2 2 2 2 3 3 3 4
9 4 7 2 2 2 2 2 4 3 3 3
1 4 7 10 16 2 3 13 1 0 0 0
16 22 1 13 5 11 10 8 0 3 0 0
25 10 4 19 9 7 6 12 0 0 5 0
25 10 19 4 4 14 15 1 0 0 0 7

b. MATLAB Codes: >> [A B*2 E+3; F-2 C D]

c. Answer: ans =

1 2 3 4 2 2 2 2 4 3 3 3
6 8 1 5 2 2 2 2 3 4 3 3
9 4 2 7 2 2 2 2 3 3 3 4
9 4 7 2 2 2 2 2 4 3 3 3
1 4 7 10 16 2 3 13 1 0 0 0
16 22 1 13 5 11 10 8 0 3 0 0
25 10 4 19 9 7 6 12 0 0 5 0
25 10 19 4 4 14 15 1 0 0 0 7