Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question

marked as duplicate by belisarius, J. M. 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.

1  
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
1  
@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
1  
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
2  
This is the same issue as in this question –  rm -rf Aug 22 '12 at 0:32
show 3 more comments

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
That was exactly what I meant. Thanks a lot J.M. –  Pragabhava Aug 21 '12 at 20:26
add comment

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?

share|improve this answer
    
You are correct. It was a bad example. I apologize. –  Pragabhava Aug 21 '12 at 20:56
add comment

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