Re: FindRoot and parameters in NIntegrate
- To: mathgroup at smc.vnet.net
- Subject: [mg124508] Re: FindRoot and parameters in NIntegrate
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sun, 22 Jan 2012 07:21:53 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
On 1/21/12 at 5:18 AM, g.crlsn at gmail.com (gac) wrote: >I am using Mathematica 8 to solve for parameters in an integral and >an auxiliary equation. A prototypical problem is >FindRoot[{1 + a + b , NIntegrate[a + b x, {x, 0, 1}]}, {{a, 0}, {b, >0}}] >Mathematica reports the correct answer for the parameters a and b: >{a -> 1., b -> -2.} >But it also reports two error messages: There are several ways around this. Best would be: In[13]:= int = Integrate[a + b x, {x, 0, 1}]; FindRoot[{1 + a + b, int}, {{a, 0}, {b, 0}}] Out[14]= {a -> 1., b -> -2.} Not only will this eliminate the error messages, but the integral is only computed once. But, this does require that the integral be one Mathematica can solve symbolically. If that is not the case you could do: In[15]:= f[a_?NumericQ, b_?NumericQ] := Integrate[a + b x, {x, 0, 1}]; FindRoot[{1 + a + b, f[a, b]}, {{a, 0}, {b, 0}}] Out[16]= {a->1.,b->-2.} The error message results since NIntegrate tries to solve the integral before FindRoot supplies numeric values for a and b. This solves the problem since f[a,b] will remain unevaluated until a and b are given numeric values. But while this works as intended, it is much less efficient than the first approach since the integral will be repeatedly solved for each time FindRoot provides numeric values for a and b. This approach should only be used in those cases Mathematica cannot provide a symbolic solution to the integral. Finally, you could do: In[17]:= Quiet@ FindRoot[{1 + a + b, NIntegrate[a + b x, {x, 0, 1}]}, {{a, 0}, {b, 0}}] Out[17]= {a->1.,b->-2.} Which simply suppresses the error messages. This is suitable whenever you know the error messages are unimportant as is the case here.