



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




































H ‘* Orly Alter 2008 L H Introduction to Computing for Biomedical Engineers, Spring 2008 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
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 = 1.5; y = If @ x > 1 , 0 , x D 0 x = 2.5; y = If @ x > 1 , 0 , x D 0 H* Problem 22 H 10 %L : Define a new function, sumfunc2 @ n _ ,m _D that uses sumfunc1 @ n _ ,m _D from Problem 18 , to sum all the entries directly surrounding the entry in the n - th row and m - th column of the array, but only if 1 < n < 10 and 1 < m < 10 , otherwise it returns 0. Execute your code for the 8 n,m < pairs 8 1 , 2 < , 8 2 , 2 < , 8 3 , 9 < , 8 3 , 10 < , 8 10 , 10 < 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 sumfunc2 @ n _ , m _D : = If @ 1 < n < 10 , If @ 1 < m < 10 , sumfunc1 @ n, m D , 0 D , 0 D sumfunc2 @ 1 , 2 D sumfunc2 @ 2 , 2 D sumfunc2 @ 3 , 9 D sumfunc2 @ 3 , 10 D sumfunc2 @ 10 , 10 D sumfunc2 @ 8 , 5 D 0 4 3 0 0 5 H Define Subroutines or Functions on Data Arrays L H Define a Function that Multiplies a Cell in a Data Array by Two *L ? : = lhs := rhs assigns rhs to be the delayed value of lhs. rhs is maintained in an unevaluated form. When lhs appears, it is replaced by rhs , evaluated afresh each time. à tab = 88 1 , 2 , 3 < , 8 4 , 5 , 6 < , 8 7 , 8 , 9 << ; TableForm @ tab D 1 2 3 4 5 6 7 8 9 f @ n _ , m _D : = tab @@ n, m **DD *** 2 tab @@ 1 , 1 DD f @ 1 , 1 D 1 2
tab @@ 2 , 3 DD f @ 2 , 3 D 6 12 H* Define a Function that Conditionally Multiplies a Cell in a Data Array by Two *L f @ n _ , m _D : = If @ 1 < n ***** m, tab @@ n, m *DD *** 2 , tab @@ n, m DDD f @ 1 , 1 D f @ 2 , 3 D 1 12 H Apply a Function that Conditionally Multiplies a Cell by Two to the Whole Array L ? Outer Outer@ f , list 1 , list 2 , …D gives the generalized outer product of the listi , forming all possible combinations of the lowest-level elements in each of them, and feeding them as arguments to f. Outer@ f , list 1 , list 2 , …, n D treats as separate elements only sublists at level n in the listi. Outer@ f , list 1 , list 2 , …, n 1 , n 2 , …D treats as separate elements only sublists at level ni in the corresponding listi. à tab1 = Outer @ f, 8 1 , 2 , 3 < , 8 1 , 2 , 3 <D ; TableForm @ tab1 D 1 4 6 8 10 12 14 16 18 tab2 = Outer @ f, Table @ a, 8 a, 1 , 3 <D , Table @ a, 8 a, 1 , 3 <DD ; TableForm @ tab2 D 1 4 6 8 10 12 14 16 18 H Problem 23 H 10 %L : Rerun the code to generate a 10 μ 10 data array of randomly selected zeroes and ones. Print the results using TableForm. Now define a function that sets the boundary cells in your 10 μ 10 data array to zero using a conditional function, and apply it to the array using the Outer command above. Hint: Table @ a, 8 a, 1 , 10 <D= 8 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 <.. Save the result in the variable "tab". Print the result using TableForm and compare to the original data array to test your code. *L Table @ a, 8 a, 1 , 10 <D 81 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 <
tab = Outer @ zeroboundary, Table @ a, 8 a, 1 , 10 <D , Table @ a, 8 a, 1 , 10 <DD ; TableForm @ tab D ArrayPlot @ tab D 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 1 1 0 0 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 1 0 0 0 1 0 0 0 1 1 0 1 0 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 H* Problem 24 H 10 %L : Apply your sum function sumfunc2 @ n _ ,m _D from Problem 22 to the data array of Problem 23 and save it in "sum1" using the Outer command above. Hint: Table @ a, 8 a, 1 , 10 <D= 8 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 <. This gives an evaluation of the sum of all entries directly surrounding a cell for each cell in the data array at once, where the boundary cells are kept at zero. Print the result using TableForm. *L Table @ a, 8 a, 1 , 10 <D 81 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 <
sum1 = Outer @ sumfunc2, Table @ a, 8 a, 1 , 10 <D , Table @ a, 8 a, 1 , 10 <DD ; TableForm @ sum1 D ArrayPlot @ sum1 D 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 2 3 3 2 0 0 2 2 1 1 4 6 5 3 0 0 1 2 1 3 5 6 4 2 0 0 1 2 1 2 5 5 4 1 0 0 3 3 3 3 5 2 3 1 0 0 2 3 4 2 3 2 3 1 0 0 4 4 5 4 3 2 3 1 0 0 2 2 4 2 3 1 2 1 0 0 0 0 0 0 0 0 0 0 0 H* Problem 25 H 10 %L : Compute the sum of all entries directly surrounding a cell and that of the cell itself and save it in "sum2". Hint: You could simply add together which two data arrays? *L
TableForm @ N @ sum1 ê 9 DD Show @ Graphics @ Raster @ sum1 ê 9 , ColorFunction Ø Hue DD , AspectRatio Ø 1 , Frame Ø True, FrameTicks Ø False D
TableForm @ N @ sum2 ê 9 DD Show @ Graphics @ Raster @ sum2 ê 9 , ColorFunction Ø Hue DD , AspectRatio Ø 1 , Frame Ø True, FrameTicks Ø False D
sum2 = ReplaceAll @ sum2, 3 Ø 1 D ; TableForm @ sum2 D 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H* Problem 30 H 10 %L : Write code that replaces all entries in sum3 = sum1 + sum2 that equal 2 with a one. L sum3 = sum1 + sum2; sum3 = ReplaceAll @ sum3, 2 Ø 1 D ; TableForm @ sum3 D 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 0 1 1 0 1 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H Problem 31 H 10 %L : Draw a flowchart that describes this updating process up to this point. Can sum3 be considered the state of the universe after one update? L H Problem 32 H 10 %L : Put your code together: H a L Create a 10 μ 10 data array of randomly selected zeroes and ones and set the boundary to zero. H b L Display it using ArrayPlot. H c L Update once. H d L Display the update in a Raster. H e L Update a second time. H f L Display the second update. L H Initialize *L
tab = Table @ Round @ RandomReal @DD , 8 10 < , 8 10 <D ; tab = Outer @ zeroboundary, Table @ a, 8 a, 1 , 10 <D , Table @ a, 8 a, 1 , 10 <DD ; ArrayPlot @ tab D H* Update Once *L sum1 = Outer @ sumfunc2, Table @ a, 8 a, 1 , 10 <D , Table @ a, 8 a, 1 , 10 <DD ; sum2 = sum1 + tab; sum1 = ReplaceAll @ sum1, 1 Ø 0 D ; sum1 = ReplaceAll @ sum1, 2 Ø 0 D ; sum1 = ReplaceAll @ sum1, 4 Ø 0 D ; sum1 = ReplaceAll @ sum1, 5 Ø 0 D ; sum1 = ReplaceAll @ sum1, 6 Ø 0 D ; sum1 = ReplaceAll @ sum1, 7 Ø 0 D ; sum1 = ReplaceAll @ sum1, 8 Ø 0 D ; sum1 = ReplaceAll @ sum1, 9 Ø 0 D ; sum2 = ReplaceAll @ sum2, 1 Ø 0 D ; sum2 = ReplaceAll @ sum2, 2 Ø 0 D ; sum2 = ReplaceAll @ sum2, 4 Ø 0 D ; sum2 = ReplaceAll @ sum2, 5 Ø 0 D ; sum2 = ReplaceAll @ sum2, 6 Ø 0 D ; sum2 = ReplaceAll @ sum2, 7 Ø 0 D ; sum2 = ReplaceAll @ sum2, 8 Ø 0 D ; sum2 = ReplaceAll @ sum2, 9 Ø 0 D ; sum1 = ReplaceAll @ sum1, 3 Ø 1 D ; sum2 = ReplaceAll @ sum2, 3 Ø 1 D ; sum3 = sum1 + sum2; sum3 = ReplaceAll @ sum3, 2 Ø 1 D ;
tab = sum3; ArrayPlot @ tab D H* Iterate a Function Using a Loop L H Print Integers From 1 to k L k = 5 ; Do @ Print @ a D , 8 a, 1 , k <D 1 2 3 4 5 H Compute the Factorial of k > 0 L k = 5 ; factor = 1 ; Do @ 8 factor = factor ***** a, Print @ factor D< , 8 a, 1 , k <D 1 2 6 24 120 H Problem 33 H 10 %L : Put your code for updating and displaying the update in a loop. Run your code again: H a L Create a 10 μ 10 data array of randomly selected zeroes and ones and set the boundary to zero. H b L Display it using ArrayPlot. H c L Run the loop that updates the data and displays it 10 times. L H Initialize *L
tab = Table @ Round @ RandomReal @DD , 8 10 < , 8 10 <D ; tab = Outer @ zeroboundary, Table @ a, 8 a, 1 , 10 <D , Table @ a, 8 a, 1 , 10 <DD ; ArrayPlot @ tab D H* Update and Display 10 Times *L Do @ 8 sum1 = Outer @ sumfunc2, Table @ a, 8 a, 1 , 10 <D , Table @ a, 8 a, 1 , 10 <DD , sum2 = sum1 + tab, sum1 = ReplaceAll @ sum1, 1 Ø 0 D , sum1 = ReplaceAll @ sum1, 2 Ø 0 D , sum1 = ReplaceAll @ sum1, 4 Ø 0 D , sum1 = ReplaceAll @ sum1, 5 Ø 0 D , sum1 = ReplaceAll @ sum1, 6 Ø 0 D , sum1 = ReplaceAll @ sum1, 7 Ø 0 D , sum1 = ReplaceAll @ sum1, 8 Ø 0 D , sum1 = ReplaceAll @ sum1, 9 Ø 0 D , sum2 = ReplaceAll @ sum2, 1 Ø 0 D , sum2 = ReplaceAll @ sum2, 2 Ø 0 D , sum2 = ReplaceAll @ sum2, 4 Ø 0 D , sum2 = ReplaceAll @ sum2, 5 Ø 0 D , sum2 = ReplaceAll @ sum2, 6 Ø 0 D , sum2 = ReplaceAll @ sum2, 7 Ø 0 D , sum2 = ReplaceAll @ sum2, 8 Ø 0 D , sum2 = ReplaceAll @ sum2, 9 Ø 0 D , sum1 = ReplaceAll @ sum1, 3 Ø 1 D , sum2 = ReplaceAll @ sum2, 3 Ø 1 D , sum3 = sum1 + sum2, sum3 = ReplaceAll @ sum3, 2 Ø 1 D , tab = sum3, Print @ ArrayPlot @ tab DD< , 8 n, 1 , 10 <D