       Re: change the solution to a differential equation into a user defined function - question.nb (0/1)

• To: mathgroup at smc.vnet.net
• Subject: [mg15602] Re: change the solution to a differential equation into a user defined function - question.nb (0/1)
• From: Hartmut Wolf <hw at gsmail01.darmstadt.dsh.de>
• Date: Thu, 28 Jan 1999 04:23:31 -0500 (EST)
• Organization: debis Systemhaus
• References: <786u4u\$29b@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Dear Yong Cai

Yong Cai schrieb:
>
> I probably have to apologize for the awkward name for the subject.
>
> My question is
>
> In:=
> sol=DSolve[y'[x]==x,y[x],x]
> Out=
> \!\({{y[x] \[Rule] x\^2\/2 + C}}\)
>
> In:=
> FullForm[sol]
> Out//FullForm=
> List[List[Rule[y[x],Plus[Times[Rational[1,2],Power[x,2]],C]]]]
>
> After I type in the command to solve the differential equation, I am
> given a replacement rule of y[x], which can be applied as a rule when
> the argument inside y is x, nothing else.
>
> In:=
> y[x_]:=x^2/2+c
> In:=
> FullForm[y[x_]]
> Out//FullForm=
> Plus[c,Times[Rational[1,2],Power[Pattern[x,Blank[]],2]]]
>
> When I define the same function for y[x] by myself, it is evidently  a
> pattern where x can be taken in any value.
>
> Then comes the question: how can we use the solution given in a
> differential equation which itself is a function as a function for
> later use?

I can't understand:  ...which itself is a function...

the question: how can we use the solution given in a
differential equation -------------------------- as a function for
later use?

>
> It seems to be straightforward, but I have looked through the
> Mathematica Book and found no clue. Your help will be greatly
> appreciated.

The result of DSolve is a list of solutions for a list of Functions of a
list of simultanious differential equations, where each element is a
replacement rule. So just use that!

Let's take the examples of Stephen Wolfram's Book (in 3.5.11 of 2nd
Edition)

Remove[y]
sol = DSolve[y[x] y'[x] == 1, y, x] //FullForm

{{y -> (- Sqrt Sqrt[#1 + C]&)}, {y -> (Sqrt Sqrt[(#1 +
C]&)}}

so to use the result (i.e. in a Plot) just do

Unprotect[C];C = 4;
Plot[Evaluate[y[x] /. sol[] ], {x, 0, 20}];

--plot not shown--

or just to calculate a value

y[2 Pi} /. sol[]

Sqrt Sqrt[2 Pi + C]

for another example:

Remove[y,z]
solutions = DSolve[{y[x]==-z'[x], z[x]==-y'[x]}, {y,z}, x]

{{y -> (4 E^(-#1) C - E^#1 C&), z -> (4 E^(-#1) C + E^#1
C&)}}

Unprotect[C]; C=1;C=-1;
Through[{y,z}[x] ] /. solutions[]

{4 E^-x + E^x, 4 E^-x - E^x}

or

{y, z[1.]} /. solutions[]

{4/E + E, -1.24676}

I think that should normally do it.

Trying to follow your trace of reasoning, we can get the function you

Remove[y]
sol = DSolve[y'[x]==x, y[x], x] //FullForm

List[List[Rule[y[x],Plus[Times[Rational[1,2],Power[x,2]],C]]]]

To define a function that corresponds to that solution

yy[x_] := x^2/2 + c

DownValues[yy] //FullForm

List[RuleDelayed[HoldPattern[yy[Pattern[x,Blank[]]]],
Plus[Times[Power[x,2],Power[2,-1]],c]]]

That's it, now we know what to do to transform the solution to the
function

sol[[1,1]]/. y[x]->HoldPattern[y[Pattern[x,Blank[]]]]  //FullForm

Rule[HoldPattern[y[Pattern[x,Blank[]]]],
Plus[Times[Rational[1,2],Power[x,2]],C]]

RuleDelayed @@ %  //FullForm

RuleDelayed[HoldPattern[y[Pattern[x,Blank[]]]],
Plus[Times[Rational[1,2],Power[x,2]],C]]

DownValues[y] = %;

?y

Global`y

y[x_] := x^2/2 + C

so to use it:

Unprotect[C];C = -1;

Plot[y[r], {r,-2, 2}]

--plot not shown here--

```

• Prev by Date: how to use Findminimum with a function defined numerically through a parameter
• Next by Date: Re: speed
• Previous by thread: RE: change the solution to a differential equation into a user defined function - question.nb (0/1)
• Next by thread: Tutorials on Package and On-Line Documentation Development