3
$\begingroup$

I have been having some trouble graphing a Taylor series approximation (Series).

f[x] := Series[E^(-x/4) Sin[3 * x], {x, 1, 4}]

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

General::ivar: 0.00012257142857142857` is not a valid variable.

enter image description here

How can I fix this error?

$\endgroup$
6
  • 5
    $\begingroup$ Use f[x_] := ... not f[x] := .... Please see Defining functions. $\endgroup$
    – Szabolcs
    Commented Aug 1, 2019 at 5:39
  • 1
    $\begingroup$ @Szabolcs and others, I reopened the question because that comment is not an answer. As shown in the last code block in my answer it does not even need to be a part of the answer. I believe voters were influenced by your comment but I won't reopen it again if it gets closed again. $\endgroup$
    – Kuba
    Commented Aug 1, 2019 at 9:14
  • $\begingroup$ Please include the message name to make it easier for others who have the same problem to search for and find this question $\endgroup$
    – Michael E2
    Commented Aug 1, 2019 at 11:47
  • $\begingroup$ @Kuba Duplicate?: mathematica.stackexchange.com/q/48980/4999 $\endgroup$
    – Michael E2
    Commented Aug 1, 2019 at 11:49
  • $\begingroup$ Related: mathematica.stackexchange.com/q/1301/4999 $\endgroup$
    – Michael E2
    Commented Aug 1, 2019 at 11:53

1 Answer 1

11
$\begingroup$

There are couple of problems with your code.

  1. It is an image, it would be way nicer not to have to rewrite it.
  2. Plotting Series

    If you go to ref / Series / Application you will see that Normal is used to plot a Series as otherwise O[x]^n will make Plot confused.

  3. Function definition

    Functions are defined more or less like that f[x_]:=... but if x is an argument to your function then Series spec {x,1,4} will become invalid as x will be replace with the passed value.

    You need to create series before you pass the value. One way to do this is to create series once for all by doing it during definition: = vs :=:

So a 'proper way' to get what you need is:

ClearAll[f];

f[x_] = Normal @ Series[E^(-x/4) Sin[3 x], {x, 1, 4}]

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

enter image description here

Coincidentally you original code was close to working, if you know what is going on:

ClearAll[f];
f[x] := Series[E^(-x/4) Sin[3 x], {x, 1, 4}]

Plot[Evaluate@Normal@f[x], {x, 0, 6}]

But this is not a way to go anyway.

$\endgroup$
1
  • 1
    $\begingroup$ ClearAll[f,x]? If you are ensuring there are no lingering definitions, then probably address all the symbols involved? $\endgroup$
    – rhermans
    Commented Aug 2, 2019 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.