I want to be able to simulate a hyperbolic equation on characteristic curves (lines). I will start with a basic one. u_{t}+2u_{x}=u^{2} with initial data u(x,0)=cos(x). The solution is u(x,t)=cos(x-2t)/(1-t*cos(x-2t)) where the characteristic curve is x=2*t+x_{0}. So the solution is defined on characteristics (method of characteristics).
x=zeros(10,5);
u=zeros(10,5);
x0=linspace(0,10,10);
t=linspace(0,5,5);
for i=1:length(x0)
for j=1:length(t)
x(i,j)=2*t(j)+x0(i);
if t(j)*cos(x(i,j)-2*t(j))==1
u(i,j)=0;
else
u(i,j)=cos(x(i,j)-2*t(j))/(1-t(j)*cos(x(i,j)-2*t(j)));
end
end
end
I'll be thankful if anyone can see my mistake.