0
$\begingroup$

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?

$\endgroup$
2
  • 1
    $\begingroup$ You are encouraged to post a complete, self-contained example rather than leaving mystery functions ns, eps, etc. $\endgroup$
    – Mr.Wizard
    Jul 11, 2016 at 15:20
  • $\begingroup$ 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? $\endgroup$
    – Lukas
    Jul 11, 2016 at 15:20

1 Answer 1

1
$\begingroup$

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:

$\endgroup$
6
  • $\begingroup$ 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. $\endgroup$ Jul 11, 2016 at 15:43
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Jul 11, 2016 at 15:51
  • $\begingroup$ 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 $\endgroup$ Jul 11, 2016 at 18:35
  • $\begingroup$ Running this code, CMB[a_?NumericQ, lambda_?NumericQ] = i /. FindRoot[ns[i, lambda, a] - .96 == 0, {i, 15}] $\endgroup$ Jul 11, 2016 at 18:46
  • $\begingroup$ 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? $\endgroup$ Jul 11, 2016 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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