Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1996
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1996

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

Search the Archive

Re: More Anomalous Behaviour with Indexed Variables

  • To: mathgroup at smc.vnet.net
  • Subject: [mg3297] Re: More Anomalous Behaviour with Indexed Variables
  • From: withoff (David Withoff)
  • Date: Sat, 24 Feb 1996 03:21:00 -0500
  • Organization: Wolfram Research, Inc.
  • Sender: owner-wri-mathgroup at wolfram.com

In article <4gbvfr$mqo at dragonfly.wolfram.com> Mark James  
<mrj at cs.usyd.edu.au> writes:
> A recent article on this group discussed the frequent failure
> of FindRoot when the search variable is indexed (e.g. x[1]).
> I have been having similar problems.
> 
> Now this has cropped up again with NDSolve.  If you
> get a chance, run the following two groups of expressions
> through your version.  Why does the second version fail?
> Is it because NDSolve no longer evaluates the dummy equation
> first? I am using 2.2.1.
> 
> (*
>     PARAMETER IS: f
> *)
>     Clear[ f ];
>     calc[ v_Real ] := ( f = v^2; 0 );
>     NDSolve[ {dummy'[t] == calc[p[t]], dummy[0]==0,
>               p'[t] == -p[t] + f, p[0]==0 }, {dummy,p}, {t,0,1} ]
> 
> (*
>     PARAMETER IS: f[1]
> *)
>     Clear[ f ];
>     calc[ v_Real ] := ( f[1] = v^2; 0 );
>     NDSolve[ {dummy'[t] == calc[p[t]], dummy[0]==0,
>               p'[t] == -p[t] + f[1], p[0]==0 }, {dummy,p}, {t,0,1} ]
> 
> Am I doing anything wrong, or is it a bug?  If it is a bug:
> Is there a workaround?  Will it be fixed in 3.0?
> Is this related to similar problems with indexed
> variables in FindRoot?
> 
> Thanks for your help.
> 
> Mark James                                   | EMAIL :  
mrj at cs.usyd.edu.au |
> Basser Department of Computer Science, F09   | PHONE : +61-2-351-3423      
|
> The University of Sydney NSW 2006 AUSTRALIA  | FAX   : +61-2-351-3838      
|

=====================================================================

This problem comes up because, in the process of solving for
the derivatives, NDSolve converts f[1] to f[1.0], after
which it won't find the rule for f[1].  You may be able to get
what you want by using

        calc[ v_Real ] := ( f[1.0] = v^2; 0 )

to define your side-effect rule, or using SetAttributes[f, NHoldAll]
to prevent NDSolve from converting f[1] to f[1.0].

A very important point, though, is that you are really out on a limb
here using a side effect of the computation of one derivative to get
the value of the other, since your results will depend on details of
the way that the numerical differential equation solver works.  The
numerical differential equation solver is free to compute values of the
derivatives in whatever order it chooses, or to compute them in parallel.
There is no guarantee that the side effect from computation of one of
the derivatives will be available when the other is computed.  Initially,
since f[1] doesn't have a value, the calculation will (quite correctly)
fail at the very first step unless dummy'[t] is computed before f'[t] is
computed.  If the calculation gets beyond the first step, and at some
point the numerical differential equation solver chooses to evaluate
f'[t] before it evaluates dummy'[t], then the computation of f'[t] will
used the side effect from the previous step, which may not be at all
what you want.

I am very uneasy about using side-effects in this way, and would think
long and hard to try to come up with some other way to do things.

Anyway, getting back to your question about indexed variables, this
behavior comes up because NDSolve converts f[1] to f[1.0] while
solving for the derivatives.  NDSolve doesn't absolutely need to do this,
and in the next version of Mathematica it doesn't, but computing
numerical values is a big part of what NDSolve does, so you'll have to
decide for yourself is this is a bug or not.  Once you understand it,
and what to do about it, it doesn't really matter.

I didn't see the articles that you referred to about FindRoot, and
don't know of any "frequent failures" in this regard, so I don't know
if that is at all related.

Dave Withoff
Research and Development
Wolfram Research


==== [MESSAGE SEPARATOR] ====


  • Prev by Date: Re: Re: Student v. Full version of Mathematica
  • Next by Date: Re: More Anomalous Behaviour with Indexed Variables
  • Previous by thread: More Anomalous Behaviour with Indexed Variables
  • Next by thread: Re: More Anomalous Behaviour with Indexed Variables