From: Robert Buchanan on
Hello,

I have a system of differential equations I am trying to solve
numerically. On the right-hand side of the ODEs I must numerically
integrate a quantity which is spatially distributed. I tried a simple
and direct approach of using NIntegrate[] on the RHS, but I am getting
warning messages and I think there must be a better way of coding this
in Mathematica. Rather than posting my complete system of ODEs I will
give a simpler, 1-dimensional example which produces the same warning
message. Suggestions on handling this issue are appreciated.

Thanks,
Bob Buchanan

NDSolve[{y[0] == 1.0,
y'[t] == t y[t] + NIntegrate[Cos[r y[t]] Exp[r^2], {r, 0, 1}]},
y[t], {t, 0, 1}]

From: DrMajorBob on
g[r_] = Integrate[Cos[r y[t]] Exp[r^2], {r, 0, 1}]

1/4 E^(y[t]^2/4) Sqrt[\[Pi]] (-I Erf[I + y[t]/2] +
Erfi[1 + 1/2 I y[t]])

NDSolve[{y[0] == 1.0, y'[t] == t y[t] + g@r}, y[t], {t, 0, 1}]

{{y[t]->InterpolatingFunction[{{0.,1.}},<>][t]}}

Bobby

On Wed, 16 Dec 2009 05:17:18 -0600, Robert Buchanan
<j.robert.buchanan(a)comcast.net> wrote:

> Hello,
>
> I have a system of differential equations I am trying to solve
> numerically. On the right-hand side of the ODEs I must numerically
> integrate a quantity which is spatially distributed. I tried a simple
> and direct approach of using NIntegrate[] on the RHS, but I am getting
> warning messages and I think there must be a better way of coding this
> in Mathematica. Rather than posting my complete system of ODEs I will
> give a simpler, 1-dimensional example which produces the same warning
> message. Suggestions on handling this issue are appreciated.
>
> Thanks,
> Bob Buchanan
>
> NDSolve[{y[0] == 1.0,
> y'[t] == t y[t] + NIntegrate[Cos[r y[t]] Exp[r^2], {r, 0, 1}]},
> y[t], {t, 0, 1}]
>


--
DrMajorBob(a)yahoo.com

From: Bob Hanlon on

For comparison purposes, calculate the symbolic integral

int = Integrate[Cos[r y[t]] Exp[r^2], {r, 0, 1}]

(1/4)*Sqrt[Pi]*E^(y[t]^2/4)*(Erfi[1 + (1/2)*I*y[t]] -
I*Erf[y[t]/2 + I])

sol1 = y[t] /. NDSolve[
{y[0] == 1.0, y'[t] == t y[t] + int},
y[t], {t, 0, 1}][[1]];

Restrict the numerical integration to evaluate only for numerical arguments

f[a_?NumericQ, t_?NumericQ] :=
NIntegrate[Cos[r a] Exp[r^2], {r, 0, 1}]

sol2 = y[t] /. NDSolve[
{y[0] == 1.0, y'[t] == t y[t] + f[y[t], t]},
y[t], {t, 0, 1}][[1]];

Comparing the results

GraphicsColumn[
Plot[#, {t, 0, 1}, ImageSize -> 400] & /@
{sol1, sol2, {sol1, sol2}}]


Bob Hanlon

---- Robert Buchanan <j.robert.buchanan(a)comcast.net> wrote:

=============
Hello,

I have a system of differential equations I am trying to solve
numerically. On the right-hand side of the ODEs I must numerically
integrate a quantity which is spatially distributed. I tried a simple
and direct approach of using NIntegrate[] on the RHS, but I am getting
warning messages and I think there must be a better way of coding this
in Mathematica. Rather than posting my complete system of ODEs I will
give a simpler, 1-dimensional example which produces the same warning
message. Suggestions on handling this issue are appreciated.

Thanks,
Bob Buchanan

NDSolve[{y[0] == 1.0,
y'[t] == t y[t] + NIntegrate[Cos[r y[t]] Exp[r^2], {r, 0, 1}]},
y[t], {t, 0, 1}]


From: dh on


Hi Robert,

Mathematica first tries simplify the integrand analytically and fails because it

can not do the integral without explicite value for y[t]. To prevent

this, declare the integral to be a function of a numeric argument:



fun[yt_?NumericQ] := NIntegrate[Cos[r yt] Exp[r^2], {r, 0, 1}]

NDSolve[{y[0] == 1.0, y'[t] == t y[t] + fun[y[t]]}, y[t], {t, 0, 1}]



Daniel



Robert Buchanan wrote:

> Hello,

>

> I have a system of differential equations I am trying to solve

> numerically. On the right-hand side of the ODEs I must numerically

> integrate a quantity which is spatially distributed. I tried a simple

> and direct approach of using NIntegrate[] on the RHS, but I am getting

> warning messages and I think there must be a better way of coding this

> in Mathematica. Rather than posting my complete system of ODEs I will

> give a simpler, 1-dimensional example which produces the same warning

> message. Suggestions on handling this issue are appreciated.

>

> Thanks,

> Bob Buchanan

>

> NDSolve[{y[0] == 1.0,

> y'[t] == t y[t] + NIntegrate[Cos[r y[t]] Exp[r^2], {r, 0, 1}]},

> y[t], {t, 0, 1}]

>



From: Albert Retey on
Hi,

> I have a system of differential equations I am trying to solve
> numerically. On the right-hand side of the ODEs I must numerically
> integrate a quantity which is spatially distributed. I tried a simple
> and direct approach of using NIntegrate[] on the RHS, but I am getting
> warning messages and I think there must be a better way of coding this
> in Mathematica. Rather than posting my complete system of ODEs I will
> give a simpler, 1-dimensional example which produces the same warning
> message. Suggestions on handling this issue are appreciated.
>
> Thanks,
> Bob Buchanan
>
> NDSolve[{y[0] == 1.0,
> y'[t] == t y[t] + NIntegrate[Cos[r y[t]] Exp[r^2], {r, 0, 1}]},
> y[t], {t, 0, 1}]
>

I think the warning is only issued because Mathematica first tries to
evaluate the RHS in symbolic form. It will give warnings, not succeed
and then proceed with the numeric evaluation at each time step.

This will work without warning:

int[y_?NumericQ] := NIntegrate[Cos[r y] Exp[r^2], {r, 0, 1}]

NDSolve[{
y[0] == 1.0,
y'[t] == t y[t] + int[y[t]]
}, y[t], {t, 0, 1}
]

hth,

albert