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.

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

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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

share|improve this answer
1  
How come Evaluate changes the behavior of the plot? –  ssch Jan 18 '13 at 20:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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