# function includes integral in which limits are solutions to separate equations. Want to plot function

Here is my code.

CMB[a_?NumericQ, lambda_?NumericQ] :=
FindRoot[ns[i, lambda, a] - .96 == 0, {i, 15}]

END[a_?NumericQ, lambda_?NumericQ] :=
FindRoot[eps[i, lambda, a] - 1 == 0, {i, 1}]

Ne[lambda_?NumericQ, a_?NumericQ] =
NIntegrate[
V[i, lambda, a]/D[V[i, lambda, a], i], {i, Evaluate[END[a, lambda]],
Evaluate[CMB[a, lambda]]}]


I am trying to get Mathematica to first evaluate the integral bounds, and then evaluate the integral at those bounds. Is there any way to make it do that?

• You are encouraged to post a complete, self-contained example rather than leaving mystery functions ns, eps, etc. Jul 11, 2016 at 15:20
• First, you probably want := instead of = in your definition of Ne? How about using a scoping construct like Module where you first compute the limits as local variables and then pass those to the integration? Jul 11, 2016 at 15:20

I cannot evaluate your code due to missing functions but here is a shot in the dark:

CMB[a_?NumericQ, lambda_?NumericQ] := FindRoot[ns[i, lambda, a] - .96 == 0, {i, 15}]

END[a_?NumericQ, lambda_?NumericQ] := FindRoot[eps[i, lambda, a] - 1 == 0, {i, 1}]

Ne[lambda_?NumericQ, a_?NumericQ] :=
With[{min = END[a, lambda], max = CMB[a, lambda]},
NIntegrate[V[i, lambda, a]/D[V[i, lambda, a], i], {i, min, max}]
]


Evaluate must be the explicit head of an argument for it to have effect. See:

• I tried your code, and while it compiled without errors, when I tried to find what Ne was for a given lambda and a, it didn't compute. Also, when i took the colon out of ':=' in the function declaration, I got the error "i = END[a,lambda] is not a valid limit of integration. " Do you have any idea why this would be? Thanks for your help. Jul 11, 2016 at 15:43
• @Seamus Providing a complete example will save us both time. Glancing again at this FindRoot return results as Rule expressions rather than bare values, so you probably need something like i /. FindRoot[ns[i, lambda, a] - .96 == 0, {i, 15}] in the definitions. I shall not attempt to debug this further without complete code. Jul 11, 2016 at 15:51
• By complete code, may I assume you are referring to the equations for V, ns, and eps? If so, they depend on an interpolating function. Is it possible to post a file of such a function here? Otherwise, the complete code will not be much use Jul 11, 2016 at 18:35
• Running this code, CMB[a_?NumericQ, lambda_?NumericQ] = i /. FindRoot[ns[i, lambda, a] - .96 == 0, {i, 15}] Jul 11, 2016 at 18:46
• Mathematica returned these errors FindRoot::nlnum: The function value {0.04 +(2. ([Piecewise] <<1>> ))/[Piecewise] <<1>> -3. (Piecewise[{{<<2>>},{<<2>>},{<<2>>},{<<2>>},{<<2>>},{<<2>>}},0.]/Piecewise[{<<6>>},0.])^2.} is not a list of numbers with dimensions {1} at {i} = {15.}. >>ReplaceAll::reps: {FindRoot[ns[i,lambda,a]-0.96==0,{i,15}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >> Do you have any idea why the program is seeing the starting value for FindRoot as a list? Jul 11, 2016 at 18:51