Tutorial 7: plotting defining functions of ODEs
Objectives:
- Use functions to graph the defining functions of ODEs
- Pass function names to other functions (optional)
Plotting defining functions of ODEs
Let consider an ODE that has a defining function with a constant term C:
\[ \frac{dN}{dt} = bN-dN + C\]
The R function for the ODE can be defined as follows:
To analyze the ODE graphically, let us create a plot of the defining function \(f(N)\) over a range of values of \(N\). This requires choosing a range that includes all the zeros of the function \(f(N)\), which are the fixed points of the ODE. So if we let \(b=0.3\), \(d=0.32\), and \(C=1\), the function \(f(N) = -0.02N+1\) has a zero at 50, so we can assign the range of values of N from 0 to 100 and make a graph of the function over this range:
Note that we need to use the function unlist()
to turn dNdt into a regular vector from a list (the list structure is necessary for it to work with the function ode
). Also note that we had to define the vector time
even though it is not used in the calculation because it is an input of the function pop_funk2
m again because itβs required by ode
.
The plot of the defining function shows the rate of change of the solution (dN/dt) as a function of N. For population values below the fixed point of 50, solutions grow, while for N>50 solutions decay, both converging to the asymptotic value of 50. This can be shown by plotting several solutions obtained by calling ode
:
Calling functions using strings (optional)
For the curious, here is a way to specify and call a function based on a given character string. You can see that calling the new_fun()
is the same as calling the original function blah()
:
This is very useful if you want to pass the name of a function as a string (e.g. blah
) to another function (e.g. my_funk
), so then it can be used to call the specified function from within my_funk
. This allows you to write a general function that can call any number of functions and perform the same calculations with them.