MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: function evaluations with NDSolve

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

"Higinio Ramos" <higra at> wrote in message news:<am95cj$k1k$1 at>...
> 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[0]==1,y'[0]==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:

NDSolve[ {y''[x]+y[x]==f[x],y[0]==1,y'[0]==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