This question is related with this other question: Trying to model a simple second order ODE.
On this other question, I get some useful comments on why the simulations are so terrible.
However, I have another question: Why time-step smaller is not better? Thank you in advance.
h=0.05 %h=0.5;
max=100;
np=max/h;
u=[];
y=[];
u(1)=0;
u(2)=-h^2/2-h;
y(1)=0;
y(2)=-h^2/2-h;
x=[0:h:max];
for i=3:np+1;
u(i)=u(i-1)*(h+2)+u(i-2)*(-h-1)+h^2*x(i);
y(i)=-(x(i))^2/2-(x(i));
end
close all
plot(x,u)
hold on
plot(x,y,'r')