1170:Lab4 Septempber 11th, 2012

advertisement
1170:Lab4
Septempber 11th, 2012
Goals For This Week
This week we’ll look at a simple model of the lung that is outlined in the book.
• First, I’ll describe the model.
• We’ll simulate it with a for loop.
• We’ll also learn how to save your work in R.
1
A mathematical model of the lung
The following model is taken from section 1.9 of your book.
Our main question is to figure out how the rate of oxygen uptake in your lungs depends on the concentration of oxygen in the air.
First, let ct be the concentration of oxygen in your lungs. Your have volume V , so that total amount of
oxygen is ct V . When you exhale, the volume of your lungs decreases by W , so the oxygen in your lungs is
now ct (V − W ). Let γ be the concentration of oxygen in the outside air. When you inhale again, the amount
of oxygen rises to ct (V − W ) + γW . We’ll call this new values ct+1 V .
We now have a disrete dynamical system that describes how the amount of oxygen in your lungs changes
between breaths.
ct (V − W ) + γW
V
Note that if ct = γ then ct+1 = γ. Thus, c∗ = γ is the steady state of the system. In other words, given
enough time, we would expect the concentration of oxygen in your lungs to be the same as in the outside
air. In reality however this will never happen as oxygen is absorbed from your lungs into your blood.
We can quickly adapt our model to include the aborption of oxygen. Before exhaling, the concentration
of oxygen decreases to (1 − α)ct where α represents the fraction of oxygen that is absorbed. Our new system
is
ct+1 =
ct (V − W )(1 − α) + γW
V
and αct V is the amount of oxygen absorbed every breath.
ct+1 =
2
Saving your work
You may want to save your work in R for future use or so that you can run repetitive commands several
times.
These instructions apply for the departmental computers. If you are using your own installation of R
then you can just select File-¿New like you would in a normal program.
First, open a new terminal by right clicking on the desktop background. Then type
1
> gedit lab4.R &
This commands creates a file called lab4.R and allows you to edit it with gedit, which is like a fancy
version of notepad. Now you can start R in the normal way.
You can type R commands into lab4.R and then save them. To run all your saved commands use
> source("lab4.R")
You’ll need to reenter this command every time you make a change to the lab4 file.
3
Automated For Loops
Now we’re going to write a function that will simulate the above model of the lung.
Recall from last week how things got a bit repetitive and slightly confusing when we tried to run the
same for loop with different parameters? Our function is going to do everything for us.
In your file ”lab4.R”, we’re going to start writing a function. First we’ll write the skeleton of the function.
lungs<-function(c_0,V,W,g,a,N){
}
Currently this function doesn’t do anything. We’ll start to add in code between the curly braces. Note
that our function takes in 6 variables. The parameters V and W appear in our discrete model. I’m using
g and a here in place of γ and α. The value c0 is the initial oxygen concentration in the lung. Finally, N
is the number of breaths we want to simulate. That’s all the information that we need in order to run the
simulation and plot the result.
The first thing to do is to create a new vector of the correct length and set the initial condition.
c<-seq(1,N)
c[1]<-c0
Now our function looks like this:
lungs<-function(c_0,V,W,g,a,N)
c<-seq(1,N)
c[1]<-c0
We’ll stand need to add two more commands: one for loop and one plot command. I’ll leave it up to
you guys to figure out what they should be. Be sure to include an appropriate x label, y label and main
function.
lungs<-function(c_0,V,W,g,a,N)
c<-seq(1,N)
c[1]<-c0
<for command>
<plot command>
When you are done, you can run your function like this.
> source("lab4.R")
> lungs(10,6,.6,20,.05,30)
The above example is with c0 = 10, V = 6,W = .6,γ = 20, α = .05 and N = 30.
2
4
Assignment For this Week
• Include your code with the assignment.
• Save plots for each of the following.
1. Let V = 6, W = .6, γ = 20, α = .05, c0 = 10 and N = 30.
2. Same as above except c0 = 30.
3. Let V = 6, W = .6, γ = 20, α = .5, c0 = 10 and N = 30.
4. Same as above except c0 = 30.
5. Let V = 6, W = .6, γ = 20, α = .5, c0 = 10 and N = 100.
6. Same as above except c0 = 30.
• Describe how each parameter changes the plot. Explain why each parameter has the affect that it
does.
3
Download