Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

NIntegrate and replacements

24 views
Skip to first unread message

S-Tat

unread,
Aug 18, 2012, 3:46:31 AM8/18/12
to
Hi everyone,

I have a simple question. There is my code, it integrates a function which is a solution of the equation:

bc[r_, x_] := r + x^2;
sol[r_] := Re[x /. FindRoot[bc[r, x] == 0, {x, 1.1 + I}]];
NIntegrate[sol[r], {r, 1, 8}]


Trying this code, I got these messages:

FindRoot::nlnum: "The function value {(0.21 +2.2\ I)+r}\\n is not a list of numbers with dimensions {1} at {epseff} = {1.1 +1.` I}"

ReplaceAll::reps: "{FindRoot[bc[r,epseff]==0,{epseff,1.1 +I}]}\\n is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing"

Sorry if it's too dumb but I cannot see why NIntegrate couldn't just use exact value for r every time. Isn't it how numerical integration is supposed to work?

Thank you

Bill Rowe

unread,
Aug 18, 2012, 8:37:18 PM8/18/12
to
On 8/18/12 at 3:47 AM, kravc...@gmail.com (S-Tat) wrote:

>I have a simple question. There is my code, it integrates a function
>which is a solution of the equation:

>bc[r_, x_] := r + x^2;
>sol[r_] := Re[x /. FindRoot[bc[r, x] == 0, {x, 1.1 + I}]];
>NIntegrate[sol[r], {r, 1, 8}]

Change the definition for sol to be

sol[r_?NumericQ]:=Re[x /. FindRoot[bc[r, x] == 0, {x, 1.1 + I}]]

THe problem with your code is Mathematica wants to evaluate
sol[r] before r is given a numeric value by NIntegrate causing
the error. Adding the test ?NumericQ prevents sol from returning
values when non-numeric arguments are supplied.


Bob Hanlon

unread,
Aug 18, 2012, 8:41:22 PM8/18/12
to
bc[r_, x_] := r + x^2;

"NIntegrate symbolically analyzes its input to transform oscillatory
and other integrands, subdivide piecewise functions, and select
optimal algorithms." Since sol uses numerical techniques it must be
restricted to numerical inputs.

sol[r_?NumericQ] :=
Re[x /. FindRoot[bc[r, x] == 0,
{x, 1.1` + I}]];

NIntegrate[sol[r], {r, 1, 8}]

1.28089*10^-18

sol2[r_] = Re[x /. Solve[bc[r, x] == 0, x]]

{Im[Sqrt[r]], -Im[Sqrt[r]]}

Integrate[sol2[r], {r, 1, 8}]

{0, 0}


Bob Hanlon


On Sat, Aug 18, 2012 at 3:47 AM, S-Tat <kravc...@gmail.com> wrote:
> Hi everyone,
>
> I have a simple question. There is my code, it integrates a function which is a solution of the equation:
>
> bc[r_, x_] := r + x^2;
> sol[r_] := Re[x /. FindRoot[bc[r, x] == 0, {x, 1.1 + I}]];
> NIntegrate[sol[r], {r, 1, 8}]
>
>
0 new messages