3
$\begingroup$

Suppose I have a function

$$ f(x) = \frac{1}{1+e^x}$$

I can use the function Series[] to generate a series expansion of $f$ as follows

x0 = 0;
f[x_] = 1/(1 + E^x);
poly[n_] := Normal[Series[f[x], {x, x0, n}]]
poly[3]

which yields

$$ f(x) \approx \frac{1}{2}-\frac{x}{4}+\frac{x^3}{48} $$

Denote by $f_n(x)$ the series truncated after the nth term. I want to generate a plot of the error $|f(x)-f_n(x)|$ versus $|x|$ on log scales. I think I can do this with LogLogPlot[] as follows

Clear[error]
error[n_] := error[n] = Abs[f[x] - poly[n]]
LogLogPlot[error[3], {x, 10^-10, 1}] 

This generates the plot

error

Which I think looks correct, the error curve is a straight line as expected. But now suppose I change the point of expansion to $x_0 = 1$. Now I want to plot $|f(x)-f_n(x)|$ versus $|x-x_0|$ on log scales. How can I do this?

I have tried

ParametricPlot[{Log[error[2]], Log[Abs[x - x0]]}, {x, x0, x0 + 1}]

but I don't know how I can label the ticks correctly as above.

error plot using ParametricPlot

I think maybe I need to use ListLogLogPlot[] but I'm not sure how to generate the points for the x range.

What i'm looking to do is plot something similar to

I want to generate something like this

the matlab code to do this is available here

$\endgroup$

1 Answer 1

3
$\begingroup$

Shift the plot slightly by evaluating the error at x+x0

x0 = 1;
f[x_] = 1/(1 + E^x);
(* I just want a function in n and x, tried to base it
   on poly but ended up in scoping hell *)
t[n_Integer, x_] := Normal[Series[f[y], {y, x0, n}]] /. y -> x
err[n_, x_] := Abs[f[x] - t[n, x]]

LogLogPlot[err[3, x + x0], {x, 10^-10, 1}]

err plot

(If you want to retain the numerical error jumping around at 10^-16 you can use LogLogPlot[Evaluate[err[3, x + x0]], {x, 10^-10, 1}] why this is the case is beyond my understanding )

You probably want to plot Max[err[n,x+x0],err[n,x0-x]] since the error might be bigger on the other side of x0

$\endgroup$
1
  • 1
    $\begingroup$ How come Evaluate changes the behavior of the plot? $\endgroup$
    – ssch
    Commented Jan 18, 2013 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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