Python Forum

Full Version: coding a function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone

I have an assignment for university which I am having some problems with. We have to define a function which will return the utility depending on consumption u_1, u_2 and u_3 and parameters which are already given. U(sol,parameters) = c1**0.5 + c2**0.5 + c3**0.5. The problem comes in when we have a look how u_2 is defined: u_2 <= q * n / z. In this case n and z are known but q and u_2 arent.

This is the code I used to define the function but obviously u_3 will be 0. how can I code this an other way?

def U(sol,params):
"""
Returns the lifetime utility of a young person as a function of his consumption and money holdings (sol)

as well as the parameters of the model (params)

"""

y, z, n, beta = params

q = sol

u_1 = (y - q) ** (1/2)

u_2 = ((n / z) * q) ** (1/2)

u_3 = ((n/z)**2 * q - (n/z)**2 * q)** (1/2)

return u_1 + beta * u_2 + beta**2 * u_3
Please use proper code tags while coding
Not enough information. You say that U1-U3 are given, but then you calculate them. From your formulas you have to be given q. This does not quite make sense. Csn you rephrase, and repost your code using tags?
Hello everyone

I have an assignment for university which I am having some problems with. We have to define a function which will return the total utility depending on consumption single utilities u_1, u_2 and u_3.
U(sol,parameters) = u_1 + beta * u_2 + beta**2 * u_3. The problem comes in when we have a look how u_2 is defined: u_2 <= (q * n / z). In this case n and z are known(as are y and beta later on) but q isnt.

This is the code I used to define the function but obviously u_3 will be 0. how can I code this an other way?

I have to create a function where I can plug in q and the parameters and get the total Utility U
The first part is the description of the function that my teacher has given me.
 def U(sol,params):
"""
Returns the lifetime utility of a young person as a function of his consumption and money holdings (sol)

as well as the parameters of the model (params)

"""

y, z, n, beta = params

q = sol

u_1 = (y - q) ** (1/2)

u_2 = ((n / z) * q) ** (1/2)

u_3 = ((n/z)**2 * q - (n/z)**2 * q)** (1/2)

return u_1 + beta * u_2 + beta**2 * u_3 
I stand by my prior post. Repeating the original (though with tags) is not really helpful
u_1, u_2 and u_3 are not given. only the parameters are given. What other info do you need in order to help me?
You have created a function that calculates U1-3, what specifically do you need?
What is missing, I think, is a problem statement. Is the problem that you don't know how to solve the equation? That is not a question for this forum. Is the question that you don't know how to write the equation(s) in Python? For that we need to know the equation(s), and all you've provided is a python implementation that supposedly doesn't work