ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Sample Metapost files for calculus
@ 2000-09-30 18:19 David Arnold
  0 siblings, 0 replies; only message in thread
From: David Arnold @ 2000-09-30 18:19 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 661 bytes --]

All,

I've attached a sample Metapost file that generates some figures I needed
for my latest calculus exam.

One file I use a lot is a little function grapher I wrote in Metapost. I
wrote it so that you just have to change the clipping window and the
function definition. Afterwards, I have to change some labels manually, but
that doesn't take much time. It's pretty good with continuous functions,
but discontinuous functions, like (x+1)/(x-1) have to be drawn in pieces. I
am not a good enough programmer to figure out how to write code to take
care of curves with discontinuities. See beginfig(4);...endfig; for an
example of what I am talking about here.

[-- Attachment #2: exam.mp --]
[-- Type: text/plain, Size: 11143 bytes --]

input mp-tool;

%prologues:=0;

%pi
numeric pi;
pi:=3.14159;

%cos function for radian input
def cos(expr x)=
 cosd(180/pi*x)
enddef;

%sin function for radian input
def sin(expr x)=
 sind(180/pi*x)
enddef;

%xtick marks
def xtick(expr p)=
 draw ((0,-2pt)--(0,2pt)) shifted p;
enddef;

%ytick marks
def ytick(expr p)=
 draw ((-2pt,0)--(2pt,0)) shifted p;
enddef;

%opendot
vardef open(expr pos)=
 path p;
 p:=fullcircle scaled 3pt;
 p:=p shifted pos;
 fill p withcolor white;
 draw p withcolor red;
enddef;

%closedot
vardef closed(expr pos)=
 path p;
 p:=fullcircle scaled 3pt;
 p:=p shifted pos;
 fill p withcolor red;
 draw p withcolor red;
enddef;

vardef pointarrow (expr pat, loc, len, off) =
save l, r, s, t ; path l, r ; numeric s ; pair t ;
t := if pair loc : loc else : point loc along pat fi ;
s := len/2 - off ; if s<=0 : s := 0 elseif s>len : s := len fi ;
r := pat cutbefore t ;
r := (r cutafter point (arctime s of r) of r) ;
s := len/2 + off ; if s<=0 : s := 0 elseif s>len : s := len fi ;
l := reverse (pat cutafter t) ;
l := (reverse (l cutafter point (arctime s of l) of l)) ;
(l..r)
enddef ;

def rightarrow (expr p,t,l) = pointarrow(p,t,l,-l) enddef ;
def leftarrow (expr p,t,l) = pointarrow(p,t,l,+l) enddef ;
def centerarrow(expr p,t,l) = pointarrow(p,t,l, 0) enddef ;

beginfig(1);

%initialize scale
numeric u; 10u=2in;

%draw the grid
for k=-5u step 1u until 5u:
 draw (k,-5u)--(k,5u) withcolor 0.85white;
 draw (-5u,k)--(5u,k) withcolor 0.85 white;
endfor;

%draw axes
drawdblarrow (-5u,0)--(5u,0);
label.rt(btex $x$ etex, (5u,0));
drawdblarrow (0,-5u)--(0,5u);
label.top(btex $y$ etex, (0,5u));

%draw the piecewise graph
pickup pencircle scaled 1pt;
drawarrow (1u,0)--(-4u,-5u) withcolor red;
drawarrow (1u,3u)--(5u,3u) withcolor red;
pickup defaultpen;

%label f
label.rt(btex $f$ etex, (5u,3u));

%draw points open and closed
open ((1u,0));
closed ((1u,2u));
open ((1u,3u));

endfig;

beginfig(2);

%initialize scale
numeric u; 10u=2in;

%draw the grid
for k=-5u step 1u until 5u:
 draw (k,-5u)--(k,5u) withcolor 0.85white;
 draw (-5u,k)--(5u,k) withcolor 0.85 white;
endfor;

%draw axes
drawdblarrow (-5u,0)--(5u,0);
label.rt(btex $x$ etex, (5u,0));
drawdblarrow (0,-5u)--(0,5u);
label.top(btex $y$ etex, (0,5u));

%draw the piecewise graph
pickup pencircle scaled 1pt;
drawarrow (1u,0)--(-4u,-5u) withcolor red;
drawarrow (1u,3u)--(5u,3u) withcolor red;
pickup defaultpen;

%label f
label.rt(btex $f$ etex, (5u,3u));

%draw points open and closed
open ((1u,0));
closed ((1u,2u));
open ((1u,3u));

%highlight limit direction with arrows
path p; p:=(-4u,-5u)--(1u,0);
pickup pencircle scaled 1pt;
drawarrow rightarrow(p,point .3 of p, .5cm) withcolor blue;
drawarrow rightarrow(p,point .6 of p, .5cm) withcolor blue;
pickup defaultpen;

endfig;

beginfig(3);

%initialize scale
numeric u; 10u=2in;

%draw the grid
for k=-5u step 1u until 5u:
 draw (k,-5u)--(k,5u) withcolor 0.85white;
 draw (-5u,k)--(5u,k) withcolor 0.85 white;
endfor;

%draw axes
drawdblarrow (-5u,0)--(5u,0);
label.rt(btex $x$ etex, (5u,0));
drawdblarrow (0,-5u)--(0,5u);
label.top(btex $y$ etex, (0,5u));

%draw the piecewise graph
pickup pencircle scaled 1pt;
drawarrow (1u,3u)--(-5u,3u) withcolor red;
drawarrow (1u,3u)--(5u,3u) withcolor red;
pickup defaultpen;

%label f
label.rt(btex $f$ etex, (5u,3u));

%draw points open and closed
open ((1u,3u));
closed ((1u,2u));

endfig;

beginfig(4);

%Define function to be drawn
def f(expr x)=
 x*x-4
enddef;

%Declare and initialize viewing window
numeric xmin, xmax, ymin, ymax;
xmin=-5;
xmax=5;
ymin=-5;
ymax=5;

%Declare and initialize scaling variables
numeric ux, uy;
(xmax-xmin)*ux=2in;
(ymax-ymin)*uy=2in;

%Declare and initialize clipping boundary
path q;
q:=(xmin,ymin)--(xmin,ymax)--(xmax,ymax)--(xmax,ymin)--cycle;
q:=q xscaled ux yscaled uy;

%Declare and initialize number of plotted points
numeric N; N:=200;

%Declare and calculate plotting increment delta x
numeric dx; dx:=(xmax-xmin)/N;

%Declare and create function path
path p; p:=(xmin,f(xmin));
for x=xmin step dx until xmax:
 p:=p--(x,f(x));
endfor; p:=p--(xmax,f(xmax));

%Scale function path
p:=p xscaled ux yscaled uy;

%Clip function path
draw p; clip currentpicture to q;

%Save and clear the function path
picture pic; pic:=currentpicture; clearit;

%draw the grid
for k=xmin*ux step 1ux until xmax*ux:
 draw (k,ymin*uy)--(k,ymax*uy) withcolor 0.85white;
endfor;
for k=ymin*uy step 1uy until ymax*uy:
 draw (xmin*ux,k)--(xmax*ux,k) withcolor 0.85white;
endfor;

%Draw and label coordinate axes
drawdblarrow (xmin*ux,0)--(xmax*ux,0);
label.rt(btex $x$ etex,(xmax*ux,0));
drawdblarrow (0,ymin*uy)--(0,ymax*uy);
label.top(btex $y$ etex,(0,ymax*uy));

%Superimpose function plot
draw pic withcolor red;

%label xticks
label.bot(btex $5$ etex, (xmax*ux,0));

%label yticks
label.lft(btex $5$ etex,(0,ymax*uy));

endfig;

beginfig(5);

%Define function to be drawn
def f(expr x)=
 x*x-4
enddef;

%Declare and initialize viewing window
numeric xmin, xmax, ymin, ymax;
xmin=-5;
xmax=5;
ymin=-5;
ymax=5;

%Declare and initialize scaling variables
numeric ux, uy;
(xmax-xmin)*ux=2in;
(ymax-ymin)*uy=2in;

%Declare and initialize clipping boundary
path q;
q:=(xmin,ymin)--(xmin,ymax)--(xmax,ymax)--(xmax,ymin)--cycle;
q:=q xscaled ux yscaled uy;

%Declare and initialize number of plotted points
numeric N; N:=200;

%Declare and calculate plotting increment delta x
numeric dx; dx:=(xmax-xmin)/N;

%Declare and create function path
path p; p:=(xmin,f(xmin));
for x=xmin step dx until xmax:
 p:=p--(x,f(x));
endfor; p:=p--(xmax,f(xmax));

%Scale function path
p:=p xscaled ux yscaled uy;

%Clip function path
draw p; clip currentpicture to q;

%Save and clear the function path
picture pic; pic:=currentpicture; clearit;

%draw the grid
for k=xmin*ux step 1ux until xmax*ux:
 draw (k,ymin*uy)--(k,ymax*uy) withcolor 0.85white;
endfor;
for k=ymin*uy step 1uy until ymax*uy:
 draw (xmin*ux,k)--(xmax*ux,k) withcolor 0.85white;
endfor;

%Draw and label coordinate axes
drawdblarrow (xmin*ux,0)--(xmax*ux,0);
label.rt(btex $x$ etex,(xmax*ux,0));
drawdblarrow (0,ymin*uy)--(0,ymax*uy);
label.top(btex $y$ etex,(0,ymax*uy));

%Superimpose function plot
draw pic withcolor red;

%label xticks
label.bot(btex $5$ etex, (xmax*ux,0));

%label yticks
label.lft(btex $5$ etex,(0,ymax*uy));

%draw the tangent line
draw (-3ux,5uy)--(-4/3*ux,-5uy) withcolor blue;

%draw right triangle
draw (-3ux,5uy)--(-2ux,5uy)--(-2ux,-1uy) withcolor 0.625yellow;

endfig;

beginfig(6);

%Define function to be drawn
def f(expr x)=
 (x*x*x-12*x)/5
enddef;

%Declare and initialize viewing window
numeric xmin, xmax, ymin, ymax;
xmin=-5;
xmax=5;
ymin=-5;
ymax=5;

%Declare and initialize scaling variables
numeric ux, uy;
(xmax-xmin)*ux=2in;
(ymax-ymin)*uy=2in;

%Declare and initialize clipping boundary
path q;
q:=(xmin,ymin)--(xmin,ymax)--(xmax,ymax)--(xmax,ymin)--cycle;
q:=q xscaled ux yscaled uy;

%Declare and initialize number of plotted points
numeric N; N:=200;

%Declare and calculate plotting increment delta x
numeric dx; dx:=(xmax-xmin)/N;

%Declare and create function path
path p; p:=(xmin,f(xmin));
for x=xmin step dx until xmax:
 p:=p--(x,f(x));
endfor; p:=p--(xmax,f(xmax));

%Scale function path
p:=p xscaled ux yscaled uy;

%Clip function path
draw p; clip currentpicture to q;

%Save and clear the function path
picture pic; pic:=currentpicture; clearit;

%draw the grid
for k=xmin*ux step 1ux until xmax*ux:
 draw (k,ymin*uy)--(k,ymax*uy) withcolor 0.85white;
endfor;
for k=ymin*uy step 1uy until ymax*uy:
 draw (xmin*ux,k)--(xmax*ux,k) withcolor 0.85white;
endfor;

%Draw and label coordinate axes
drawdblarrow (xmin*ux,0)--(xmax*ux,0);
label.rt(btex $x$ etex,(xmax*ux,0));
drawdblarrow (0,ymin*uy)--(0,ymax*uy);
label.top(btex $y$ etex,(0,ymax*uy));

%Superimpose function plot
draw pic withcolor red;

%label xticks
label.bot(btex $5$ etex, (xmax*ux,0));

%label yticks
label.lft(btex $5$ etex,(0,ymax*uy));

%label the graph
label.rt(btex $f$ etex, (4.5ux,5uy));

endfig;

beginfig(7);

%Define function to be drawn
def f(expr x)=
 (x*x*x-12*x)/5
enddef;

%Declare and initialize viewing window
numeric xmin, xmax, ymin, ymax;
xmin=-5;
xmax=5;
ymin=-5;
ymax=5;

%Declare and initialize scaling variables
numeric ux, uy;
(xmax-xmin)*ux=2in;
(ymax-ymin)*uy=2in;

%Declare and initialize clipping boundary
path q;
q:=(xmin,ymin)--(xmin,ymax)--(xmax,ymax)--(xmax,ymin)--cycle;
q:=q xscaled ux yscaled uy;

%Declare and initialize number of plotted points
numeric N; N:=200;

%Declare and calculate plotting increment delta x
numeric dx; dx:=(xmax-xmin)/N;

%Declare and create function path
path p; p:=(xmin,f(xmin));
for x=xmin step dx until xmax:
 p:=p--(x,f(x));
endfor; p:=p--(xmax,f(xmax));

%Scale function path
p:=p xscaled ux yscaled uy;

%Highlight path where concave up
path r; r:=p cutbefore (point 100 of p);

%Clip function path
draw p withcolor red;
draw r withcolor blue;
clip currentpicture to q;

%Save and clear the function path
picture pic; pic:=currentpicture; clearit;

%draw the grid
for k=xmin*ux step 1ux until xmax*ux:
 draw (k,ymin*uy)--(k,ymax*uy) withcolor 0.85white;
endfor;
for k=ymin*uy step 1uy until ymax*uy:
 draw (xmin*ux,k)--(xmax*ux,k) withcolor 0.85white;
endfor;

%Draw and label coordinate axes
drawdblarrow (xmin*ux,0)--(xmax*ux,0);
label.rt(btex $x$ etex,(xmax*ux,0));
drawdblarrow (0,ymin*uy)--(0,ymax*uy);
label.top(btex $y$ etex,(0,ymax*uy));

%Superimpose function plot
draw pic;

%label xticks
label.bot(btex $5$ etex, (xmax*ux,0));

%label yticks
label.lft(btex $5$ etex,(0,ymax*uy));

%label the graph
label.rt(btex $f$ etex, (4.5ux,5uy));

endfig;

beginfig(8);

%Define function to be drawn
def f(expr x)=
 -(x*x*x-12*x)/5
enddef;

%Declare and initialize viewing window
numeric xmin, xmax, ymin, ymax;
xmin=-5;
xmax=5;
ymin=-5;
ymax=5;

%Declare and initialize scaling variables
numeric ux, uy;
(xmax-xmin)*ux=2in;
(ymax-ymin)*uy=2in;

%Declare and initialize clipping boundary
path q;
q:=(xmin,ymin)--(xmin,ymax)--(xmax,ymax)--(xmax,ymin)--cycle;
q:=q xscaled ux yscaled uy;

%Declare and initialize number of plotted points
numeric N; N:=200;

%Declare and calculate plotting increment delta x
numeric dx; dx:=(xmax-xmin)/N;

%Declare and create function path
path p; p:=(xmin,f(xmin));
for x=xmin step dx until xmax:
 p:=p--(x,f(x));
endfor; p:=p--(xmax,f(xmax));

%Scale function path
p:=p xscaled ux yscaled uy;

%Clip function path
draw p withcolor red;
clip currentpicture to q;

%Save and clear the function path
picture pic; pic:=currentpicture; clearit;

%draw the grid
for k=xmin*ux step 1ux until xmax*ux:
 draw (k,ymin*uy)--(k,ymax*uy) withcolor 0.85white;
endfor;
for k=ymin*uy step 1uy until ymax*uy:
 draw (xmin*ux,k)--(xmax*ux,k) withcolor 0.85white;
endfor;

%Draw and label coordinate axes
drawdblarrow (xmin*ux,0)--(xmax*ux,0);
label.rt(btex $x$ etex,(xmax*ux,0));
drawdblarrow (0,ymin*uy)--(0,ymax*uy);
label.top(btex $y$ etex,(0,ymax*uy));

%Superimpose function plot
draw pic;

%label xticks
label.bot(btex $5$ etex, (xmax*ux,0));

%label yticks
label.lft(btex $5$ etex,(0,ymax*uy));

%label the graph
label.rt(btex $f'$ etex, (4.5ux,-5uy));

endfig;

end

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2000-09-30 18:19 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-09-30 18:19 Sample Metapost files for calculus David Arnold

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).