# Passing a Piecewise function of NIntegrate through NIntegrate?

I am trying to pass a piecewise numerical integral (function1) through another numerical integral (function2). When I try this, however, I receive an error that says "t = T is not a valid limit of integration." So, it seems that function1 is not "receiving" the values that are being passed to it from function2. Is there a solution to this problem? Any help would be greatly appreciated!

Alex

a = 0.03; b = d = 1; c = 0.5; K = HMax = 2000; v = 10^(-7);

function1[g_, t0_] := Block[{HandX1, h7, HandX2, h8, B3},

HandX1 = DSolve[{H'[t] == -a*H[t], H == HMax,
X'[t] == X[t]*b*H[t]/HMax - X[t]*d*X[t]/K, X == K}, {H[t],
X[t]}, t]; h7 = HandX1[][][];

B3[u_?NumericQ] := NIntegrate[h7*t, {t, t0, u}, Method -> {Automatic, "SymbolicProcessing" -> 0}];

HandX2 = DSolve[{H'[u] == -a*H[u], H == HMax,
X'[u] == X[u]*b*H[u]/HMax - X[u]*d*X[u]/K, X == K}, {H[u],
X[u]}, u]; h8 = HandX2[][][];

Piecewise[{{0, g < 0.2}, {2/(1 + NIntegrate[Exp[-B3[u]]*h8/K, {u, t0, 5000},Method -> {Automatic, "SymbolicProcessing" -> 0}]), g >= 0.2}}]]

function2[g_] :=Block[{},1 - Exp[-v*
NIntegrate[function1[g, T], {T, 0, 500},
Method -> {Automatic, "SymbolicProcessing" -> 0}]]]

function2[0.4]


There are several messages when using DSolve[]

In:= DSolve[{H'[u] == -a*H[u], H == HMax,
X'[u] == X[u]*b*H[u]/HMax - X[u]*d*X[u]/K, X == K}, {H[u],
X[u]}, u]

During evaluation of In:= Solve::incnst: Inconsistent or redundant transcendental equation. After reduction, the bad equation is -2000+Subscript[\[ConstantC], 1] == 0.

During evaluation of In:= Solve::incnst: Inconsistent or redundant transcendental equation. After reduction, the bad equation is -2000+Subscript[\[ConstantC], 1] == 0.

During evaluation of In:= Solve::incnst: Inconsistent or redundant transcendental equation. After reduction, the bad equation is -2000+Log[E^Subscript[\[ConstantC], 1]] == 0.

During evaluation of In:= General::stop: Further output of Solve::incnst will be suppressed during this calculation.

During evaluation of In:= Solve::ifun: Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information.

Out= {{H[u] -> 2000 E^(-3 u/100),
X[u] -> (6000 E^(100/3 - 100/3 E^(-3 u/100)))/(
3 + 100 E^(100/3) ExpIntegralEi[-(100/3)] -
100 E^(100/3) ExpIntegralEi[-(100/3) E^(-3 u/100)])}}


If all these acceptable, then we can use code

a = 3/100; b = d = 1; c = 1/2; K = HMax = 2000; v = 10^(-7);

function1[g_, t0_] :=
Module[{HandX1, h7, HandX2, h8, B3},
HandX1 = DSolve[{H'[t] == -a*H[t], H == HMax,
X'[t] == X[t]*b*H[t]/HMax - X[t]*d*X[t]/K, X == K}, {H[t],
X[t]}, t] // Quiet; h7 = HandX1[][][] // Quiet;
B3[u_?NumericQ] :=
NIntegrate[h7*t, {t, t0, u},
Method -> {Automatic, "SymbolicProcessing" -> 0}];
HandX2 =
DSolve[{H'[u] == -a*H[u], H == HMax,
X'[u] == X[u]*b*H[u]/HMax - X[u]*d*X[u]/K, X == K}, {H[u],
X[u]}, u] // Quiet; h8 = HandX2[][][] // Quiet;
f1[t1_?NumericQ] :=
Piecewise[{{0,
g < 0.2}, {2/(1 +
NIntegrate[Exp[-B3[u]]*h8/K, {u, t1, 5000},
Method -> {Automatic, "SymbolicProcessing" -> 0}]),
g >= 0.2}}]; f1[t0]]
function2[g_?NumericQ] :=
Module[{},
f2 = 1 - Exp[-v*
NIntegrate[function1[g, T], {T, 0, 500},
Method -> {Automatic, "SymbolicProcessing" -> 0}]]; f2]


Check how it works

function2[0.4]

(*Out[]= 0.0000999949*)