Message Boards Message Boards

0
|
2044 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Creating a function for Series expansion and unable of plotting series

Posted 1 year ago

Hello,

Subscript[E, 1] = Series[E^x, {x, 0, 1}] // Normal
Subscript[E, 2] = Series[E^x, {x, 0, 2}] // Normal
Subscript[E, 3] = Series[E^x, {x, 0, 3}] // Normal
Subscript[E, 4] = Series[E^x, {x, 0, 4}] // Normal
Plot[{Subscript[E, 1], Subscript[E, 2], Subscript[E, 3], Subscript[E, 
  4]}, {x, 0, 5}, 
 PlotLegends -> {Subscript[E, 1], Subscript[E, 2], Subscript[E, 3], 
   Subscript[E, 4]}, GridLines -> Automatic]

enter image description here

Test[n_] := Series[E^x, {x, 0, n}] // Normal
Plot[{Test[1], Test[2], Test[3], Test[4]}, {x, 0, 5}, 
 PlotLegends -> {Test[1], Test[2], Test[3], Test[4]}, 
 GridLines -> Automatic]

enter image description here

Mathematica 13.2 Notebook file attached.

Thank you.

Attachments:
POSTED BY: Cornel B.
3 Replies

You have to use Evaluate:

Plot[{Test[1], Test[2], Test[3], Test[4]}, {x, 0, 5}, 
 PlotLegends -> {Test[1], Test[2], Test[3], Test[4]}, 
 GridLines -> Automatic]

The problem is that Test[n_] is defined with a delayed definition Test[n_] := and the numerical values of x are replaced in Test[1] before its definition is applied. The expressions E_n were defined with immediate evaluation.

It is indeed rather subtle.

POSTED BY: Gianluca Gorni
Posted 1 year ago

Ok. Thank you. I think that's what you meant when you said to use Evaluate (in the code posted by you, Evaluate doesn't appear)

Test[n_] := Series[E^x, {x, 0, n}] // Normal
Plot[{Evaluate[Test[1]], Evaluate[Test[2]], Evaluate[Test[3]], 
  Evaluate[Test[4]]}, {x, 0, 5}, 
 PlotLegends -> {Test[1], Test[2], Test[3], Test[4]}, 
 GridLines -> Automatic]

enter image description here

POSTED BY: Cornel B.

Yes, sorry, I pasted the wrong code.

POSTED BY: Gianluca Gorni
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