Sei sulla pagina 1di 3

Tutorial: Differential Equations in Neuroscience 1

-Matt Jones, 263-4394, MSC 283, jonesmat@physiology.wisc.edu


Exercise # 1: The simplest differential equation in the world.
In this exercise, we’ll learn how to use the simplest commands in Excel, set up the Mass
Action equation as a finite difference equation, iterate it to find the solution, and graph the
results. Skip ahead as necessary if you already know how to use Excel.

1) Start Microsoft Excel. You should see something like this:

If Excel doesn’t automatically open a new, blank, spreadsheet window, then go to the File menu
item at the top and choose New->Spreadsheet, or New->Workbook. Look at the Workbook1
window. There are letters across the top, and numbers running down the left side. These identify
each “cell” in the window, A1, B1, C34, etc. Click in cell A1, and type this text: “Initial
Conditions”. Then click on cells A3, A5 and A7 sequentially, and each time, type one of these
phrases: “delta-t”, “S-naught” (this is how you pronounce S0) “k”. Then in cells B3, B5 and B7,
type in our initial condition values for the corresponding items:, 0.5, 1 and 1. Your window
should now look like this:

1) Next we’ll create a series of time values, based on our delta-t value. In cell D1, type “Time,

ms”, and in cell D2, type 0. The first time value is zero. The next time will be 1 x ∆t, then 2x
∆t, etc. But we won’t type all these in. Instead, we’ll make Excell keep track of everything. In
cell D3, type an equals sign and before pressing[return] click the mouse in cell D2, and again
in cell B3. You should now see this in cell D3: Excel has automatically filled in
the cell values that you clicked, and separated them by a “+” sign. What you have done is told
Excel that it should fill cell D3 with the sum of the values in D2 and B3. Now press [return],
and watch cell D3. It should suddenly display the value 0.5 (which is the sum of what’s in D2
and B3). This is not quite what we want, though, because Excel does not really interpret this
Tutorial: Differential Equations in Neuroscience 2
-Matt Jones, 263-4394, MSC 283, jonesmat@physiology.wisc.edu
as D2+B3, but rather as “the cell immediately above the current cell plus the cell two
columns to the left of this cell”. This is called relative addressing, because the values come
from specified positions relative to the current cell. It is extremely useful, and we will use it a
lot, but it’s not what we want right now. We want absolute addressing, and we’ll get it by
placing a dollar sign ($) in front of the letter and number of the cell we intend. So click back
in D3. You’ll see the text “D3” appear in a little box about an inch below the File menu, and
to the right of this you’ll see the text you typed into D3 before. Click here, and insert a $ just
before the B and the 3 in B3, so the equation now reads: “=D3+$B$3”, then press enter.
These dollar signs tell Excel that you want it to add the contents of the cell at absolute
location B3, to whatever is in the cell immediately above the current cell. If we copy the
equation to subsequent cells, they will all follow this rule, and all refer back to cell B3 to get
the correct value of delta-t. Now for the whole series. Click in D3, and drag the mouse
downward to select all the cells from D3 to D62 (they will change color when selected). Now
go up to the Edit menu item, and choose Edit->Fill->Down. All the cells you selected should
fill up with numbers, because the equation you wrote in D3 has now been copied to all the
selected cells. If you got the absolute addressing part right, then these values should start at 0
in D2 and end at 30 in D62, and should go in steps of ∆t (i.e., 0.5). Just for kicks, click in cell
B3, change it to 0.1, press [return] and watch what happens to the series you just created (If
nothing happened, go to Tools->Preferences->Calculation, and make sure the “Automatic”
checkbox is selected). Then change B3 back to 0.5 and press [return].
2) Before numerically solving the finite difference equation, let’s create the exact or analytical
solution, so that we can compare our numerical solution with it later, to test the accuracy of
our approximation. Click in cell E1 and type the text:”S-exact”. Then go to E2, and type in
the analytical solution (Eqn. 2). We want the contents of this cell to equal S-naught times e
raised to the power of (minus k times time). So click in E2, type an equals sign, and before
pressing [return], click again in B5. Then type “ * exp(“. The “*” is computerese for a
multiplication sign, and “exp()” is computerese for “e raised to the power of…”, then type a
minus sign, then click in B7 (which is where k is stored), type “*” again, then click in D2
(which is the first time value), and finally type”)” to close the parentheses and press [return].
A 1 should appear in D2, which is the correct answer, but won’t be for other cells unless we
make sure that Excel always looks to absolute address B5 to find the correct value of S-
naught, and to absolute address B7 to find k. You will need to add absolute addressing
symbols, “$”, to make $B$5 and $B$7. Do this now. Then click and drag the mouse from E2
down to E62 to select all the cells. Go to Edit->Fill-Down, to copy the equation to all the
selected cells, press [return], and watch the column fill up with numbers. If you got the
absolute and relative addressing parts right, then E2 should contain 1, E3 should contain
0.606…, E3 should contain 0.367, and by the time you get past time=5 ms, the numbers
should be less than about 0.006. If not, go back and check your cell addressing.
3) Next, the actual finite difference equation. In cell F1, type the title “S-approx”, and in F2, tell
Excel where to get the initial value, S-naught: “=$B$5”. Then type Eqn.4 into cell F3, but
instead of typing the characters for k, ∆t and St, tell Excel what absolute or relative address to
look in for those values. We want absolute addresses for k and for ∆t, because those numbers
never change and Excel needs to always get them from exactly the same cell every time. But
we want relative addresses for St, because that value changes on every time step, and we want
Excel to follow it along down the whole column. Try putting in the right equation now.
Done? Good. Here is what I hope you typed in cell F3: “=-$B$7*F2*$B$3+F2”. Now click
Tutorial: Differential Equations in Neuroscience 3
-Matt Jones, 263-4394, MSC 283, jonesmat@physiology.wisc.edu
and drag to select F3 (not F2) down to F62, and do the Edit->Fill->Down thing again to copy
the equation down all the cells in this column. Look at the new numbers, and compare them
with those in the S-exact column. Are they close? Let’s plot them in a graph.
4) Click in D1 and drag down and across to F63 to select the Time, S-exact and S-approx
columns all at once. Now open Excel’s graph manager by going to the menu and choosing
Insert->Chart. Click on the item that says “XY (Scatter)”, then click on the displayed graph
type that has the point markers connected by straight lines, and then click [Next]. It oughta
show you a sample graph, and hopefully there are two exponentialish-looking curves on it.
Click [Next] repeatedly until you get to the window that says “Chart Location”, and select
“As object in Sheet 1”. This will create the graph figure and stick it right into your
spreadsheet. If you don’t see these windows, then you may be using an old version of Excel,
and you’ll need to try and guess the right answers to get the chart to appear within the
spreadsheet. Shouldn’t be too hard to figure out, I hope. When the chart appears in the
spreadsheet, select it and drag it off to the right side of the S-approx column, so you can see
everything in the window properly.
5) Now look at the chart. It should have two exponential curves, one of which is the graph of the
exact solution, and one of which is your numerically approximated solution to the Mass
Action differential equation. How do they compare? Are they exactly identical? If not, why
do you think not? What could you do to improve the approximation?
6) Try this: Click in the delta-t cell, B3, type 0.1, and press [return]. Now how do they compare
(note that you will have changed the x-axis range by doing this)? Go back to B3, type 0.01,
press [return]. Now how do they compare?
7) Even more importantly, change delta-t to 1. Now how do they compare? Try delta-t = 2. Let
that be a lesson to you about how to choose delta-t! Try changing S-naught. Try changing k.
Cool, huh?

Potrebbero piacerti anche