I think that your loop solution is the best solution, though a few of your lines are unnecessary.
i=1;
i=i+1;
If you do want to use the bar graph, I would use a midpoint Riemann sum, though I do not know how to remove the outline along the bottom of the boxes.
If you want to implement this idea:
x = linspace(0,11);
y =((0.2*x).^3-0.2*x+0.5).*cos(0.13*x);
xa = linspace(0,10,11);
xb = xa +1;
yab = ((0.2*xa).^3-0.2*xa+0.5).*cos(0.13*xa);
g = bar(xa,yab,'histc'); % Moves labels to the left
g.FaceColor = 'none'; % Changes the fill of bars
g.EdgeColor = 'b'; % Changes edge color of bars
hold on
plot(x,y);
axis tight % Sets axis to just fit the data
The output looks like this:
If you use the loop way, there will be no line along the x axis, though there is less to type here.