Lab_10_Solutions

Lab_10_Solutions - H Orly Alter 2009*L H Introduction to...

This preview shows pages 1–4. Sign up to view the full content.

H * ‘ Orly Alter 2009 * L H * Introduction to Computing for Biomedical Engineers, Fall 2009 * L H * Lab 10: The Game of Life in Mathematica * L H * General Commands * L Off @ General:: "spell" D ; Off @ General:: "spell1" D ; H * From Lab 9 * L tab = Table @ Round @ RandomReal @DD , 8 10 < , 8 10 <D ; TableForm @ tab D 0 1 0 0 1 1 0 1 0 1 1 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0 0 0 1 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 0 0 1 1 1 0 1 0 0 1 0 0 0 1 1 1 0 1 1 1 0 0 1 1 1 0 0 0 0 1 0 0 1 0 0 0 1 0 1 0 0 0 1 1 0 0 0 1 1 1 0 1 1 1 ArrayPlot @ tab D H * Solution to Problem 17 : Write code that uses a definition of the variables n and m to sum all the entries directly surrounding the entry in the n - th row and m - th column of the array. Execute your code for the 8 n,m < pairs 8 2,2 < , 8 3,9 < and 8 8,5 < * L

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
n = 2; m = 2; y = tab @@ n - 1, m - 1 DD + tab @@ n - 1, m DD + tab @@ n - 1, m + 1 DD + tab @@ n, m - 1 DD + tab @@ n, m + 1 DD + tab @@ n + 1, m - 1 DD + tab @@ n + 1, m DD + tab @@ n + 1, m + 1 DD Clear @ m, n D 4 n = 3; m = 9; y = tab @@ n - 1, m - 1 DD + tab @@ n - 1, m DD + tab @@ n - 1, m + 1 DD + tab @@ n, m - 1 DD + tab @@ n, m + 1 DD + tab @@ n + 1, m - 1 DD + tab @@ n + 1, m DD + tab @@ n + 1, m + 1 DD Clear @ m, n D 3 n = 8; m = 5; y = tab @@ n - 1, m - 1 DD + tab @@ n - 1, m DD + tab @@ n - 1, m + 1 DD + tab @@ n, m - 1 DD + tab @@ n, m + 1 DD + tab @@ n + 1, m - 1 DD + tab @@ n + 1, m DD + tab @@ n + 1, m + 1 DD Clear @ m, n D 5 H * Solution to Problem 18 : Modify your code for Problem 17 so it defines a function, that uses a definition of the variables n and m to sum all the entries directly surrounding the entry in the n - th row and m - th column of the array. Execute your code for the 8 n,m < pairs 8 2,2 < , 8 3,9 < and 8 8,5 < * L sumfunc1 @ n _ , m _ D : = tab @@ n - 1, m - 1 DD + tab @@ n - 1, m DD + tab @@ n - 1, m + 1 DD + tab @@ n, m - 1 DD + tab @@ n, m + 1 DD + tab @@ n + 1, m - 1 DD + tab @@ n + 1, m DD + tab @@ n + 1, m + 1 DD sumfunc1 @ 2, 2 D sumfunc1 @ 3, 9 D sumfunc1 @ 8, 5 D 4 3 5 H * Define Conditional Subroutines or Functions * L H * Compute If x = 1 Then y = 0 Otherwise y = 1 * L ?If If @ condition , t , f D gives t if condition evaluates to True, and f if it evaluates to False. If @ condition , t , f , u D gives u if condition evaluates to neither True nor False. à x = 1; y = If @ x == 1, 0, 1 D 0 x = 0; y = If @ x == 1, 0, 1 D 1 x = 5; y = If @ 0 < x < 10, 0, 1 D 0 x = 15; y = If @ 0 < x < 10, 0, 1 D Lab_10_Solutions.nb p. 2
x = - 5; y = If @ 0 < x < 10, 0, 1 D 1 H * Problem 19 H 10 % L : Compute if x > 1 then y = x otherwise y = 1. * L x = - 0.5; y = If @ x > 1, x, 1 D 1 x = 0.5; y = If @ x > 1, x, 1 D 1 x = 1.5; y = If @ x > 1, x, 1 D 1.5 x = 2.5; y = If @ x > 1, x, 1 D 2.5 H * Problem 20 H 10 % L : Similarly, compute if x > 1 then x = 0 otherwise x = 1. * L x = - 0.5; x = If @ x > 1, 0, 1 D 1 x = 0.5; x = If @ x > 1, 0, 1 D 1 x = If @ x > 1, 0, 1 D 1 x = 1.5; x = If @ x > 1, 0, 1 D 0 x = 2.5; x = If @ x > 1, 0, 1 D 0 x = If @ x > 1, 0, 1 D 1 H * Problem 21 H 10 % L : Similarly, compute if x > 1 then x = 0 otherwise x does not change. * L x = - 0.5;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 41

Lab_10_Solutions - H Orly Alter 2009*L H Introduction to...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online