prologues := 3;
outputformat := "svg";
defaultfont := "cmr10";
defaultscale := 1.1;
color Blue,Teal,Orange,Green,Grey,Light,Wash;
Blue := (0,84/255,159/255); Teal := (0,177/255,183/255);
Orange := (246/255,168/255,0); Green := (87/255,171/255,39/255);
Grey := (100/255,101/255,103/255); Light := (236/255,236/255,237/255);
Wash := (232/255,241/255,250/255);
% Format displayed numbers to hundredths using integer strings.
vardef numlabel(expr value)=
 save n,whole,frac; numeric n,whole,frac;
 n:=round(100abs value); whole:=floor(n/100); frac:=n mod 100;
 (if value<-.005: "-" & fi decimal whole
  if frac>0: & "." & if frac<10: "0" & fi decimal frac fi)
enddef;
pickup pencircle scaled .9;
outputtemplate := "MPT-10-002.svg";
beginfig(1);
fill unitsquare xscaled 320 yscaled 220 withcolor white;
% BEGIN DRAWING
numeric xmin,xmax,ymin,ymax,xx,yy;
xmin:=0; xmax:=9; ymin:=0; ymax:=9;
vardef P(expr x,y)=
 (40+250*(x-xmin)/(xmax-xmin),35+145*(y-ymin)/(ymax-ymin))
enddef;
for i=0 upto 4:
 xx:=xmin+i*(xmax-xmin)/4; yy:=ymin+i*(ymax-ymin)/4;
 draw P(xx,ymin)--P(xx,ymax) withcolor Light;
 draw P(xmin,yy)--P(xmax,yy) withcolor Light;
 label.bot(numlabel(xx),P(xx,ymin));
 label.lft(numlabel(yy),P(xmin,yy));
endfor
draw P(xmin,ymax)--P(xmin,ymin)--P(xmax,ymin) withcolor Grey;
numeric v[];
v[1]:=2.1;
v[2]:=2.8;
v[3]:=4.2;
v[4]:=4.0;
v[5]:=5.6;
v[6]:=6.8;
v[7]:=6.6;
v[8]:=8.1;
numeric N,sx,sy,sxx,sxy,slope,intercept;
N:=8; sx:=sy:=sxx:=sxy:=0;
for i=1 upto N:
 sx:=sx+i; sy:=sy+v[i]; sxx:=sxx+i*i; sxy:=sxy+i*v[i];
 fill fullcircle scaled 5 shifted P(i,v[i]) withcolor Orange;
endfor
slope:=(N*sxy-sx*sy)/(N*sxx-sx*sx);
intercept:=(sy-slope*sx)/N;
draw P(0,intercept)--P(9,intercept+9slope) withcolor Blue;
label(btex Least-squares fit etex,(165,195));
message "check: slope="&decimal slope;
% END DRAWING
label.lft("MPT-10-002" infont "cmr8",(312,210));
setbounds currentpicture to unitsquare xscaled 320 yscaled 220;
endfig;
end.
