summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/metapost/bpolynomial/examples.mp
blob: 8caa5ced684a93eb17c02474d707e204873d2738 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
input bpolynomial;
input metafun
input graph
input hatching
prologues := 3;

transform T;
T := identity xscaled 10mm yscaled 1mm;

beginfig(1);
  newBPolynomial.f(2, 0, -3, -1);
  draw f.getPath(-2, 2) transformed T;
endfig;


beginfig(2);
  newBPolynomial.f(2, 0, -3, -1);
  draw f.getPath(-2, 2) transformed T;
  draw f.getTangent(-0.5)(-1, 1) transformed T;
endfig;


beginfig(3);
  dotlabeldiam := 2bp;
  labeloffset := 10bp;
  newBPolynomial.f(2, 0, -3, -1);
  draw f.getPath(-2, 2) transformed T;
  x := -0.5;
  show (x, f.eval(x));
  draw f.getTangent(x)(-1, 1) transformed T;
  dotlabel.top(btex $(-0.5, 0.25)$ etex, (x, f.eval(x)) transformed T);
endfig;

labeloffset := 3bp;

beginfig(4);
  newBPolynomial.f(2, 0, -3, -1);
  draw f'.getPath(-2, 2) transformed T;
  draw f'.getTangent(-0.25)(-1, 1) transformed T;
endfig;


beginfig(5);
  T := identity scaled 10mm;
  newBSqrRoot.s(1,0,0);
  newBCubRoot.c(1,0,0);
  draw s.getPath(0,6) transformed T;
  draw c.getPath(0,6) transformed T;
  draw s.getTangent(3)(-2, 2) transformed T;
endfig;


T := identity xscaled 10mm yscaled 1mm;
beginfig(6);
  draw getBezierFromPolynomial(2, 0, -3, -1)(-2, 2) transformed T;
endfig;


beginfig(11);
path f, g;
  xmin := -7; xmax := 7;
  ymin := -7; ymax := 7;
  %%% Define polynomials f and g.
  newBPolynomial.f(0.3, 0, -3, -1);
  f := f.getPath(xmin, xmax);
  newBPolynomial.g(0, 0.5, -2, 0);
  g := g.getPath(xmin, xmax);
  %%% Draw graph.
  draw begingraph(10cm, 6cm);
    setrange(xmin,ymin, xmax,ymax);
    autogrid(grid.bot, grid.lft) dashed evenly withcolor .9white;
    drawoptions(withpen pencircle scaled 1bp);
    gdraw f;
    gdraw g dashed evenly scaled 2;
    drawoptions();
  endgraph;
  show f;
  %%% Write table with some points of f to log file.
  show "Polynomial: " & decimal f.a & "x^3 + " &
    decimal f.b & "x^2 + " & decimal f.c & "x + " & decimal f.d;
  for x=-5 upto 5:
    show (x, f.eval(x));
  endfor
endfig;


beginfig(12);
  xmin := -6; xmax := 6;
  ymin := -6; ymax := 6;
  newBPolynomial.f(0.3, -0.5, -0.5, -1);
  draw begingraph(10cm, 6cm);
    setrange(xmin,ymin, xmax,ymax);
    autogrid(grid.bot, grid.lft) dashed evenly withcolor .9white;
    drawoptions(withpen pencircle scaled 1bp);
    %%% Draw f and its derivatives f', f'', f'''.
    gdraw f.getPath(xmin, xmax);
    gdraw f'.getPath(xmin, xmax) dashed evenly scaled 2;
    gdraw f''.getPath(xmin, xmax) dashed withdots
      withpen pencircle scaled 2bp;
    gdraw f'''.getPath(-5, 5) withcolor .6white;
    %%% Draw tangents and mark points.
    x := 2;
    drawoptions(withcolor (1, 0.6, 0.6));
    gdraw f.getTangent(x)(-2, 2);
    gdraw f'.getTangent(x)(-1, 1);
    gdraw f''.getTangent(x)(-2, 2);
    gdraw f'''.getTangent(x)(-2, 2);
    drawoptions(withcolor (0.6, 0.6, 1));
    dotlabeldiam := 2.5bp;
    gdotlabel("", (x, f.eval(x)));
    gdotlabel("", (x, f'.eval(x)));
    gdotlabel("", (x, f''.eval(x)));
    gdotlabel("", (x, f'''.eval(x)));
    drawoptions();
  endgraph;
endfig;


beginfig(13);
path f, g, A;
  xmin := -3; xmax := 6;
  ymin := -3; ymax := 6;
  newBPolynomial.f(-0.25, 0.5, 2, -1);
  newBPolynomial.g(0, 0.5, -2, 0);
  f := f.getPath(-2.5, 5.5);
  g := g.getPath(-1.5, 5.5);
  %%% Find area between f and g.
  A := buildcycle(g, reverse f);
  draw begingraph(10cm, 6cm);
    setrange(xmin,ymin, xmax,ymax);
    autogrid(grid.bot, grid.lft) dashed evenly withcolor .9white;
    %%% Fill area with transparent colour.
    gfill A withcolor transparent (1, .3, (1, 0.5, 0));
    drawoptions(withpen pencircle scaled 1bp);
    gdraw f;
    gdraw g dashed evenly scaled 2;
    drawoptions();
  endgraph;
endfig;


beginfig(14);
path f, g, A;
  T := identity xscaled 10mm yscaled 6mm;
  %%% Draw coordinate system.
  xmin := -3; xmax := 6;
  ymin := -3; ymax := 6;
  drawoptions(withpen pencircle scaled 1bp withcolor 0.8white);
  drawarrow ((xmin,0)--(xmax,0)) transformed T;
  drawarrow ((0,ymin)--(0,ymax)) transformed T;
  newBPolynomial.f(-0.25, 0.5, 2, -1);
  newBPolynomial.g(0, 0.5, -2, 0);
  f := f.getPath(-2.5, 4.2);
  g := g.getPath(-1, 5);
  A := buildcycle(g, reverse f);
  %%% Fill area with pattern.
  drawoptions();
  hatchoptions(withcolor (0.6, 0.3, 0.3));
  hatchfill A transformed T
    withcolor (-45, 2mm, -0.5bp) withcolor (45, 2mm, -0.5bp);
  drawoptions(withpen pencircle scaled 1bp);
  draw f transformed T;
  draw g transformed T dashed evenly scaled 2;
endfig;


%%%  The following figures work around a bug in metafun's
%%%  mp-form.mp package for the original figures 11 to 13.
%%%  The bug shows up when rendering negative numbers
%%%  on corrdinate axes using macro 'format'.
beginfig(21);
path f, g;
  xmin := -7; xmax := 7;
  ymin := -7; ymax := 7;
  %%% Define polynomials f and g.
  newBPolynomial.f(0.3, 0, -3, -1);
  f := f.getPath(xmin, xmax);
  newBPolynomial.g(0, 0.5, -2, 0);
  g := g.getPath(xmin, xmax);
  %%% Draw graph.
  draw begingraph(10cm, 6cm);
    setrange(xmin,ymin, xmax,ymax);
%     autogrid(grid.bot, grid.lft) dashed evenly withcolor .9white;
    for i=xmin+1 step 2 until xmax-1:
      grid.bot(decimal i, i) dashed evenly withcolor .9white;
    endfor
    for i=ymin+1 step 2 until ymax-1:
      grid.lft(decimal i, i) dashed evenly withcolor .9white;
    endfor
    drawoptions(withpen pencircle scaled 1bp);
    gdraw f;
    gdraw g dashed evenly scaled 2;
    drawoptions();
  endgraph;
  show f;
  %%% Write table with some points of f to log file.
  show "Polynomial: " & decimal f.a & "x^3 + " &
    decimal f.b & "x^2 + " & decimal f.c & "x + " & decimal f.d;
  for x=-5 upto 5:
    show (x, f.eval(x));
  endfor
endfig;


beginfig(22);
  xmin := -6; xmax := 6;
  ymin := -6; ymax := 6;
  newBPolynomial.f(0.3, -0.5, -0.5, -1);
  draw begingraph(10cm, 6cm);
    setrange(xmin,ymin, xmax,ymax);
%     autogrid(grid.bot, grid.lft) dashed evenly withcolor .9white;
    for i=xmin step 2 until xmax:
      grid.bot(decimal i, i) dashed evenly withcolor .9white;
    endfor
    for i=ymin step 2 until ymax:
      grid.lft(decimal i, i) dashed evenly withcolor .9white;
    endfor
    drawoptions(withpen pencircle scaled 1bp);
    %%% Draw f and its derivatives f', f'', f'''.
    gdraw f.getPath(xmin, xmax);
    gdraw f'.getPath(xmin, xmax) dashed evenly scaled 2;
    gdraw f''.getPath(xmin, xmax) dashed withdots
      withpen pencircle scaled 2bp;
    gdraw f'''.getPath(-5, 5) withcolor .6white;
    %%% Draw tangents and mark points.
    x := 2;
    drawoptions(withcolor (1, 0.6, 0.6));
    gdraw f.getTangent(x)(-2, 2);
    gdraw f'.getTangent(x)(-1, 1);
    gdraw f''.getTangent(x)(-2, 2);
    gdraw f'''.getTangent(x)(-2, 2);
    drawoptions(withcolor (0.6, 0.6, 1));
    dotlabeldiam := 2.5bp;
    gdotlabel("", (x, f.eval(x)));
    gdotlabel("", (x, f'.eval(x)));
    gdotlabel("", (x, f''.eval(x)));
    gdotlabel("", (x, f'''.eval(x)));
    drawoptions();
  endgraph;
endfig;


beginfig(23);
path f, g, A;
  xmin := -3; xmax := 6;
  ymin := -3; ymax := 6;
  newBPolynomial.f(-0.25, 0.5, 2, -1);
  newBPolynomial.g(0, 0.5, -2, 0);
  f := f.getPath(-2.5, 5.5);
  g := g.getPath(-1.5, 5.5);
  %%% Find area between f and g.
  A := buildcycle(g, reverse f);
  draw begingraph(10cm, 6cm);
    setrange(xmin,ymin, xmax,ymax);
%     autogrid(grid.bot, grid.lft) dashed evenly withcolor .9white;
    for i=xmin+1 step 2 until xmax:
      grid.bot(decimal i, i) dashed evenly withcolor .9white;
    endfor
    for i=ymin+1 step 2 until ymax:
      grid.lft(decimal i, i) dashed evenly withcolor .9white;
    endfor
    %%% Fill area with transparent colour.
    gfill A withcolor transparent (1, .3, (1, 0.5, 0));
    drawoptions(withpen pencircle scaled 1bp);
    gdraw f;
    gdraw g dashed evenly scaled 2;
    drawoptions();
  endgraph;
endfig;

end