Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.1K Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Why Do I Need the Evaluate and ReplaceAll (/.) Command ?

Posted 10 years ago
m = 1
k = 1
s = NDSolve[{m*x''[t] == -k*x[t], x[0] == 0.001, x'[0] == 0.001}, 
  x, {t, 0, 30}]
Plot[Evaluate[x[t] /. s], {t, 0, 30}, PlotRange -> All]

Can someone explain why I need the Evaluate and the /. (ReplaceAll) command in the Plot command above ?

Regards ;-)

POSTED BY: john massucci
2 Replies

Actually you do not need Evaluate in this case. This works:

 Plot[ x[t] /. s, {t, 0, 30}, PlotRange -> All]

Sometimes you do need Evaluate with Plot since Plot has HoldAll attribute. This question/answer Why do I have to put Evaluate[] here has more details.

POSTED BY: Nasser M. Abbasi
Posted 10 years ago

I believe that is not ReplaceAll.. more likely it extracts x[t] from s, just like when you extract the solution for a parameter when you use Solve[].

I am not sure why would you need Evaluate[] there. The Plot[] function seems to work well without it. In general, I have used it only once or twice, when I had an expression as an argument. It is a safe bet to use it in these kind of cases, but I would like to hear from someone with good lexical scoping knowledge in Mathematica, what happens when we use Evaluate in this case and when we don't? Does the program do something unnecessary in the first case?

The plot can also be generated with:

Plot[ s[[1,1,2]][t], {t, 0, 30}, PlotRange -> All]
POSTED BY: Sandu Ursu
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard