# Seemingly simple optimization problem is slow

I’m trying to generate a demand curve numerically, given a utility function. The function for the demand is defined like so:

Demand[UF_, x_, m_, w_, p_] := NArgMax[{UF, {x >= 0, m >= 0, p x + m <= w}}, {x, m}][[1]] Where UF is the utility, x is consumption of a good, w is consumption of everything else in dollars, p is the price of the good in dollars and w is the budget. The constraints say that consumption must be non-negative and that the budget must not be exceeded. Now I'm trying to plot the demand curve for an innocent looking utility function: Manipulate[Plot[Demand[5 Sqrt[x] + m, x, m, w, p], {p, 1, 10}], {w, 1, 100}] This is very slow. What am I doing wrong? ================= ================= 1 Answer 1 ================= Because numerical optimization is a slow thing. You can interpolate the Demand function for your particular utility to speed the manipulation up: Clear[Demand]; Demand[uf_, w_?NumericQ, p_?NumericQ] := NArgMax[ {uf[x, m], {x >= 0, m >= 0, p x + m <= w}}, {x, m}][[1]] g = FunctionInterpolation[ Demand[5 Sqrt[#1] + #2 &, w, p], {w, 1, 100}, {p, 1, 10}] Manipulate[Plot[g[w, p], {p, 1, 10}], {w, 1, 100}]      The documentation on FunctionInterpolation is a bit sparse but checking Options[FunctionInterpolation] you see it takes some options: {InterpolationOrder -> 3, InterpolationPrecision -> Automatic, AccuracyGoal -> Automatic, PrecisionGoal -> Automatic, InterpolationPoints -> 11, MaxRecursion -> 6} that are all described elsewhere in the the documentation
– ssch
Jan 21 ’13 at 14:05