Modeling in Excel
Modeling in Excel
Modeling in Excel
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
The rst time you use Solver you will have to install it on your machine. To do this, open up Excel, and follow the instructions below
1. Click on the main menu button and go to Excel Options 2. Go to the Add-ins tab, choose Solver and click Go
3. Check the Solver box and click OK Solver should now install on your machine
LAB FIVE
5.1.2
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 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: (5.1)
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 nding the x value at which the curves intersect. 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 nd 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. it contains the formula =exp(-A5)-sin(A5). It should look something like this:
LAB FIVE
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.
LAB FIVE
5.2
5.2.1
Exercises
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: Nt+1 Pt+1 where Nt the population of prey in generation t Pt the population of predators in generation t B the net birth-rate factor K the kill-rate factor Q efciency in use of prey The initial parameters for this model are N0 = 50, P0 = 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 efciency factor (Q) = = (1.0 B(Nt 100))Nt KNt Pt QNt Pt
5.2.2
Solving equations
x2 + 2y2 = =
Solve the system of non-linear equations below by considering them as constraints in Solver. 22 11 (5.2) (5.3)
2 x + xy 3y
2
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 2 x2 and the area of the rectangle is maximised? (5.4)
LAB FIVE
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/Conways_Game_of_Life for more details and demos. Cellular automata are used widely in simulations in elds 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 rst 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
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/Conways_Game_of_Life
LAB FIVE
5.3
Capability checklist
When youve nished 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 dont 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.