Reputation: 307
Is there a simple way to define a function from a result of series command? (that is, without the remainder term)
For example
[> series(cos(x), x=0, 5);
1-1/2*x^2+1/24*x^4+O(x^5)
and I want to define a function
[> f:=(x)->1-1/2*x^2+1/24*x^4;
I don't know a simple way how to do it without manual copy/paste, I need to do it dynamically. At the moment the way I do it is really cumbersome:
[> S:=series(cos(x), x=0, 5);
f:=unapply(`+`(seq(op(S)[2*k-1]*x^(op(S)[2*k]), k=1..nops(S)/2-1)), x);
In case of generalized series requiring fractional exponents I can do it this way:
[> S:=series(sqrt(sin(x)), x=0, 4);
f:=unapply(S-op(S)[nops(S)], x);
I don't know a reliable way to combine both cases into one (the number of terms in the second case is similar to the order, while it's almost double in the first case, but it's not a reliable way since some functions might have very few terms even for high order).
Upvotes: 2
Views: 96
Reputation: 7246
This methodology covers both examples in the same fashion.
restart;
S:=series(cos(x), x=0, 5);
1 2 1 4 / 6\
S := 1 - - x + -- x + O\x /
2 24
P:=convert(S, polynom);
1 2 1 4
P := 1 - - x + -- x
2 24
F:=unapply(P, x);
1 2 1 4
F := x -> 1 - - x + -- x
2 24
F(0.3);
0.9553375000
cos(0.3);
0.9553364891
And the other,
restart;
S:=series(sqrt(sin(x)), x=0, 5);
(1/2) 1 (5/2) 1 (9/2) / (13/2)\
S := x - -- x + ---- x + O\x /
12 1440
P:=convert(S, polynom);
(1/2) 1 (5/2) 1 (9/2)
P := x - -- x + ---- x
12 1440
F:=unapply(P, x);
(1/2) 1 (5/2) 1 (9/2)
F := x -> x - -- x + ---- x
12 1440
F(0.3);
0.5436177192
sqrt(sin(0.3));
0.5436177027
You can see this step which you were missing (to convert
to polynom
), in the examples on the help page for series
itself.
Upvotes: 1