Numerical Methods with MATLAB - A linear hyperbolic system

212 Views Asked by At

I am studying Numerical Methods for Conservations Laws with MATLAB by first time and I've tried to follow an example and calculate the solutions for the following Riemann Problem:

$$\begin{bmatrix}u\\ v \\ w\end{bmatrix}_t+\begin{bmatrix}1&0&0\\ 0&1&0\\ 0&0&2\end{bmatrix}\begin{bmatrix}u\\ v \\ w\end{bmatrix}_x=0$$

$$U_l=\begin{bmatrix}0\\ 1 \\ 1\end{bmatrix}\qquad U_r=\begin{bmatrix}1\\ 1 \\ 2\end{bmatrix}$$

Well, I tried the following code, but I got an error at line 38

unpl(i)=u(i)-dt/dx*v*(u(i)-u(i-1));

The system does not say the error and I cannot see what is it.

Many thanks for any help.

%clear workspaces
clear
clc

% define variables
xmin=-10;      % minimum value of x
xmax=10;       % maximum value of x
N=100;         % no. nodes - 1
dt=0.005;      % timestep
t=0;           % time
tmax=10;       % maximum value of time
v = [1 0 0; 0 1 0; 0 0 2]

% discretise the domain
dx= (xmax-xmin)/N;
x = xmin -dx : dx : xmax + dx;

% initial conditions
for x=xmin:dx:xmax  
if x<0
    u0=[0 1 1]
else 
    u0=[1 1 2]
end
end

u=u0;
unpl=u0;

% loop through time
nsteps= tmax/dt;
for n=1 : nsteps
    
    
    
    % calculate the FOU scheme
    for i = 2 : N+2
        unpl(i)=u(i)-dt/dx*v*(u(i)-u(i-1));
    end
    
    % update t and u
    t=t+dt;
    u=unpl;
    

    % plot solution
    plot(x,u,'bo-','markerfacelor','b');
    shg;
    pause(dt);
    
end

Edit (the comand window)

v =

     1     0     0
     0     1     0
     0     0     2


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =
     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     0     1     1


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =
     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2


u0 =

     1     1     2

Unable to perform assignment because the left and right sides have a different number of elements.

Error in testUntitled (line 38)
        unpl(i)=u(i)-dt/dx*v*(u(i)-u(i-1));
 
1

There are 1 best solutions below

4
On BEST ANSWER

The upwind scheme is fine. The code below is improved. There were various syntax errors on data types (scalars, vectors, matrices).

%clear workspaces
clear
clc

% define variables
xmin=-10;      % minimum value of x
xmax=10;       % maximum value of x
N=100;         % no. nodes - 1
dt=0.005;      % timestep
t=0;           % time
tmax=10;       % maximum value of time
v = [1 0 0; 0 1 0; 0 0 2];

% discretise the domain
dx= (xmax-xmin)/N;
x = xmin -dx : dx : xmax + dx;
u0 = [x;x;x];

% initial conditions
for i=1:length(x)  
if x(i)<0
    u0(:,i)=[0; 1; 1];
else 
    u0(:,i)=[1; 1; 2];
end
end

u=u0;
unpl=u0;

fig = plot(x,u(1,:),'bo-');
tit = title('n =0');

% loop through time
nsteps= floor(tmax/dt);
for n=1 : nsteps



    % calculate the FOU scheme
    for i = 2 : N+2
        unpl(:,i)=u(:,i)-dt/dx*v*(u(:,i)-u(:,i-1));
    end

    % update t and u
    t=t+dt;
    u=unpl;


    % plot solution
    set(fig,'YData',u(1,:));
    set(tit,'String',strcat('n =',num2str(n)));
    drawnow;
end

Output:

output