3

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?

rhermans
  • 36,518
  • 4
  • 57
  • 149
Magna Wise
  • 131
  • 2
  • 5
    Use f[x_] := ... not f[x] := .... Please see Defining functions. – Szabolcs Aug 01 '19 at 05:39
  • 1
    @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. – Kuba Aug 01 '19 at 09:14
  • Please include the message name to make it easier for others who have the same problem to search for and find this question – Michael E2 Aug 01 '19 at 11:47
  • @Kuba Duplicate?: https://mathematica.stackexchange.com/q/48980/4999 – Michael E2 Aug 01 '19 at 11:49
  • Related: https://mathematica.stackexchange.com/q/1301/4999 – Michael E2 Aug 01 '19 at 11:53
  • I think the close votes were inspired by the low quality of the question. Questions with only images as context and explanation tend to irritate people. I have edited the question that originally showed no code and no diligence and could qualify as a duplicate and a trivial error. I have voted to leave it open mainly to honour Kuba's great effort to salvage this question with an excellent answer. – rhermans Aug 02 '19 at 09:36

1 Answers1

11

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.

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • 1
    ClearAll[f,x]? If you are ensuring there are no lingering definitions, then probably address all the symbols involved? – rhermans Aug 02 '19 at 09:23