Question
Could anyone help me how to use rand in MATLAB to write this code: To initialize your game, randomly distribute a 2D array where each
Could anyone help me how to use rand in MATLAB to write this code:
"To initialize your game, randomly distribute a 2D array where each cell has probability 0.1 of being alive and 0.9 of being dead (i.e., your initial condition should be a random mix with roughly 10% 1s and 90% 0s)"
Thank you.
In this problem, well be simulating the fate of living cells using the rules from mathematician John Conways famous Game of Life. Cells exist at each point on a 2D grid and can be in one of two states: alive (1) or dead (0), as shown in the figure below.
In calculating the next generation, an individual cells survival depends on the state of its 8 nearest neighbors (vertically, horizontally, and diagonally adjacent cells). Your code should enforce the following classical rules to calculate each successive generation: A living cell with either 2 or 3 living neighbors survives on to the next generation. A living cell with fewer than 2 or more than 3 living neighbors does not survive on to the next generation due to isolation or overcrowding, respectively. A dead cell with exactly 3 live neighbors becomes a living cell in the next generation. You must employ periodic boundary conditions in this problem to allow the grid to wrap around onto itself in both the x and y-direction to avoid artificially influencing cells at the edge of the domain (in the simplified figure above, no boundary conditions were considered and non-visible cells were assumed to hold zeros). Refer to the lecture slides on how to implement these boundary conditions. To visualize the results, well be using the MATLAB function imagesc. To create an animation of your results, simply call imagesc for every generation and use the drawnow function to ensure that each timestep is displayed. (a) Demonstrate your method on a grid with num_rows = 150 and num_cols = 200. To initialize your game, randomly distribute a 2D array where each cell has probability 0.1 of being alive and 0.9 of being dead (i.e., your initial condition should be a random mix with roughly 10% 1s and 90% 0s). Run your simulation for 300 timesteps (generations) and present both the initial and final distributions in your report (additional images can be included, but be sure to include these two). In a separate figure, plot the number of living cells in your simulation as a function of time. 1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started