Message Boards Message Boards

0
|
1053 Views
|
7 Replies
|
7 Total Likes
View groups...
Share
Share this post:

How to plot a series expansion?

Posted 6 months ago

Hi, Can anybody explain to me why the following code fails to produce a plot of the truncated series expansion?

F[x_]:=Normal[Series[Exp[x],{x,0,2}]];
Plot[F[y],{y,0,1}]

(there is a cryptic error message saying that something is not a valid variable). The online help does not explain anything.
I would also be happy to know what to change in order to get the plot without splitting the process into two stages: first obtaining the truncated series, and next re-copying the formula obtained to form a definition of a function to be plotted. Leslaw.

POSTED BY: Leslaw Bieniasz
7 Replies

Your solution is slow because the series expansion is done anew for every numerical value of x, IĀ suppose. How about this:

Clear[x, n, f];
nmax = 10;
Do[f[x_, n] = Normal[Series[Exp[x], {x, 0, n}]],
 {n, 0, nmax}]

where nmax is largest n that you are going to use.

Another way:

g[x_, n_] = Sum[SeriesCoefficient[Exp[x],
    {x, 0, i}] x^i,
  {i, 0, n}]
POSTED BY: Gianluca Gorni
Posted 6 months ago

The solution:

f[x_] = Normal[Series[Exp[x], {x, 0, 2}]]

appears elegant. However, an attempt to generalize it to a variable number of expansion terms:

f[x_,n_] = Normal[Series[Exp[x], {x, 0, n}]]

does not work. The generalization of my proposal:

   F[x_,n_]:=Module[{y,c},c=Normal[Series[Exp[y],{y,0,n}]];y=x;c];

works. The only problem is that it works damned slow during plotting (not really for the Exp function but for a more complicated one that I have); I presume the expansion is performed every time a new point is to be plotted. Leslaw

POSTED BY: Leslaw Bieniasz

The same problem happens with the derivative:

f[x_] := D[x^2, x];
f[1]
POSTED BY: Gianluca Gorni

The problem is the order of evaluation. If you insist on using delayed assignment

F[x_]:=Normal[Series[Exp[x],{x,0,2}]]

then f[1] will become Series[E, {1, 0, 2}]. The number 1 will be replaced into Normal[Series[Exp[x],{x,0,2}]] before the expansion starts. You must force the symbolic calculation before any number is replaced. I suggested immediate evaluation:

f[x_] = Normal[Series[Exp[x], {x, 0, 2}]]
POSTED BY: Gianluca Gorni
Posted 6 months ago

Regarding the proposal of Bill: indeed the Plot works, and a command

F[x]

produces an expansion formula, but there is a problem calculating a value for a particular x, for example:

Evaluate[F[2.0]]

I have tried:

F[x_]:=Module[{y,c},c=Normal[Series[Exp[y],{y,0,2}]];y=x;c];

and this seems to work correctly in all three cases, that is one can show a formula, compute a value, and create a plot without using Evaluate[]. Is this an acceptable approach? Can anybody explain to me what is going on here? I cannot understand the mechanism, and the information contained in manuals is very limited. Leslaw

POSTED BY: Leslaw Bieniasz

Another way:

Clear[x](*just in case x has already a value*);
f[x_] = Normal[Series[Exp[x], {x, 0, 2}]];
Plot[f[y], {y, 0, 1}]
POSTED BY: Gianluca Gorni
Posted 6 months ago
 F[x_]:=Normal[Series[Exp[x],{x,0,2}]];
 Plot[Evaluate[F[y]],{y,0,1}]

<<<and your plot appears>>>
POSTED BY: Bill Nelson
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