0

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.

talonmies
  • 70,661
  • 34
  • 192
  • 269
user1018331
  • 143
  • 4
  • 14

1 Answers1

2

I am not certain exactly if this solves your problem but you are accessing elements of your t matrix using the i and j indices. For example:

  (1-t(i)*cos(x(i)-2*t(j))

Since i can take values beyond the range of your t array, it is possible that you are mixing up your indices. At the very least you could be getting errors from accessing an element of t that is beyond its range. It is hard to say as you don't specify an error. I am just pointing out a potential problem.

It seems to me that you should use i to access elements of x0 and j to access elements of t since these are within the limits you specify in your loops. Maybe

for i=1:length(x0)
    for j=1:length(t)
        x(i,j)=2*t(j)+x0(i);
        if t(j)*cos(x(i)-2*t(j))==1
            u(i,j)=0;
        else
            u(i,j)=cos(x(i)-2*t(j))/(1-t(j)*cos(x(i)-2*t(j)));
        end
    end
end

is what you are looking for.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
  • thank you very much for your answer. When I plot u(mesh u), I get a funny figur, which I does not want to see. Do you think that I also have a grid problem: from characteristic lines to rectangular grid? – user1018331 Jul 13 '12 at 22:01
  • 2
    @user1018331 I cannot say for sure. If you have a different problem with the plotting it is probably better to open a new question and provide specific details of the new problem. Otherwise it can become confusing. Maybe including some image of your plot in the new question if it helps – mathematician1975 Jul 13 '12 at 22:04