       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:=
feq =
Table[(f[i])'[x] + p[i] f[i][x] + q[i],
{i,1,2}]
Out=
{q + p f[x] + (f)'[x],
q + p f[x] + (f)'[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:=
Table[DSolve[feq[[i]] == 0, f[i][x], x,
DSolveConstants -> cnst1], {i,1,2}]
Out=
cnst1   q
{{{f[x] -> -------- - ----}},
x p    p
E

cnst1   q
{{f[x] -> -------- - ----}}}
x p    p
E
......
What I'd really like is for something like the following to work. That way, my
various integration constants, for example cnst3[i], would have the same
indexing system as my equations. How can I do this?
In:=
Table[DSolve[  feq[[i]] == 0, f[i][x], x,
DSolveConstants -> cnst[i]   ], {i,1,2}]
DSolve::csym:
Value of option DSolveConstants -> cnst
must be a symbol.......
Out=
.............
Is there a way to immediately substitute C[i] for C\$1, say, and not get
lost in the indexing? I eventually want to do this with systems of 12 or more
equations.
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:=
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=

==========================================================================
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:= feq = Table[(f[i])'[x] + p[i] f[i][x] + q[i], {i,1,2}]

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

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

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

q    cnst
>    {{f[x] -> -(----) + ----------}}}
p      x p
E

Here is another possibility.

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

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

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

q    cnst
>    {{f[x] -> -(----) + ----------}}}
p      x p
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:= Block[{i},
Table[DSolve[  feq[[i]] == 0, f[i][x], x,
DSolveConstants -> ConstantFixer], {i,1,2}] ]

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

q    cnst
>    {{f[x] -> -(----) + ----------}}}
p      x p
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