| 1 |
% Jan Holecek, Alexander Grahn |
| 2 |
% 2022/01/25 |
| 3 |
|
| 4 |
prologues:=3; |
| 5 |
outputtemplate := "%j_%c.mps"; |
| 6 |
outputformat := "mps"; |
| 7 |
|
| 8 |
verbatimtex |
| 9 |
\documentclass{article} |
| 10 |
%\usepackage{lmodern} |
| 11 |
\usepackage[tt=false]{libertine} |
| 12 |
\usepackage[libertine]{newtxmath} |
| 13 |
\usepackage{amsmath} |
| 14 |
\begin{document} |
| 15 |
etex |
| 16 |
|
| 17 |
e := 2.718282; |
| 18 |
N=8; |
| 19 |
size := 5cm; |
| 20 |
bars := 0.5mm; |
| 21 |
diff := 0.05; |
| 22 |
xmin := -2; |
| 23 |
xmax := 2; |
| 24 |
xo := -2; |
| 25 |
ymin := 0; |
| 26 |
ymax := e**xmax; |
| 27 |
xcoe := size / (xmax - xmin); |
| 28 |
ycoe := size / (ymax - ymin); |
| 29 |
|
| 30 |
|
| 31 |
pair xaxis[], yaxis[], npnt; |
| 32 |
xaxis[1] = (xmin * xcoe, 0); xaxis[2] = (xmax * xcoe, 0); |
| 33 |
yaxis[1] = (0, ymin * ycoe); yaxis[2] = (0, ymax * ycoe); |
| 34 |
npnt = (xmin * xcoe, ymax * ycoe); |
| 35 |
|
| 36 |
picture xlabel, ylabel, elabel, nlabel[]; |
| 37 |
xlabel = btex $x$ etex; |
| 38 |
ylabel = btex $y$ etex; |
| 39 |
elabel = btex $y = e^x$ etex; |
| 40 |
nlabel[1] = btex $n = 1$ etex; |
| 41 |
nlabel[2] = btex $n = 2$ etex; |
| 42 |
nlabel[3] = btex $n = 3$ etex; |
| 43 |
nlabel[4] = btex $n = 4$ etex; |
| 44 |
nlabel[5] = btex $n = 5$ etex; |
| 45 |
nlabel[6] = btex $n = 6$ etex; |
| 46 |
nlabel[7] = btex $n = 7$ etex; |
| 47 |
nlabel[8] = btex $n = 8$ etex; |
| 48 |
|
| 49 |
|
| 50 |
def pnt (expr x,y)= |
| 51 |
( x * xcoe, y * ycoe ) |
| 52 |
enddef; |
| 53 |
|
| 54 |
def exp (expr x)= |
| 55 |
pnt (x , e**x) |
| 56 |
enddef; |
| 57 |
|
| 58 |
def taylor (expr s,x)= |
| 59 |
begingroup |
| 60 |
save i,v,w; |
| 61 |
v = 1; |
| 62 |
w = 1; |
| 63 |
for i := 1 upto s: |
| 64 |
w := (w * (x-xo)) / i; |
| 65 |
v := v + w; |
| 66 |
endfor; |
| 67 |
pnt (x , v * (e**xo)) |
| 68 |
endgroup |
| 69 |
enddef; |
| 70 |
|
| 71 |
path pexp,ptaylor[]; |
| 72 |
|
| 73 |
pexp = exp(xmin) |
| 74 |
for i:=xmin+diff step diff until xmax: |
| 75 |
.. exp(i) |
| 76 |
endfor; |
| 77 |
|
| 78 |
for j:=1 upto N: |
| 79 |
ptaylor[j] := taylor (j,xmin) |
| 80 |
for i:=xmin+diff step diff until xmax: |
| 81 |
.. taylor(j,i) |
| 82 |
endfor; |
| 83 |
endfor; |
| 84 |
|
| 85 |
def axis= |
| 86 |
drawarrow xaxis[1]--xaxis[2]; |
| 87 |
drawarrow yaxis[1]--yaxis[2]; |
| 88 |
for k:=-1,1: |
| 89 |
draw (k * xcoe, -bars) -- (k * xcoe, bars); |
| 90 |
endfor; |
| 91 |
for k:=1 upto 6: |
| 92 |
draw (-bars, k * ycoe) -- (bars, k * ycoe); |
| 93 |
endfor; |
| 94 |
draw pexp withcolor red; |
| 95 |
label.lrt (xlabel, xaxis[2]); |
| 96 |
label.ulft (ylabel, yaxis[2]); |
| 97 |
label.top (elabel, point (length (pexp)) of pexp); |
| 98 |
enddef; |
| 99 |
|
| 100 |
path bounds; |
| 101 |
beginfig(0); |
| 102 |
pickup pencircle scaled 0.4mm; |
| 103 |
axis; |
| 104 |
bounds:=bbox currentpicture; |
| 105 |
setbounds currentpicture to bounds; |
| 106 |
endfig; |
| 107 |
|
| 108 |
for i:=1 upto N: |
| 109 |
beginfig(i); |
| 110 |
pickup pencircle scaled 0.4mm; |
| 111 |
draw ptaylor[i] withcolor blue; |
| 112 |
label.lrt ( nlabel[i], npnt); |
| 113 |
setbounds currentpicture to bounds; |
| 114 |
endfig; |
| 115 |
endfor; |
| 116 |
|
| 117 |
end. |