2
$\begingroup$

I have defined this function:

f[x0_, n_] := Normal[Series[Exp[x], {x, 0, n}]] /. x -> x0

it seems to be working but when I want to plot it

 Plot[f[x, 1], {x, 0, 1}]

I face this problem (Series::ivar):

The problem I faced

The problem I faced

Could you please help me to understand why? Thank you so much

$\endgroup$
1
  • 3
    $\begingroup$ Try Evaluate[f[x,1]] in the Plot function. $\endgroup$ Commented May 21 at 15:31

3 Answers 3

3
$\begingroup$

While it might not seem to make much difference at the end of the day, I'd actually suggest you change your definition of f. What you're doing is creating a series in x and then replacing x with a value. But that's what a function is: an expression in terms of some symbol used as a formal variable, which when applied to an argument replaces the formal variable with the argument.

fnew[n_] := Function[x, Evaluate[Normal[Series[Exp[x], {x, 0, n}]]]]

And then

Plot[fnew[1][x], {x, 0, 1}]
$\endgroup$
3
$\begingroup$

To me, the main problem is that the definition of f[] depends on the global symbol x:

f[x0_, n_] := Normal[Series[Exp[x], {x, 0, n}]] /. x -> x0;

This means a user or a function calling f[], like Plot[] or Table[], can affect its behavior. This is clearly not intended in this case. (If it were, the standard recommendation would be to make x an argument of f[].)

I suppose the standard fix is to use Module[]:

ClearAll[f]; (* <-- Good practice: remove previous defs *)
f[x0_, n_] := Module[{x},
  Normal[Series[Exp[x], {x, 0, n}]] /. x -> x0];

Then Plot[f[x, 1], {x, 0, 1}] works without problem.

If you don't like local variables, here is a less standard, but perfectly okay, approach:

ClearAll[f];
f[x0_, n_Integer] := Evaluate[Normal@Series[Exp[#], {#, 0, n}]] &[x0];

Note the pattern restriction n_Integer. This ensures that Series[] won't be evaluated unless n is a definite integer. See remark below, too, for a related issue.


Remark: The issue skates close to the typical ?NumericQ issue discussed in What are the most common pitfalls awaiting new users?. But I think localizing the dummy variable x is the best approach.

$\endgroup$
1
  • $\begingroup$ Just noticed: the def of f[] above is similar to @lericr's in part (Function vs. # and &). $\endgroup$ Commented May 21 at 18:22
3
$\begingroup$

There are 2 issues here. First "SetDelayed" evaluates each time you call the function. This means, e.g. in a plot, that the series expansion is done for every plot point. Second, if you include the order and expansion point of the series in the call f[x0_,n_], this also means that the series expansion is done in every call.

What you want is a definition that does the expansion, specifying "n" and "x0" and that returns a polynomial of the truncated series, that can subsequently be plotted. To define such a function:

getPoly[x0_, n_Integer] := 
 Function[x, Evaluate[Normal[Series[Exp[x], {x, x0, n}]]]]

We need "Evaluate" here, because "Function" has the attribute "HoldAll". Otherwise, the expansion would be done in every call of the returned function. Now we can create the truncated series:

poly = getPoly[0, 3]

enter image description here

Finally we can plot it, e.g.:

Plot[poly[x], {x, 0, 4}]

enter image description here

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.