PDA

View Full Version : What am i missing?



HAYD1979
03-01-2012, 07:39 PM
Could someone possibly give me some pointers as to what i am missing in the top two functions below? I am supposed to create function where demand is calculated first and then used to perform the second calculation called "Profit". The "demand" calculation is supposed to be a part of the "profit" function, as i understand it. Only problem is i have no idea how to get it to work.

The third function, at the bottom, actually works fine. So i am a bit amazed that i can't get the top ones to work as they seem a lot simpler to me. :help

Option Explicit

Function demand(alpha, beta, gamma, price, adv)

Dim demand

demand = alpha * (price ^ beta) * (adv ^ gamma)

End Function

Function Profit(alpha, beta, gamma, cost, price, adv)

Profit = (price - cost) * demand(alpha, beta, gamma, price, adv) - adv

End Function

Function Opt_Price(alpha, beta, gamma, cost, adv)

Dim price, max_profit, price_start

price_start = 10

max_profit = Profit(alpha, beta, gamma, cost, price_start, adv)

For price = 10 To 100 Step 1

If max_profit < Profit(alpha, beta, gamma, cost, price, adv) Then
max_profit = Profit(alpha, beta, gamma, cost, price, adv)

Opt_Price = price

End If

Next price

End Function

Bob Phillips
03-02-2012, 01:33 AM
What exactly is your problem? I haven't run your code, but it looks okay from first sight.

Paul_Hossler
03-02-2012, 09:24 AM
The only real thing I see is in Demand().

There is a variable the same name as the function

I didn't have a feel for realistic numbers, but single stepping through sub Test seems to at least go all the right places

BTW, I like to always explicitly Dim variables, so that's why I added the ' as Double' in many places




Option Explicit

Function demand(alpha As Double, beta As Double, gamma As Double, price As Double, adv As Double) As Double
'Dim demand ----------- removed --- same name as function

demand = alpha * (price ^ beta) * (adv ^ gamma)

End Function


Function Profit(alpha As Double, beta As Double, gamma As Double, cost As Double, price As Double, adv As Double) As Double

Profit = (price - cost) * demand(alpha, beta, gamma, price, adv) - adv

End Function

Function Opt_Price(alpha As Double, beta As Double, gamma As Double, cost As Double, adv As Double) As Double

Dim price As Double, max_profit As Double, price_start As Double

price_start = 10

max_profit = Profit(alpha, beta, gamma, cost, price_start, adv)

For price = 10 To 100 Step 1

If max_profit < Profit(alpha, beta, gamma, cost, price, adv) Then
max_profit = Profit(alpha, beta, gamma, cost, price, adv)

Opt_Price = price

End If

Next price

End Function


Sub test()
MsgBox Opt_Price(0.95, 0.98, 0.99, 10000, 5000)
End Sub


Paul