Hyperbolicsnonlinear
Hyperbolicsnonlinear
clear all;
clearvars;
x=0:0.1:4;
length=4;
dx=0.1;
nx=(length/dx)+1;
for i=1:nx
if x(i)<=2
u(i)=1;
else
u(i)=0;
end
end
plot(x,u);
hold on;
un=u;
uold=u;
while(1)
fprintf("Processes available:\n")
fprintf("1:Lax Explicit scheme:\n")
fprintf("2:Lax-wendroff scheme:\n")
fprintf("3:Maccormack Explicit scheme:\n")
fprintf("exit for any other number\n")
z=input('specify the process number:');
switch(z)
case 1
disp("Lax Explicit scheme")
case 2
disp("Lax-wendroff scheme")
case 3
disp("Maccormack Explicit scheme")
otherwise
disp("invalid number")
return;
end
dt=input("Enter the time step:");
c=dt/dx;
time=1.8;
n=time/dt;
for i=1:nx
eold(i)=0.5*uold(i)*uold(i);
end
e=eold;
for iterations=1:n
switch(z)
case 1
for i=2:nx-1
1
un(i)=0.5*(u(i+1)+u(i-1))-0.25*c*(u(i+1)*u(i+1)-
u(i-1)*u(i-1));
end
case 2
for i=2:nx-1
e(i)=0.5*u(i)*u(i);
e(i+1)=0.5*u(i+1)*u(i+1);
e(i-1)=0.5*u(i-1)*u(i-1);
un (i)=u(i)-0.5*c*(e(i+1)-e(i-1));
un (i)=un(i)+0.25*c*c*((u(i+1)+u(i))*(e(i+1)-e(i)) -
(u(i)-u(i-1)) *(e(i)-e(i-1)));
end
case 3
for i=2:nx-1
eold(i)=0.5*uold(i)*uold(i);
eold(i+1)=0.5*uold(i+1)*uold(i+1);
u(i)=uold(i)-c*(eold(i)-eold(i-1));
end
for i=2:nx-1
e(i)=0.5*u(i)*u(i);
e(i-1)=0.5*u(i-1)*u(i-1);
un(i)=0.5*(uold(i)+u(i))-0.5*c*(e(i+1)-e(i));
end
uold=u;
u=un;
otherwise
return;
end
u=un;
if rem(iterations,2)==0
plot(x,un);
end
hold on;
end
end
Processes available:
1:Lax Explicit scheme:
2:Lax-wendroff scheme:
3:Maccormack Explicit scheme:
exit for any other number
Lax Explicit scheme
2
Processes available:
1:Lax Explicit scheme:
2:Lax-wendroff scheme:
3:Maccormack Explicit scheme:
exit for any other number
invalid number