# Plot a function under constraint

Let's say I have a function $F(q,p)$ and I want to plot the function in 3D view for $p$ and $q$ where $p+q=1$ As you probably notice that $p$ and $q$ are the probability of occurring/not occurring for an event.

I tried the following:

 f = (p^2 (0.25 - 0.375 q) - 0.5 (1. - 1. q)^2 q +
p (-1. + 0.875 q + 0.125 q^2))/(p (-1. + q) (1.5 + 1. q))
Plot3D[f, {p, 0, 1}, {q, 0, 1},RegionFunction -> Function[{p, q}, p + q = 1]]


Which gave me the plot for all $p,q \in [0,1]$ and this is not the thing I wanted. I tried to set the constraint to zero, where no values satisfy this except for zeros. But I also gave me the same thing.

I also tried

Plot3D[ConditionalExpression[f,  p + q = 1], {p, 0, 1}, {q, 0, 1}, BoundaryStyle -> Directive[Thick, Red], Mesh -> None]


But it gave me an empty graph.

• should be double equal p+q==1 (cant say if it will work tho) Apr 25 '18 at 0:07
• Make up a sample function to be able to do something concrete. This f[p_,q_]:=p^2+q^2; Plot3D[f[p,q],{p,0,1},{q 0,1},RegionFunction->Function[{p,q}, .93<p+q<1]] shows a plot, but if you change that .93 to .94 or if you change to p+q==1 the the plot appears empty..
– Bill
Apr 25 '18 at 0:51
• @Bill I edited my question. Based on george2079 comment, the output started to show up. but when $p=0$ and $q=1$ the behavior of my function is indeterminate. Apr 25 '18 at 1:05
• this can really be done easily by just drawing a line something like Grapics3D[Line@Table[{p,1-p,f/.q->1-p},{p,0,1,0.01}]] Apr 25 '18 at 1:16

I've found that the simplest way is to use the RegionFunction command. For example, here is a simple plot of the Gini Impurity function:

Plot3D[ z, {x, 0, 1}, {y, 0, 1},
RegionFunction -> Function[{x, y}, 0.99 < x + y < 1.01 ],
BaseStyle -> {FontSize -> 16, FontFamily -> "Latin Modern Roman"},
AxesStyle -> Black,
PlotRange -> {0, .6},
AxesLabel -> Automatic
]


which produces the following:

Note: if the constraint was simply x+y == 1, then it appears as if you get an empty graph. That is because that line is infinitesimally thin. Its best if you give the plot some "width" by putting in an inequality as a constraint.