# Defining a function using Series? [duplicate]

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?

-

## marked as duplicate by belisarius, Ｊ. Ｍ.Aug 22 '12 at 11:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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). –  acl Aug 21 '12 at 17:46
@NasserM.Abbasi M=4 or 5 to say something. I thought it was implied in the context of Series, silly me. –  Pragabhava Aug 21 '12 at 20:39
@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) –  acl Aug 21 '12 at 21:44
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 :) –  acl Aug 21 '12 at 23:19
This is the same issue as in this question –  rm -rf Aug 22 '12 at 0:32

## 2 Answers

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.

-
That was exactly what I meant. Thanks a lot J.M. –  Pragabhava Aug 21 '12 at 20:26

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?

-
You are correct. It was a bad example. I apologize. –  Pragabhava Aug 21 '12 at 20:56