Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python Coding Help Needed! I'm stuck on what to put for Step 4, adding a time array. I've posted the full question and all the
Python Coding Help Needed! I'm stuck on what to put for Step 4, adding a time array. I've posted the full question and all the first three steps. Additionally, at the end of the code I have the example code we were given. Any help is greatly appreciated!
STUCK ON 4!
EXAMPLE!
Viral infections are one the main causes of disease in humans. Along with bacteria and parasite infections, viral infections are caused by the introduction of a microscopic organism" into our bodies. Sometimes these viruses live in our bodies throughout our lifetime with little damage, sometimes, they quickly kill us. We would like to build a model for how viruses infected the cells of our body. We will consider an infection in which our immune system is able to clear the virus. Since winter is coming, consider this to be the influenza virus. How do viruses work? We need to understand a bit about this in order to build our model. The most important feature is that viruses cannot reproduce on their own - that is what they need you for. Your body, to a virus, is simply a factory for making and spreading more of them. If virions were writing the history books, humans would simply be their maternity ward. The way this works is that virions enter your cells, where there is a factory at work to keep the cell functioning, and they give all of the factory workers new instructions. A factory that was making cars, once the virions enter, is now making laptops, or, in this case, rather than keeping the cell alive and healthy, the machinery of the cells makes more virons. For every virion that enters a cell, that cell might make hundreds of new virions, leading to a population explosion. Your body will need to quickly mount an immune response to keep this under control. What compartments might we need? (Look at the picture above.) Target cells; these are the healthy cells the virion might enter Infected cell; these cells produce new virions, and might die over time Virion population; the number of virons at any given time during the infection What parameters will we need? rate at which cells become infected: this reduces the value of T and increases the value of I the production rate of new virions, which increases the value of V . rate at which infected cells die rate at which virions degrade and leave the system Viral Kinetics (VK) Model A simple model is then: = FTV - 81, a = pl-rv. Using this template, fill in your code below for the VK model given above. Here it is again, so that you don't need to keep scrolling up and down: =-prv, - = BTV - 81, di = pl-rV. Step 1: Setting up the ODES The comparmental model is given to you, and it has already been converted to ODEs in the form complete the remaining steps. odeint wants (see the equations above); you just need to Step 2: Define the derivatives function # STEP 2: define the function that contains the derivatives def derivs(y, t, beta, delta, P, r): # unpack List of incoming variables target=y[@] infected=[1] virion=[2] # use variables to compute derivatives on the right-hand-side of the above equations # example: dTdt = dTdt = beta TV didt = beta TV-delta I dvdt = p I-r v # return the derivatives in a List by changing the "pass" to "return" along with the correct List return [dTdt, didt, dvdt] Step 3: Define initial conditions Since you might not have a sense for a reasonable set of initial conditions for this problem. We've defined some for you. ]: # STEP 3: define the initial conditions target_0 = 4e8 infected_0 = 0.0 virions_0 = 1.0 # Here we put them all into a list so that they can be passed to odeint yo = [target_o, infected_e, virions_@] Step 4: Define interval over which we want a solution Think about how long you run to run your model for and choose a time range. ]: # STEP 4: add time array here Step 5: Define the parameters of the model Again, since you might not have a good sense for what some reasonable choices might be, we've provided some as a starting point for your exploration. ]: # STEP 5: define model parameters here beta = le-5 delta = 4.0 p = le-2 r = 1 A Worked example Let's look at an example you can base your codes on and solve some equations. This example is for a non-linear oscillator (pendulum) system: =-wsin(x), x(0) = Xo- (0) = D. Because of the sin(x), this a non-linear ODE with no simple solution; in fact, most interesting models will contain some form of non-linearity. Alas, with odeint, you mostly don't need to worry about this issue - let the computer do it! Below is a complete, worked example that you can use as a template for building a very wide range of ODE solutions. Notice that step 1 is already done - the equations in the standard are given above. # IMPORTANT IMPORT COMMANDS import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes 3D import numpy as np matplotlib inline # Note that the Scipy Library is needed for odeint from scipy.integrate import odeint *** CODE FOR ODE SOLUTION FOR NON-LINEAR OSCILLATOR # STEP 2: define your ODE system of equations def deriv_func(y, t, freq): # unpack List coming in that contains current values position = y[@] velocity = y[1] # the right-hand-side of the ODE system of equations deriv_1 = velocity deriv_2 = -freg**2 * np.sin(position) # send back the derivatives return (deriv_1, deriv_2] # send back the der VacLves return (deriv_1, deriv_2] 5 # STEP 3: setup up the initial conditions X_0 = 3.6 # initial position v_0 = 0.5 # initial velocity yo = [x_@, V_0] # initial condition, in a list # STEP 4: define time steps to use time_grid = np.linspace(0, 18., 1000) # time grid # STEP 5: define the parameters of your model frequency = 4.0 # we make it a global variable # STEP 6: solve the equations and access the solution # solve the ODES (finally!) by giving odeint the derivative function, initial # conditions and the grid over which we want a solution solution - odeint(deriv_func, yo, time_grid, args=(frequency,)) # note the syntax of this call to odeint, especially the "args" # unpack the solution into its parts pos = solution[:, ] # what does this slicing mean? Viral infections are one the main causes of disease in humans. Along with bacteria and parasite infections, viral infections are caused by the introduction of a microscopic organism" into our bodies. Sometimes these viruses live in our bodies throughout our lifetime with little damage, sometimes, they quickly kill us. We would like to build a model for how viruses infected the cells of our body. We will consider an infection in which our immune system is able to clear the virus. Since winter is coming, consider this to be the influenza virus. How do viruses work? We need to understand a bit about this in order to build our model. The most important feature is that viruses cannot reproduce on their own - that is what they need you for. Your body, to a virus, is simply a factory for making and spreading more of them. If virions were writing the history books, humans would simply be their maternity ward. The way this works is that virions enter your cells, where there is a factory at work to keep the cell functioning, and they give all of the factory workers new instructions. A factory that was making cars, once the virions enter, is now making laptops, or, in this case, rather than keeping the cell alive and healthy, the machinery of the cells makes more virons. For every virion that enters a cell, that cell might make hundreds of new virions, leading to a population explosion. Your body will need to quickly mount an immune response to keep this under control. What compartments might we need? (Look at the picture above.) Target cells; these are the healthy cells the virion might enter Infected cell; these cells produce new virions, and might die over time Virion population; the number of virons at any given time during the infection What parameters will we need? rate at which cells become infected: this reduces the value of T and increases the value of I the production rate of new virions, which increases the value of V . rate at which infected cells die rate at which virions degrade and leave the system Viral Kinetics (VK) Model A simple model is then: = FTV - 81, a = pl-rv. Using this template, fill in your code below for the VK model given above. Here it is again, so that you don't need to keep scrolling up and down: =-prv, - = BTV - 81, di = pl-rV. Step 1: Setting up the ODES The comparmental model is given to you, and it has already been converted to ODEs in the form complete the remaining steps. odeint wants (see the equations above); you just need to Step 2: Define the derivatives function # STEP 2: define the function that contains the derivatives def derivs(y, t, beta, delta, P, r): # unpack List of incoming variables target=y[@] infected=[1] virion=[2] # use variables to compute derivatives on the right-hand-side of the above equations # example: dTdt = dTdt = beta TV didt = beta TV-delta I dvdt = p I-r v # return the derivatives in a List by changing the "pass" to "return" along with the correct List return [dTdt, didt, dvdt] Step 3: Define initial conditions Since you might not have a sense for a reasonable set of initial conditions for this problem. We've defined some for you. ]: # STEP 3: define the initial conditions target_0 = 4e8 infected_0 = 0.0 virions_0 = 1.0 # Here we put them all into a list so that they can be passed to odeint yo = [target_o, infected_e, virions_@] Step 4: Define interval over which we want a solution Think about how long you run to run your model for and choose a time range. ]: # STEP 4: add time array here Step 5: Define the parameters of the model Again, since you might not have a good sense for what some reasonable choices might be, we've provided some as a starting point for your exploration. ]: # STEP 5: define model parameters here beta = le-5 delta = 4.0 p = le-2 r = 1 A Worked example Let's look at an example you can base your codes on and solve some equations. This example is for a non-linear oscillator (pendulum) system: =-wsin(x), x(0) = Xo- (0) = D. Because of the sin(x), this a non-linear ODE with no simple solution; in fact, most interesting models will contain some form of non-linearity. Alas, with odeint, you mostly don't need to worry about this issue - let the computer do it! Below is a complete, worked example that you can use as a template for building a very wide range of ODE solutions. Notice that step 1 is already done - the equations in the standard are given above. # IMPORTANT IMPORT COMMANDS import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes 3D import numpy as np matplotlib inline # Note that the Scipy Library is needed for odeint from scipy.integrate import odeint *** CODE FOR ODE SOLUTION FOR NON-LINEAR OSCILLATOR # STEP 2: define your ODE system of equations def deriv_func(y, t, freq): # unpack List coming in that contains current values position = y[@] velocity = y[1] # the right-hand-side of the ODE system of equations deriv_1 = velocity deriv_2 = -freg**2 * np.sin(position) # send back the derivatives return (deriv_1, deriv_2] # send back the der VacLves return (deriv_1, deriv_2] 5 # STEP 3: setup up the initial conditions X_0 = 3.6 # initial position v_0 = 0.5 # initial velocity yo = [x_@, V_0] # initial condition, in a list # STEP 4: define time steps to use time_grid = np.linspace(0, 18., 1000) # time grid # STEP 5: define the parameters of your model frequency = 4.0 # we make it a global variable # STEP 6: solve the equations and access the solution # solve the ODES (finally!) by giving odeint the derivative function, initial # conditions and the grid over which we want a solution solution - odeint(deriv_func, yo, time_grid, args=(frequency,)) # note the syntax of this call to odeint, especially the "args" # unpack the solution into its parts pos = solution[:, ] # what does this slicing meanStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started