6
$\begingroup$

Possible Duplicate:
General::ivar is not a valid variable when plotting - what actually causes this and how to avoid it?

I need to define a function using the output of series, i.e.

 f[x_,M_]:= Normal[Series[Sin[x],{x,0,M}]];

Now, for reasons explained in Normal[Series[ ]] does not give a normal expression that I can't fully understand (due inexperience and lack of time), the above code does not work as is.

I've managed to use f within other functions, like

 InverseLaplaceTransform[#,s,t] & @ f[s,M]

where M is given, but failing when trying to evaluate, i.e.

In[1]:= f[x_,m_]:= Normal[Series[Sin[x],{x,0,m}]];

In[2]:= f[x,3]

Out[2]= x - x^3/6

In[3]:= f[2,3]

General::ivar: 2 is not a valid variable. >>
General::ivar: 2 is not a valid variable. >>

Out[3]= Sin[2]

In a nutshell, is there a way to use f like a "normal" function?

$\endgroup$
  • 1
    $\begingroup$ what do you mean "the above code does not work as is."? what do you do and what goes wrong? (apart from a syntax error due to a missing bracket, which I suppose you forgot to paste). $\endgroup$ – acl Aug 21 '12 at 17:46
  • $\begingroup$ @NasserM.Abbasi M=4 or 5 to say something. I thought it was implied in the context of Series, silly me. $\endgroup$ – Pragabhava Aug 21 '12 at 20:39
  • 1
    $\begingroup$ @Manuel perhaps you could update the question to indicate what the problem is, then? by, eg, adding "for instance, f[3,3] fails with...". this would make it easier to locate (this is a common problem, so others will definitely have the same question) $\endgroup$ – acl Aug 21 '12 at 21:44
  • 1
    $\begingroup$ No reason to feel bad about clarifying the question, it will probably help others in the future this way. Just think of the happiness you are spreading by doing this :) $\endgroup$ – acl Aug 21 '12 at 23:19
  • 2
    $\begingroup$ This is the same issue as in this question $\endgroup$ – rm -rf Aug 22 '12 at 0:32
8
$\begingroup$

In a nutshell, is there a way to use f like a "normal" function?

If you want to be able to evaluate f for numerical values of x as well, then your definition as is won't work. One way to go about it is to expand Sin[] with a dummy variable before passing to it the value of x. Consider, for instance

f[x_, m_Integer] := (Normal[Series[Sin[\[FormalX]], {\[FormalX], 0, m}]] /. \[FormalX] -> x)

Here, \[FormalX] is what's called as a "formal symbol", which cannot be assigned values (accidentally or otherwise); Series[] however still knows how to treat it as a dummy variable, which one can then replace with an actual number after Normal[] does its work.

$\endgroup$
  • $\begingroup$ That was exactly what I meant. Thanks a lot J.M. $\endgroup$ – Pragabhava Aug 21 '12 at 20:26
6
$\begingroup$

If I define

f[x_, M_] := Normal[Series[Sin[x], {x, 0, M}]]

(note the extra ']') then, for instance,

InverseLaplaceTransform[f[s, 5] , s, t]

returns (* \[Delta]^\[Prime](t)-1/6 \[Delta]^(3)(t)+1/120 \[Delta]^(5)(t) *)

As it should?

$\endgroup$
  • $\begingroup$ You are correct. It was a bad example. I apologize. $\endgroup$ – Pragabhava Aug 21 '12 at 20:56

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