# Draw parabola with MetaPost

I keep getting this error: "! A tertiary expression can't begin with ['."

beginfig(1);
numeric u,a,x;
vardef yb(expr x)=
x*x-6*x+8;
enddef;
pair igreka[],igrekb[];
path paraboola,paraboolb;
u := 1cm;
drawarrow (-4u,0u)--(6u,0u);
drawarrow (0u,-4u)--(0u,9u);
for i=0 upto 16:
igreka[i]:=((i/2)*u,((i/2)**2-6*(i/2)+8)*u);
endfor;
paraboola:=igreka[0]..igreka[1]..igreka[2]..igreka[3]..[igreka[4]..igreka[9]..igreka[10]..igreka[11]..igreka[13]..igreka[14]..igreka[15]..igreka[16];
draw paraboola;
endfig;


I'm trying to draw a parabola, but I'm not able to draw a path with the list shown in my code, the list igreka[].

I'd also like to know if there's a way to define the path of the parabola in some easier way via some for cycle such that:

beginfig(1);
numeric u,a,x;
vardef yb(expr x)=
x*x-6*x+8;
enddef;
pair igreka[],igrekb[];
path paraboola,paraboolb;
u := 1cm;
drawarrow (-4u,0u)--(6u,0u);
drawarrow (0u,-4u)--(0u,9u);
for i=0 upto 16:
igreka[i]:=((i/2)*u,((i/2)**2-6*(i/2)+8)*u);
endfor;
EXAMPLE HERE: for i=1 upto 16:
paraboola:=igrek[1]..igrek[2].. ..igrek[16];
endfor;
draw paraboola;
endfig;


The first issue was just a typo: you wrote [igreka[4] instead of igreka[4]. So MetaPost complains that you shouldn't begin your expression with a [.

MetaPost, similar to TeX, is a macro based language. Therefore control structures like for are not restricted to have full statements in their body, their body can also contain partial statement like .. igreka[i].

beginfig(1);
numeric u,a,x;
vardef yb(expr x)=
x*x-6*x+8;
enddef;
pair igreka[],igrekb[];
path paraboola,paraboolb;
u := 1cm;
drawarrow (-4u,0u)--(6u,0u);
drawarrow (0u,-4u)--(0u,9u);
for i=0 upto 16:
igreka[i]:=((i/2)*u,((i/2)**2-6*(i/2)+8)*u);
endfor;
paraboola := igreka[0]
for i=1 upto 16: .. igreka[i] endfor;
draw paraboola;
endfig;
bye;


I know this is old, and I see that the original answer fixed the issues in the OP, but in case anyone comes here hoping to find a good way to “draw a parabola with Metapost”, I thought it might be worth adding another solution, that is perhaps a bit more generic.

This is wrapped up in luamplib and makes use of the mplibtextextlabel magic, so you need to compile it with lualatex.

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);

path ff;  % first the function and its curve
vardef f(expr x) = x * x - 6 * x + 8 enddef;
ff = (0, f(0)) for x = 1 upto 12: .. (x, f(x)) endfor;

% scale the curve nicely in each dimension
numeric u, v; u = 20; v = 2;
ff := ff xscaled u yscaled v;

% this is just one way to make nice axes
path xx, yy;
bboxmargin := 20pt;
xx = subpath (0, 1) of bbox ff;
xx := xx shifted (0, -ypart point 0 of xx);
yy = subpath (4, 3) of bbox ff;
yy := yy shifted (-xpart point 0 of yy, 0);

% draw and label the function curve
draw ff withcolor 2/3 red;
label.top("$y=x^2 - 6x + 8$", point infinity of ff)
withcolor 2/3 red;

% draw and label the axes
drawarrow xx; label.rt("$x$", point 1 of xx);
drawarrow yy; label.top("$y$", point 1 of yy);

% add some scales along the axes
for x = 2 step 2 until 12:
draw (x * u, 0) -- (x * u, -3);
label.bot("$" & decimal x & "$", (x * u, -3));
endfor
for y = 20 step 20 until 80:
draw (0, y * v) -- (-3, y * v);
label.lft("$" & decimal y & "$", (-3, y * v));
endfor

endfig;
\end{mplibcode}
\end{document}
`