3
$\begingroup$

I'm trying to ListPlot3D the following function

$f=\frac{-\frac{(t-1) (d-s) \left(2 c d (d (q-1)+s)-s \left(-2 d^2+d (s+2)+s^2\right)\right)}{2 s^2}+d^2-d s-d t+s t}{s}$

against $c \in [0,1]$ and $q \in [1,2]$ under the conditions of $s=2$, $d=0.8$, $t=0$, $0\leq c \leq 1$, and $1 \leq q \leq \frac{1}{c}$.

I'm struggling with how to reflect the last condition, i.e. $1 \leq q \leq \frac{1}{c}$ in my Mathematica code. I used Assumption but didn't work.

Here is the code I tried:

Block[{s = 2, d = 0.8, t = 0}, f = (d^2 - d s - ((d - s) (2 c d (d (-1 + q) + s) - s (-2 d^2 + s^2 + d (2 + s))) (-1 + t))/(2 s^2) - d t + s t)/s; maxn = Flatten[Table[{c, q, f}, {c, 0, 1, .1}, {q, 1, 2, .1}, Assumptions -> {0 < c <= 1, 1 <= q <= 1/c}], 1];] {ListPlot3D[maxn, AxesLabel -> {"c", "q", "V"}]}
$\endgroup$
4
$\begingroup$

Instead of sampling the function yourself, you can use Plot3D.

expr = Block[{s = 2, d = 0.8, t = 0}, 
  (d^2 - d s - ((d - s) (2 c d (d (-1 + q) + s) - 
          s (-2 d^2 + s^2 + d (2 + s))) (-1 + t))/(2 s^2) - d t + s t)/s
];

Plot3D[expr, {c, 0, 1}, {q, 1, 2},
 PlotPoints -> 50,
 MaxRecursion -> 5,
 RegionFunction -> 
  Function[{c, q}, 1 <= q <= 1/(c + $MinMachineNumber)]
]

enter image description here

As you can see, the troubling condition can be incorporated by using RegionFunction.

| improve this answer | |
$\endgroup$
3
$\begingroup$

Here's another way: Iterators may depend on iterators that precede them.

expr = Block[{s = 2, d = 0.8, t = 0},
 (d^2 - d s - ((d - s) (2 c d (d (-1 + q) + s) - 
     s (-2 d^2 + s^2 + d (2 + s))) (-1 + t))/(2 s^2) - d t + s t)/s];

Plot3D[expr, {c, 0, 1}, {q, 1, Min[2, 1/(c + $MinMachineNumber)]}]

enter image description here

This also works, if you don't like fudging with $MinMachineNumber (which doesn't affect the plot at all):

q2[c_?NumericQ] := Min[2, Quiet@Check[1/c, Infinity]];
Plot3D[expr, {c, 0, 1}, {q, 1, q2[c]}]

Alternatively, you can special-case zero (the definition for c == 0 must come first):

ClearAll[q2];
q2[0 | 0.] := 2;
q2[c_?NumericQ] := Min[2, 1/c];
| improve this answer | |
$\endgroup$
  • $\begingroup$ I should point out that when the boundary can be specified in this way, a crisp, clean boundary can be computed with much less computational effort than the sampling/interpolation method used in RegionFunction. $\endgroup$ – Michael E2 Jul 14 '19 at 18:55
2
$\begingroup$

Some of your c are zero and Mathematica complains about testing for 1/c

So I assume any result 1<=q<=1/0 is acceptable by checking for c==0 first and only if c !=0 do I also check for 1/c

maxn = Flatten[Table[{c, q, f}, {c, 0, 1, .1}, {q, 1, 2, .1}],1];
newmaxn=Select[maxn,(1<=#[[2]]&&#[[1]]==0)||1<=#[[2]]<=1/#[[1]]&];
ListPlot3D[newmaxn]
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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