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[1]:=
> sol=DSolve[y'[x]==x,y[x],x]
> Out[1]=
> \!\({{y[x] \[Rule] x\^2\/2 + C[1]}}\)
>
> In[2]:=
> FullForm[sol]
> Out[2]//FullForm=
> List[List[Rule[y[x],Plus[Times[Rational[1,2],Power[x,2]],C[1]]]]]
>
> 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[3]:=
> y[x_]:=x^2/2+c[1]
> In[4]:=
> FullForm[y[x_]]
> Out[4]//FullForm=
> Plus[c[1],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...
....but to answer to
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[2] Sqrt[#1 + C[1]]&)}, {y -> (Sqrt[2] Sqrt[(#1 +
C[1]]&)}}
so to use the result (i.e. in a Plot) just do
Unprotect[C];C[1] = 4;
Plot[Evaluate[y[x] /. sol[[1]] ], {x, 0, 20}];
--plot not shown--
or just to calculate a value
y[2 Pi} /. sol[[2]]
Sqrt[2] Sqrt[2 Pi + C[1]]
for another example:
Remove[y,z]
solutions = DSolve[{y[x]==-z'[x], z[x]==-y'[x]}, {y,z}, x]
{{y -> (4 E^(-#1) C[1] - E^#1 C[2]&), z -> (4 E^(-#1) C[1] + E^#1
C[2]&)}}
Unprotect[C]; C[1]=1;C[2]=-1;
Through[{y,z}[x] ] /. solutions[[1]]
{4 E^-x + E^x, 4 E^-x - E^x}
or
{y[1], z[1.]} /. solutions[[1]]
{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
want. Let's use your example:
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[1]]]]]
To define a function that corresponds to that solution
yy[x_] := x^2/2 + c[1]
DownValues[yy] //FullForm
List[RuleDelayed[HoldPattern[yy[Pattern[x,Blank[]]]],
Plus[Times[Power[x,2],Power[2,-1]],c[1]]]]
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[1]]]
RuleDelayed @@ % //FullForm
RuleDelayed[HoldPattern[y[Pattern[x,Blank[]]]],
Plus[Times[Rational[1,2],Power[x,2]],C[1]]]
DownValues[y] = %;
?y
Global`y
y[x_] := x^2/2 + C[1]
so to use it:
Unprotect[C];C[1] = -1;
Plot[y[r], {r,-2, 2}]
--plot not shown here--