Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 5 down vote accepted

Because numerical optimization is a slow thing. You can interpolate the Demand function for your particular utility to speed the manipulation up:

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}]
share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.