Given a Black Box function ff
that takes a single floating-point parameter, write a function M
to generate the Maclaurin series for that function, according to the following specifications:
- take a parameter
n
, and generate all terms fromx^0
tox^n
where0 <= n <= 10
- coefficients for each term should be given to at least 5 decimal places for non-integral values; underflows can simply be ignored or replaced with
0
and left out if you wish, or you can use scientific notation and more decimal places - assume
ff
is a continuous function and everywhere/infinitely differentiable forx > -1
, and returns values for inputx
in the range:-10^9 <= ff(x) <= 10^9
- programming languages with functions as first-class data types can take
ff
as a parameter; all other languages assume a function calledff
has already been defined. - output should be formatted like
# + #x + #x^2 + #x^5 - #x^8 - #x^9 ...
, i.e., terms having0
coefficient should be left out, and negative coefficients should be written- #x^i
instead of+ -#x^i
; if the output terms are all zero, you can output either0
or null output - output can be generated either to
stdout
or as a return value of typestring
from the function
Examples (passing ff
as a first parameter):
> M(x => sin(x), 8)
x - 0.16667x^3 + 0.00833x^5 - 0.00020x^7
> M(x => e^x, 4)
1 + x + 0.5x^2 + 0.16667x^3 + 0.04167x^4
> M(x => sqrt(1+x), 4)
1 + 0.5x - 0.125x^2 + 0.0625x^3 - 0.03906x^4
> M(x => sin(x), 0)
0 (or blank is also accepted)
0
either one. I'll make those both clear, thanks! \$\endgroup\$ – mellamokb Oct 26 '11 at 20:53sqrt(1+x)
, but you allowed us to assume thatff
is continuous and everywhere differentiable. \$\endgroup\$ – Peter Taylor Oct 26 '11 at 22:38everywhere/infinitely differentiable for x > -1
. My goal was that caring about discontinuities would not be part of the problem, but in the case ofsqrt(1+x)
, I had just lifted off of the samples on the Wiki page without thinking. I believe you can find the answer as long asff
is infinitely differentiable in at least small range centered around0
. \$\endgroup\$ – mellamokb Oct 27 '11 at 2:00