# 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?

-
You have syntax error. Also, please give a complete working example of how you called it. Saying "where M is given.". What does this mean? Given by what, how, where, when? what value? you want me to make one up? – Nasser Aug 21 '12 at 17:21
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
@Manuel, I know it was implied what it is. But you missed the point. Someone asking a question should provide a complete working example and not assume anything that the user must make up. It makes everything clear. Please see sscce.org . This makes it easier to get faster help. – Nasser Aug 21 '12 at 20:42
@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
This is the same issue as in this question – rm -rf Aug 22 '12 at 0:32

## marked as duplicate by belisarius, J. M.♦Aug 22 '12 at 11:25

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