LAB FIVE Modelling in Excel Exercises marked with a are based on material from A Guide to Microsoft Excel 2007 for Scientists and Engineers, Bernard Liengme, 2009. 5.1 Walkthrough examples In this section we will get Solver set up in Excel, and try an example to demonstrate different ways of solving equations in Excel. 5.1.1 Walkthrough 1: Setting up Solver The first time you use Solver you may have to install it on your machine. To do this, open up Excel. 1. Check if Solver is already installed (in the labs at uni, this may well have been done already). Look under the Data tab for the Data Analysis category; is Solver listed? If not, continue with the steps below. 2. Click on the main menu button and go to Excel Options 3. Go to the Add-ins tab, choose Solver and click Go 4. Check the Solver box and click OK Solver should now install on your machine 5. You should now see it on the Data tab, under Data Analysis 1
5.1.2 Walkthrough 2: Solving an Equation This walkthrough demonstrates different ways of solving equations using Excel. Find the roots of the equation in the range 0 < x < 1 e x sin(x) = 0 (5.1) in these different ways: A) By plotting Plot the curves y = e x and y = sin(x) and locate the point where the curves cross. Set up a spreadsheet with three columns: x, exp( x) and sin(x). It should look something like this: Add a chart with x on the x-axis and the two functions on the y-axis. It should look something like this: Determine the root of the equation by visually finding the x value of the point at which the curves cross. Check this is reasonable by substituting x into the original equation, either analytically or using Excel. B) Using GoalSeek One way you could try to find roots of the equation is set up a spreadsheet with x and f (x) and guess values of x until f (x) equals zero. This is exactly what GoalSeek will do for you! Set up a spreadsheet with cell A5 equal to x and cell B5 equal to f (x) i.e. =exp(-a5)-sin(a5). It should look something like this: it contains the formula James Curran and Tara Murphy 2
Now go to the Data tab and under What-if-Analysis, run Goal Seek. Choose to set cell B5 to value 0 by changing A5 This should result in an answer of x = 0.589 as shown below C) Using Solver Finally, we can use Solver, a more powerful optimisation tool. This is not strictly necessary for this problem GoalSeek does a good job, but it is useful to try it out on a problem we already know the answer to. Set up the same spreadsheet as for part (B) Go to the Data tab and under Analysis, run Solver. There are multiple ways you can use Solver, but we are going to use it with constraints because this generalises well to harder problems. So, enter the constraint B5=0 as shown below You can now Solve and should get a similar answer to your previous methods. James Curran and Tara Murphy 3
5.2 Exercises 5.2.1 Modelling Recall the predator-prey model we discussed in Lecture 4: Problem: An ecological niche contains two species: one is the prey, the other the predator. A theoretical analysis of the problem has yielded equations for the successive population of the two species: where N t+1 = (1.0 B(N t 100))N t KN t P t P t+1 = QN t P t N t P t the population of prey in generation t the population of predators in generation t B the net birth-rate factor K the kill-rate factor Q efficiency in use of prey The initial parameters for this model are N 0 = 50, P 0 = 0.2, B = 0.005, K = 0.5 and Q = 0.0205. Set up this model in a spreadsheet and create a graph of the change predator population and prey population with generation. Experiment to see what happens when you 1. Increase/decrease the birth-rate 2. Change the size of the starting population 3. Increase/decrease the efficiency factor (Q) 5.2.2 Solving equations Solve the system of non-linear equations below by considering them as constraints in Solver. x 2 + 2y 2 = 22 2x 2 + xy 3y = 11 (5.2) (5.3) Confirm your answer by plotting the functions over a suitable range. 5.2.3 Maximising area The area of a rectangle is given by A = xy. What will the x,y coordinates for the top right corner of the rectangle below be, such that it touches the curve 3y = 18 2x 2 (5.4) and the area of the rectangle is maximised? James Curran and Tara Murphy 4
5.2.4 1D Game of Life The Game of Life is the best know example of a cellular automaton and is one of the most famous games in computer science. Each cell in a 2D grid is allowed to be alive or dead. It works iteratively according the following rules: 1. Any live cell with fewer than two live neighbours dies, as if by needs caused by underpopulation. 2. Any live cell with more than three live neighbours dies, as if by overcrowding. 3. Any live cell with two or three live neighbours lives, unchanged, to the next generation. 4. Any dead cell with exactly three live neighbours becomes a live cell. See http://en.wikipedia.org/wiki/conway s_game_of_life for more details and demos. Cellular automata are used widely in simulations in fields such as biology and economics. They are also of interest to computer scientists because cellular automata are Turing complete which means that any program could be rewritten using cellular automata (scary!). In this exercise you have to set up a 1D cellular automaton that evolves by iterating down the rows in a spreadsheet. To do this Set up a seed generation in row 1 of your spreadsheet. You should use the values of 1 and 0 for alive and dead. The initial generation should evolve according to the following rules, illustrated graphically below So, for example, if in generation 1, cells D1, E1 and F1 are all alive, then in generation 2, cell E2 will be dead (due to overcrowding!). If in generation 1, cells D1 and E1 were alive, but F1 was dead, then in generation 2, cell E2 would be alive. Fill in the appropriate formulae in row 2 such that when you drag the formulae down the spreadsheet, the Game will play out. As a test case, if you start with a 5 column game, with 0,0,1,0,0 in the first generation, you should end up with the pattern Hint: Assume the columns wrap around, so the parents of cell E2 in a 5 column game are D1, E1 and A1. Hint: Conditional formatting makes the game look better! 5.2.5 2D Game of Life Challenge question Note that this material is not assessable but it is fun! Try setting up a 2 dimensional Game of Life, following these instructions http://blogs.msdn.com/excel/archive/2007/11/02/iteration-conway-s-game-of-life.aspx Try implementing some of the famous Game of Life phenomena, such as gliders and pulsars http://en.wikipedia.org/wiki/conway s_game_of_life James Curran and Tara Murphy 5
5.3 Capability checklist When you ve finished this lab, check that you know how to... 1. Plot data in Excel 2. Use relative and absolute cell referencing 3. Solve an equation using GoalSeek 4. Solve an equation using Solver If you don t know how to do any of these things once you have completed the lab, please come and ask us. You should also use this checklist when you are revising for the practical test. James Curran and Tara Murphy 6