Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to run the following command in Mathematica:

FindRoot[NIntegrate[D[f[x], x] / Sqrt[1 - x^2], {x, 0, 1}] - d, {a, 245}]

As you might expect, a is buried within f[x] and 245 is a reasonable initial guess at its value. As you may also have guessed, the above integral is an expression for d in terms of a but I can't do the integral in closed form so I can't invert the expression to find a in terms of d.

So, I wish to numerically integrate my integral expression for d but that just give me the d that corresponds to a given value of a. I want to go the other direction, find the a value that corresponds to a given d. That's what I'm using FindRoot for (kudos if you're staying with me so far, it's taken me days to get to this point).

So here's the problem: NIntegrate gets upset if anything in the integrand is an undefined variable so every variable in the above integrand is defined except a (because I don't know it yet). However, FindRoot does know it. What I mean is, FindRoot is presumably running in an iterative loop and in every iteration it is working with a guess at a so somewhere in the bowels of the code, a always has a definite value. If I could somehow get NIntegrate to know this, I think I can get the above command to work (which would be a nice example of a powerful one line Mathematica operation). Any tips or do you think I'm trying to do something impossible?

Thanks,

Dessie.

share|improve this question

migrated from stackoverflow.com Sep 29 '12 at 20:43

This question came from our site for professional and enthusiast programmers.

    
In general you can deal with the first problem by defining int[a_?NumericQ, d_?NumericQ] = NIntegrate[...]. This way FindRoot will evaluate its argument only when you supply numerical values. –  b.gatessucks Sep 27 '12 at 22:09

1 Answer 1

Maybe the solution is to insert the FindRoot option Evaluated -> False. In this way, you ensure that the integral is calculated before the FindRoot[expr] is evaluated. For example:

Block[{f, d = Pi},
 f[x_] := a^2 Sin[x];
 FindRoot[
  NIntegrate[D[f[x], x]/Sqrt[1 - x^2], {x, 0, 1}] - d, {a, 245},
  Evaluated -> False
 ]
]
(* -> {a -> 1.6166952922140634`} *)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.