Re:More Anomalous Behaviour with Indexed Variables
- Subject: [mg3307] Re:[mg3256] More Anomalous Behaviour with Indexed Variables
- From: fransm at win.tue.nl (Frans Martens)
- Date: 25 Feb 1996 14:42:02 -0600
- Approved: usenet@wri.com
- Distribution: local
- Newsgroups: wri.mathgroup
- Organization: Wolfram Research, Inc.
- Sender: daemon at wri.com
Begin forwarded message: >From: Mark James <mrj at cs.usyd.edu.au> >To: mathgroup at smc.vnet.net >Organization: The University of Sydney >Subject: [mg3256] More Anomalous Behaviour with Indexed Variables 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} ] ===== FURTHER EXPLANATION: The second differential equation in the first NDSolve call above depends of a parameter f that is a function of p. This parameter is calculated as a side effect of integrating the first differential equation. (My actual equation set, is 1000 times more complicated than this, and I really want to use the side effect method to efficiently calculate a number of parameters each integration step that would otherwise each be calculated many times per step.) I have used this method for a year without problems. But changing the parameter to f[1] does not work. (I now need to use indexed parameters in my real system. I have managed to get this real system to work if I substitute every indexed variable by a non-indexed one: e.g. s[1] -> s1 .) 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 | 8888888888888888888888888888888*8888888888888888888888888888888888888 The second instruction with NDSolve did not work properly because the expression f[1] in the equations is replaced by f[1.]. Solution are: (* SOLUTION 1, PARAMETER IS f[1] *) Clear[ f ]; f[a_Real] := f[Round[a]]; 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} ] (* SOLUTION 2, 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} ] Surprisingly, the idea to give the attribute NProtectedAll to the variable f does not work. Frans Martens