Re: More Anomalous Behaviour with Indexed Variables
- Subject: [mg3306] Re: [mg3256] More Anomalous Behaviour with Indexed Variables
- From: jpk at apex.mpe.FTA-Berlin.de (Jens-Peer Kuska)
- Date: 25 Feb 1996 16:03:08 -0600
- Approved: usenet@wri.com
- Distribution: local
- Newsgroups: wri.mathgroup
- Organization: Wolfram Research, Inc.
- Sender: daemon at wri.com
> 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} ]
>
> Thanks for your help.
>
> Mark James
Hi Mark,
at first
I think that it is really dangerous to use sutch side
effect, because there is *no* guarantie that NDSolve evaluate
the differential equations in the same order as given. NDSolve
has to do a lot of things bevore it can start with the numerical
solution.
at second
don't talk about efficient solutions, Mathematica is a interpreter
and will be allways 100 or more times slower than a compiled
code. You can still using Mathematica via MathLink for dawing and post processing.
You mix up in Your code assignet function values and indexing. f[1]=v^2 will assign
the value v^2 to the function f[z] for z=1, indexing f[[1]]=v^2 mean that the first component
of the vector f has the value v^2. How ever here is a working example
(*
PARAMETER IS: f[[1]]
*)
Clear[ f,calc ];
f={0.};
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},Compiled->False ]
{{dummy -> InterpolatingFunction[{0., 1.}, <>],
p -> InterpolatingFunction[{0., 1.}, <>]}}
Hope that helps
Jens