       Re: function evaluations with NDSolve

• To: mathgroup at smc.vnet.net
• Subject: [mg36714] Re: function evaluations with NDSolve
• From: Eckhard.Hennig at infineon.com (Eckhard Hennig)
• Date: Fri, 20 Sep 2002 04:16:30 -0400 (EDT)
• References: <am95cj\$k1k\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```"Higinio Ramos" <higra at usal.es> wrote in message news:<am95cj\$k1k\$1 at smc.vnet.net>...
> I pretend to know how many times the function f has to be evaluated when
> using NDSolve to solve a differential equation.
> I've tried :
>
> cont=0;
> f[x_]:=(cont++;x^2);
> NDSolve[ {y''[x]+y[x]==f[x],y==1,y'==0},y,{x,0,6}]
>
> I obtain the solution of the ODE but cont returns 1 instead the number
> of funtion evaluations.
> How can I obtain it?
>
> Thanks.
> Higinio Ramos

Hi Higinio,

you need to make sure that the function f[x] is evaluated only when it
gets a numeric argument. Otherwise, Mathematica expands f[x]
symbolically to the expression x^2 before NDSolve is called.

Writing your code as follows will solve your problem:

cont=0;
f[x_?NumericQ]:=(cont++;x^2);
NDSolve[ {y''[x]+y[x]==f[x],y==1,y'==0},y,{x,0,6}]

Best regards,

Eckhard Hennig

```

• Prev by Date: Re: the meaning of a exponential plot in the negative region
• Next by Date: Re: Front end proble in Linux
• Previous by thread: function evaluations with NDSolve
• Next by thread: Inv.Interpol.Function