Using NSolve in a function, selecting one indexed result and using it to plot another function containing a function containing the indexed result

I have a problem using the result pcvar[g] g e (0,1) in the plot cvar1. In the plot cvar I can use it. It must be somehow connected to clearing p in the definition of pcvar[g]. I am gratefull for any suggestions.

Here's the code:

L = 0.1;
U = 1.9;
k = 2;
b = 1000;
c = 2;
z = 1;

sol1[g_] :=
NSolve[(1/(2*(U - L)*g)) (b*p^(-k) + (p - z)*(-k)*b*
p^(-k - 1)) ((L + (g*(p - c)/(p - z)) (U - L))^2 -
L^2) + (L + (g*(p - c)/(p - z))*(U - L)) b*
p^(-k) ((c - z)/(p - z)) == 0, p] // N;
pcvar[g_] := p /. sol1[g][[1]];

cslcvar[p_, g_] := Max[0, (g*(p - c)/(p - z))] // N;
vcvar[p_, g_] := L + cslcvar[p, g]*(U - L);

acvar[p_, g_] := b*p^(-k) vcvar[p, g] // N;

cvar[p_, v_, g_] :=
Piecewise[{{N[(p - c) (v*b /p^k) - (1/g) (p - z) (b /p^k) Integrate[
CDF[UniformDistribution[{L, U}], w], {w, L, v}]],
v <= InverseCDF[UniformDistribution[{L, U}],
g]}, {N[-(c - z) (v*b /p^k) + (1/g) (p - z) b/p^k Integrate[
w PDF[UniformDistribution[{L, U}], w], {w, L,
InverseCDF[UniformDistribution[{L, U}], g]}]],
v > InverseCDF[UniformDistribution[{L, U}], g]}}];

pcvar[0.1]
pcvar[0.2]

4.50312

4.72099

plotcvar[g_] := Plot[{cvar[pcvar[g], v, g]}, {v, L, U},
PlotRange -> {-50, 200},
LabelStyle -> Directive[14, Bold], PlotStyle -> {{Black, Thick}},
AxesLabel -> {"v",
"\!$$\*SubscriptBox[\(CVaR$$, \
$$\[Alpha]$$]\)(\!$$\*SuperscriptBox[\(g$$, $$A$$]\))"},
ImageSize -> {600, 500},
PlotLegends ->
Placed[LineLegend[{"\!$$\*SubscriptBox[\(CVaR$$, $$\[Alpha]$$]\)(\
\!$$\*SuperscriptBox[\(g$$, $$A$$]\)(v,\!$$\*SubsuperscriptBox[\(p$$, \
SubscriptBox[$$CVaR$$, $$\[Alpha]$$], $$\(A$$$$*$$\)]\),W))"},
LegendLayout -> {"Column", 4}], Top]];
Do[Print[plotcvar[u]], {u, 0.1, 0.9, 0.4}]

plotcvar1[g_] :=
Plot[{cvar[p, acvar[pcvar[g], g]/(b/p^k), g]}, {p, c, 10},
LabelStyle -> Directive[14, Bold],
PlotRange -> {-50, 200},
PlotStyle -> {{Black, Thick}},
AxesLabel -> {"p",
"\!$$\*SubscriptBox[\(CVaR$$, \
$$\[Alpha]$$]\)(\!$$\*SuperscriptBox[\(g$$, $$A$$]\))"},
ImageSize -> {600, 500},
PlotLegends ->
Placed[LineLegend[{"\!$$\*SubscriptBox[\(CVaR$$, $$\[Alpha]$$]\)(\
\!$$\*SuperscriptBox[\(g$$, $$A$$]\)(v,\!$$\*SubsuperscriptBox[\(p$$, \
SubscriptBox[$$CVaR$$, $$\[Alpha]$$], $$\(A$$$$*$$\)]\),W))"}], Top]];
Do[Print[plotcvar1[u]], {u, 0.1, 0.5, 0.2}]

cvar[3, acvar[pcvar[0.2], 0.2]/(b/3^k), 0.2]

15.6256

cvar[10, acvar[pcvar[0.2], 0.2]/(b/10^k), 0.2]

8.90173

acvar[pcvar[0.2], 0.2]

16.2983


Here's a simple example:

b = 1000;
c = 2;
k = 2;

sol1[g_] := NSolve[g*(p^3 - c) - b == 0, p] // N;
p1[g_] := p /. sol1[g][[3]];
a[g_] := p1[g]*(b /p1[g]^k) // N;
f[p_, v_, g_] := g (p - c) (v*b /p^k);

plot1[g_] := Plot[{f[p1[g], v, g]}, {v, 0.2, 1.8}];
Do[Print[plot1[u]], {u, 0.1, 0.9, 0.4}]

plot2[g_] := Plot[{f[p, a[g]/(b/p^k), g]}, {p, c, 10}];
Do[Print[plot2[u]], {u, 0.1, 0.9, 0.4}]


If I do not define p1 and a in terms of g, but explicitly choose a value, I can manually solve the problem and plot the graphs, but it should not be necessary.

b = 1000;
c = 2;
k = 2;
g = 0.2;

sol1 = NSolve[g*(p^3 - c) - b == 0, p] // N;
p1 = p /. sol1[[3]];
a = p1*(b /p1^k) // N;
f[p_, v_] := g (p - c) (v*b /p^k);

Plot[{f[p, a/(b/p^k)]}, {p, c, 10}]

-
Can there possibly be a shorter minimal example? It could make the analysis of your code easier. – Szczypawka Apr 25 '14 at 10:51
I created a simple example with a random function. plot1 works, but plot2 does not. – user12806 Apr 25 '14 at 11:15
You just need to change your function definition to: plot2[g_] := Plot[Evaluate@f[p, a[g]/(b/p^k), g], {p, c, 10}]; – ubpdqn Apr 25 '14 at 11:54
Concerning your simple example, I believe part of your problem is the a[g]/(b/p^k) evaluates to complex numbers. – m_goldberg Apr 25 '14 at 12:01
@ubpdqn: Thanks for the example that works. There is still some trouble with the original model above, but that might be unconnected. – user12806 Apr 25 '14 at 12:09