MathGroup Archive 1996

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

Search the Archive

RE: DSolveConstants question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg3751] RE: DSolveConstants question
  • From: rich_klopp at qm.sri.com (Richard W. Klopp)
  • Date: Sun, 14 Apr 1996 03:02:44 -0400
  • Organization: SRI International
  • Sender: owner-wri-mathgroup at wolfram.com

I previously posted the following on comp.soft-sys.math.mathematica and
received but one response, for the (Module[{C}, C] &) syntax question. I
then wrote to Wolfram Support and received a couple of answers to my more
central question. I am sharing these with the group with the hope that
they help someone else, too.
---------------------------------------------
 In solving a system of differential equations, I run into the problem of
Mathematica using the same constant of integration for all the equations,
which is an unwanted behavior in my case, but I can't figure out an elegant
workaround. I'd like my integration constants to be indexed the same as my
equations. See the following example.

Define a system of 2 equations:
In[1]:=
feq =
   Table[(f[i])'[x] + p[i] f[i][x] + q[i],
   {i,1,2}]
Out[1]=
{q[1] + p[1] f[1][x] + (f[1])'[x], 
 q[2] + p[2] f[2][x] + (f[2])'[x]}
Solve the equations with the requirement that any constants of integration be
named cnst1[j] and note that, as (unhappily) expected, Mathematica restarts
the index j for cnst[j] with each invocation of DSolve.
In[2]:=
Table[DSolve[feq[[i]] == 0, f[i][x], x,
   DSolveConstants -> cnst1], {i,1,2}]
Out[2]=
              cnst1[1]   q[1]
{{{f[1][x] -> -------- - ----}}, 
               x p[1]    p[1]
              E
 
               cnst1[1]   q[2]
  {{f[2][x] -> -------- - ----}}}
                x p[2]    p[2]
               E
......
What I'd really like is for something like the following to work. That way, my
various integration constants, for example cnst3[i][1], would have the same
indexing system as my equations. How can I do this?
In[4]:=
Table[DSolve[  feq[[i]] == 0, f[i][x], x,
   DSolveConstants -> cnst[i]   ], {i,1,2}]
DSolve::csym: 
   Value of option DSolveConstants -> cnst[1]
     must be a symbol.......
Out[4]=
.............
Is there a way to immediately substitute C[i][1] for C$1[1], say, and not get
lost in the indexing? I eventually want to do this with systems of 12 or more
equations.
Thanks for your help!
Rich Klopp
SRI International
415-859-6482

PS
**I've received an answer to the following:  just get rid of the parenthesis
and ampersand pure function syntax**
THE BOOK, p. 783, says that I can use the option
DSolveConstants -> (Module[{C}, C]&)
to have Mathematica not restart the index, but this does not seem to work.
Unfortunately, the book has no DSolveConstants examples to show how I've
screwed up the systax. Can someone set me straight? I also guess that even if
this did work, it would not be what I want because if there were more than one
constant per equation, then the ith equation would have the nth and (n+1)th
constants, with i != n.
In[3]:=
Table[DSolve[feq[[i]] == 0, f[i][x], x,
   DSolveConstants ->
   (Module[{cnst2}, cnst2]&)], {i,1,2}]
DSolve::csym: 
   Value of option DSolveConstants -> 
    Module[{cnst2}, cnst2] &  must be a symbol.......
Out[3]=

==========================================================================
The following is from David Withoff at Wolfram:
---------------------------------------------------
I'm not entirely sure that I understand what you want to do, but hopefully
the guesses below will be close enough to what you want to be useful.

Here is one possibility.

In[1]:= feq = Table[(f[i])'[x] + p[i] f[i][x] + q[i], {i,1,2}]

Out[1]= {q[1] + p[1] f[1][x] + (f[1])'[x], q[2] + p[2] f[2][x] + (f[2])'[x]}

In[2]:= Table[Module[{c}, DSolve[feq[[i]] == 0, f[i][x], x,
            DSolveConstants -> c] /. c -> cnst[i]], {i, 1, 2}]

                        q[1]    cnst[1][1]
Out[2]= {{{f[1][x] -> -(----) + ----------}}, 
                        p[1]      x p[1]
                                 E
 
                    q[2]    cnst[2][1]
>    {{f[2][x] -> -(----) + ----------}}}
                    p[2]      x p[2]
                             E

Here is another possibility.

In[3]:= ConstantFixer[p_] := cnst[i][p]

In[4]:= Table[DSolve[  feq[[i]] == 0, f[i][x], x,
            DSolveConstants -> ConstantFixer], {i,1,2}]

                        q[1]    cnst[1][1]
Out[4]= {{{f[1][x] -> -(----) + ----------}}, 
                        p[1]      x p[1]
                                 E
 
                    q[2]    cnst[2][1]
>    {{f[2][x] -> -(----) + ----------}}}
                    p[2]      x p[2]
                             E

This second example has the awkward feature of requiring that the
free variable i in the definition of ConstantFixer have the same
meaning as the local variable i in Table[..., {i, 1, 2}].  In
other words, this method requires that i doesn't have a value.
You can protect yourself from a global value for i using Block:

In[5]:= Block[{i},
          Table[DSolve[  feq[[i]] == 0, f[i][x], x,
              DSolveConstants -> ConstantFixer], {i,1,2}] ]

                        q[1]    cnst[1][1]
Out[5]= {{{f[1][x] -> -(----) + ----------}}, 
                        p[1]      x p[1]
                                 E
 
                    q[2]    cnst[2][1]
>    {{f[2][x] -> -(----) + ----------}}}
                    p[2]      x p[2]
                             E

which will work even if i has a global value.

The strategy here is to do whatever you have to do to sneak the
value of the DSolveConstants option past the argument checker in
DSolve, which will only allow the value of the DSolveConstants
option to be a symbol.  Except in contrived examples the rest of
DSolve doesn't care what the value of the DSolveConstants option
is, so if you can get the value past the code that checks arguments,
everything will work fine

I believe that the argument checker in DSolve has already been changed
for the next release of Mathematica such that the example you mentioned
will work.

Dave Withoff
Research and Development
Wolfram Research

-- 
Rich Klopp
rich_klopp at qm.sri.com

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


  • Prev by Date: Re: patterns
  • Next by Date: Linear Regression
  • Previous by thread: Re: DSolveConstants question
  • Next by thread: complex roots