Message Boards Message Boards

0
|
5012 Views
|
4 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Solving y'=y, y(0)=1

Why does the following keep happening to me. Using 10.0.1.0. This is with a clean start, quit the kernel, evaluate the notebook.

enter image description here

POSTED BY: David Arnold
4 Replies

David,

One of the best answers I have seen thus far.

Thanks.

POSTED BY: David Arnold
Posted 10 years ago

In this code sequence

In[1]:= Clear[y]

In[2]:= sol = DSolve[{y'[t] == y[t], y[0] == 1}, y[t], t]

Out[2]= {{y[t] -> E^t}}

In[3]:= y[t_] := y[t] /. First[sol]

In[4]:= y[x]

During evaluation of In[4]:= $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>

Out[4]= Hold[y[x]]

In[3] uses a SetDelayed. (=. ) This means that the kernel now has a definition for y[t_] ( meaning y[anything], but call it t ) for which the defined substitution is the rule on the right hand side. But DELAYED set means that the right hand side is not evaluated until the definition is applied. When In[4] is encountered, the engine has a definition for y[x] which tells it to first substitute x for t into y[t]/.First[sol], which gives y[x]/.First[sol], and then to evaluate that, which gives y[x]. It gives y[x] because the rule being applied defines y[t], not y[x]. But it does not stop there. When y[x] is returned the evaluation engine again looks to see if it has a rule for y[x]. It does, it still has the definition for y[anything]. The evaluation process in Mathematica works like that. It continues to evaluate until the result is such that no rule will apply. That's why the engine stops at a RecursionLimit.

In the code below

In[5]:= Clear[y]

In[6]:= y[t_] := Evaluate[y[t] /. First[sol]]

In[7]:= y[x]

Out[7]= E^x

The Evaluate on the right hand side forced the evaluation so that the definition for y[t] that is produced is really y[t_]:=E^t, since the right hand side was evaluated before the definition was made. Now, when In[7] is evaluated, the first evaluation finds that there is a rule in the kernel for y[anything] which takes it to E^anything. so y[x] becomes E^x. The engine finds it has no further rule for E^x, so it returns that. (If x=2 was previously executed, it would find it had a rule for x, and would return E^2.

Evaluate on the right hand side of SetDelayed is functionally equivalent to using Set rather than SetDelayed, as in the code below

In[8]:= Clear[y]

In[9]:= y[t_] = y[t] /. First[sol]

Out[9]= E^t

In[10]:= y[x]

Out[10]= E^x

Kind regards,

David

POSTED BY: David Keith

What does the phrase "otherwise the rule is executed only if y is called ..." mean? Thanks.

POSTED BY: David Arnold

This works,

In[33]:= Clear[y]
sol = DSolve[{y'[t] == y[t], y[0] == 1}, y[t], t]
Out[34]= {{y[t] -> E^t}}

In[35]:= y[t_] := Evaluate[y[t] /. First[sol]]

In[36]:= y[x]
Out[36]= E^x

otherwise the rule is executed only if y is called ...

POSTED BY: Udo Krause
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract