Sie sind auf Seite 1von 9

Lax fredricks scheme

Code
clc;clear all;close all;
% lambda = 0.5; % lambda = c*dt/dx
c = 1.0;
iplot = 1;
myinit = 1; % Initial condition, 1 = step func, 2 = sine wave
my_scheme = 3;
nx = [100];
dt = 0.0015;
L = 1; % Length of domain
for kk = 1:length(nx)
dx(kk) = L/nx(kk); % Cell/grid size
lambda = (c*dt)/dx;
time = 1.2; % alloted time in the x axis
nt = time/dt; % number of time steps
nxp1 = nx(kk)+1; % Ghost cell
nxp2 = nx(kk)+2; % Ghost cell
a1 = (2*L/5)/dx(kk); % Intervals definition (Unit step function)
b1 = fix((L/3)/dx(kk)); % Intervals definition (Sine wave)
% ------------------------------------------------------------------------%
% Initial condition %
% ------------------------------------------------------------------------%
if myinit == 1
u_sol = [zeros(1,a1+1) ones(1,a1/2) zeros(1,a1)]
end
% ------------------------------------------------------------------------%
x = 0:dx(kk):L; % Grid points on x-axis
x_Ex = x;
u_Ex = u_sol; % Save the exact soltion
xmin = 0; % Define x & y - limits
xmax = L;
if myinit == 1
ymin = -0.5*max(u_sol);
elseif myinit == 2
ymin = -1.5*max(u_sol);
end
ymax = 1.5*max(u_sol);
% ------------------------------------------------------------------------%
% Start time loop and code the numerical discretizations %
% ------------------------------------------------------------------------%
for it = 1:nt
x_Ex = mod(x_Ex+lambda*dx(kk),L-dx(kk)); % Move x-axis for exact sol.
uim1 = u_sol(1);
uim2 = u_sol(2);
uim3 = u_sol(3);
%------------------------------------------------------------------------%
if my_scheme == 1
% Forward Time Backward Space (FTBS) -- Upwind.
% ------------------------------------------------------------------------%
for ix = 2:nx(kk)
tmp1 = u_sol(ix);
u_sol(ix) = u_sol(ix) - lambda*(u_sol(ix) - uim1);
uim1 = tmp1;
end
% ------------------------------------------------------------------------%
elseif my_scheme == 2
% Forward Time Central Space (FTCS)
% ------------------------------------------------------------------------%
%
%
% ------------------------------------------------------------------------%
elseif my_scheme == 3
% Lax-Friedrichs Scheme
% ------------------------------------------------------------------------%
for c = 2:nx(kk)
mamoon = u_sol(c);
mamoon2 = u_sol(c+1);
u_sol(c) = 1/2*(uim1+uim3)-lambda/2*(uim3-uim1);
uim1 = mamoon;
uim3 = mamoon2;
end
% ------------------------------------------------------------------------%
elseif my_scheme == 4
% Lax-Wendroff Scheme
% ------------------------------------------------------------------------%
for ix2 = 2:nx(kk)
tmp2 = u_sol(ix2);
tmp3 = u_sol(ix2+1);
u_sol(ix2) = u_sol(ix2) - lambda*(uim3-uim1)+1/2*lambda^2*(uim3+uim1-
2*u_sol(ix2));
uim1 = tmp2;
uim3 = tmp3;
end
% ------------------------------------------------------------------------%
elseif my_scheme == 5
% Beam-Warming Scheme
% ------------------------------------------------------------------------%
for ix2 = 3:nx(kk)
tmp2 = u_sol(ix2-1); % u(i-2)
tmp3 = u_sol(ix2); % u(i-1)
u_sol(ix2) = u_sol(ix2) - lambda/2*(3*u_sol(ix2)-
4*uim1+4*uim2)+1/2*lambda^2*(u_sol(ix2)+uim2-2*uim1);
uim1 = tmp2;
uim2 = tmp3;
end
% ------------------------------------------------------------------------%
%
%
end
% ------------------------------------------------------------------------%
% Plot results %
% ------------------------------------------------------------------------%
figure(kk);
if mod(it-1,iplot) == 0
plot(x(1:length(x)-1),u_sol(1:nx(kk)),'b-',...
x_Ex(1:length(x_Ex)-1),u_Ex(1:nx(kk)),'r-',...
'LineWidth',3,'MarkerSize',6)
if my_scheme == 1
legend('FTBS','Exact','location','northwest')
elseif my_scheme == 2
legend('FTCS','Exact','location','northwest')
elseif my_scheme == 3
legend('Lax-Friedrichs','Exact','location','northwest')
elseif my_scheme == 4
legend('Lax-Wendroff','Exact','location','northwest')
elseif my_scheme == 5
legend('Beam-Warming','Exact','location','northwest')
end
axis([xmin xmax ymin ymax]);
set(gca,'FontSize',24,'linewidth',2)
xlabel('x','FontSize',24)
ylabel('u','FontSize',24)
drawnow;
% pause(1);
% ginput(1);
end
end
i_tmp=round(x_Ex(1)*(nx(kk)/L));
% i_tmp=10;
u_tmp=u_Ex;
u_Ex(1:i_tmp)=u_tmp(nx(kk)-(i_tmp-1):nx(kk));
u_Ex(i_tmp+1:nx(kk))=u_tmp(1:(nx(kk)-i_tmp));
error(kk) = (L/nx(kk))*sqrt(sum((u_sol-u_Ex).^2));
end
x1=[.02 .2];
y1=[.02 .2].^2;
Plot

Lax wendroff Scheme


Code
clc;clear all;close all;
% lambda = 0.5; % lambda = c*dt/dx
c = 1.0;
iplot = 1;
myinit = 1; % Initial condition, 1 = step func, 2 = sine wave
my_scheme = 4;
nx = [100];
dt = 0.0015;
L = 1; % Length of domain
for kk = 1:length(nx)
dx(kk) = L/nx(kk); % Cell/grid size
lambda = (c*dt)/dx;
time = 1.2; % alloted time in the x axis
nt = time/dt; % number of time steps
nxp1 = nx(kk)+1; % Ghost cell
nxp2 = nx(kk)+2; % Ghost cell
a1 = (2*L/5)/dx(kk); % Intervals definition (Unit step function)
b1 = fix((L/3)/dx(kk)); % Intervals definition (Sine wave)
% ------------------------------------------------------------------------%
% Initial condition %
% ------------------------------------------------------------------------%
if myinit == 1
u_sol = [zeros(1,a1+1) ones(1,a1/2) zeros(1,a1)]
end
% ------------------------------------------------------------------------%
x = 0:dx(kk):L; % Grid points on x-axis
x_Ex = x;
u_Ex = u_sol; % Save the exact soltion
xmin = 0; % Define x & y - limits
xmax = L;
if myinit == 1
ymin = -0.5*max(u_sol);
elseif myinit == 2
ymin = -1.5*max(u_sol);
end
ymax = 1.5*max(u_sol);
% ------------------------------------------------------------------------%
% Start time loop and code the numerical discretizations %
% ------------------------------------------------------------------------%
for it = 1:nt
x_Ex = mod(x_Ex+lambda*dx(kk),L-dx(kk)); % Move x-axis for exact sol.
uim1 = u_sol(1);
uim2 = u_sol(2);
uim3 = u_sol(3);
%------------------------------------------------------------------------%
if my_scheme == 1
% Forward Time Backward Space (FTBS) -- Upwind.
% ------------------------------------------------------------------------%
for ix = 2:nx(kk)
tmp1 = u_sol(ix);
u_sol(ix) = u_sol(ix) - lambda*(u_sol(ix) - uim1);
uim1 = tmp1;
end
% ------------------------------------------------------------------------%
elseif my_scheme == 2
% Forward Time Central Space (FTCS)
% ------------------------------------------------------------------------%
%
%
% ------------------------------------------------------------------------%
elseif my_scheme == 3
% Lax-Friedrichs Scheme
% ------------------------------------------------------------------------%
for mam = 2:nx(kk)
z = u_sol(mam);
y = u_sol(tmam+1);
u_sol(ix2) = 1/2*(uim1+uim3)-lambda/2*(uim3-uim1);
uim1 = z;
uim3 = y;
end
% ------------------------------------------------------------------------%
elseif my_scheme == 4
% Lax-Wendroff Scheme
% ------------------------------------------------------------------------%
for mam = 2:nx(kk)
tmp2 = u_sol(mam);
tmp3 = u_sol(mam+1);
u_sol(mam) = u_sol(mam) - lambda*(uim3-uim1)+1/2*lambda^2*(uim3+uim1-
2*u_sol(mam));
uim1 = tmp2;
uim3 = tmp3;
end
% ------------------------------------------------------------------------%
elseif my_scheme == 5
% Beam-Warming Scheme
% ------------------------------------------------------------------------%
for ix2 = 3:nx(kk)
tmp2 = u_sol(ix2-1); % u(i-2)
tmp3 = u_sol(ix2); % u(i-1)
u_sol(ix2) = u_sol(ix2) - lambda/2*(3*u_sol(ix2)-
4*uim1+4*uim2)+1/2*lambda^2*(u_sol(ix2)+uim2-2*uim1);
uim1 = tmp2;
uim2 = tmp3;
end
% ------------------------------------------------------------------------%
%
%
end
% ------------------------------------------------------------------------%
% Plot results %
% ------------------------------------------------------------------------%
figure(kk);
if mod(it-1,iplot) == 0
plot(x(1:length(x)-1),u_sol(1:nx(kk)),'b-',...
x_Ex(1:length(x_Ex)-1),u_Ex(1:nx(kk)),'r-',...
'LineWidth',3,'MarkerSize',6)
if my_scheme == 1
legend('FTBS','Exact','location','northwest')
elseif my_scheme == 2
legend('FTCS','Exact','location','northwest')
elseif my_scheme == 3
legend('Lax-Friedrichs','Exact','location','northwest')
elseif my_scheme == 4
legend('Lax-Wendroff','Exact','location','northwest')
elseif my_scheme == 5
legend('Beam-Warming','Exact','location','northwest')
end
axis([xmin xmax ymin ymax]);
set(gca,'FontSize',24,'linewidth',2)
xlabel('x','FontSize',24)
ylabel('u','FontSize',24)
drawnow;
% pause(1);
% ginput(1);
end
end
i_tmp=round(x_Ex(1)*(nx(kk)/L));
% i_tmp=10;
u_tmp=u_Ex;
u_Ex(1:i_tmp)=u_tmp(nx(kk)-(i_tmp-1):nx(kk));
u_Ex(i_tmp+1:nx(kk))=u_tmp(1:(nx(kk)-i_tmp));
error(kk) = (L/nx(kk))*sqrt(sum((u_sol-u_Ex).^2));
end
x1=[.02 .2];
y1=[.02 .2].^2;
Plot

Beam warming scheme


Code
clc;clear all;close all;
% lambda = 0.5; % lambda = c*dt/dx
c = 1.0;
iplot = 1;
myinit = 1; % Initial condition, 1 = step func, 2 = sine wave
my_scheme = 5;
nx = [100];
dt = 0.0015;
L = 1; % Length of domain
for kk = 1:length(nx)
dx(kk) = L/nx(kk); % Cell/grid size
lambda = (c*dt)/dx;
time = 1.2; % alloted time in the x axis
nt = time/dt; % number of time steps
nxp1 = nx(kk)+1; % Ghost cell
nxp2 = nx(kk)+2; % Ghost cell
a1 = (2*L/5)/dx(kk); % Intervals definition (Unit step function)
b1 = fix((L/3)/dx(kk)); % Intervals definition (Sine wave)
% ------------------------------------------------------------------------%
% Initial condition %
% ------------------------------------------------------------------------%
if myinit == 1
u_sol = [zeros(1,a1+1) ones(1,a1/2) zeros(1,a1)]
end
% ------------------------------------------------------------------------%
x = 0:dx(kk):L; % Grid points on x-axis
x_Ex = x;
u_Ex = u_sol; % Save the exact soltion
xmin = 0; % Define x & y - limits
xmax = L;
if myinit == 1
ymin = -0.5*max(u_sol);
elseif myinit == 2
ymin = -1.5*max(u_sol);
end
ymax = 1.5*max(u_sol);
% ------------------------------------------------------------------------%
% Start time loop and code the numerical discretizations %
% ------------------------------------------------------------------------%
for it = 1:nt
x_Ex = mod(x_Ex+lambda*dx(kk),L-dx(kk)); % Move x-axis for exact sol.
uim1 = u_sol(1);%positioning of cells for moving forwared in space
uim2 = u_sol(2);
uim3 = u_sol(3);
%------------------------------------------------------------------------%
if my_scheme == 1
% Forward Time Backward Space (FTBS) -- Upwind.
% ------------------------------------------------------------------------%
for ix = 2:nx(kk)
tmp1 = u_sol(ix);
u_sol(ix) = u_sol(ix) - lambda*(u_sol(ix) - uim1);
uim1 = tmp1;
end
% ------------------------------------------------------------------------%
elseif my_scheme == 2
% Forward Time Central Space (FTCS)
% ------------------------------------------------------------------------%
%
%
% ------------------------------------------------------------------------%
elseif my_scheme == 3
% Lax-Friedrichs Scheme
% ------------------------------------------------------------------------%
for ix2 = 2:nx(kk)
tmp2 = u_sol(ix2);
tmp3 = u_sol(ix2+1);
u_sol(ix2) = 1/2*(uim1+uim3)-lambda/2*(uim3-uim1);
uim1 = tmp2;
uim3 = tmp3;
end
% ------------------------------------------------------------------------%
elseif my_scheme == 4
% Lax-Wendroff Scheme
% ------------------------------------------------------------------------%
for ix2 = 2:nx(kk)
tmp2 = u_sol(ix2);
tmp3 = u_sol(ix2+1);
u_sol(ix2) = u_sol(ix2) - lambda*(uim3-uim1)+1/2*lambda^2*(uim3+uim1-
2*u_sol(ix2));
uim1 = tmp2;
uim3 = tmp3;
end
% ------------------------------------------------------------------------%
elseif my_scheme == 5
% Beam-Warming Scheme
% ------------------------------------------------------------------------%
for k = 3:nx(kk)
tnm = u_sol(k-1); % u(i-2)
tnm23 = u_sol(k); % u(i-1)
u_sol(k) = u_sol(k) - lambda/2*(3*u_sol(k)-
4*uim1+4*uim2)+1/2*lambda^2*(u_sol(k)+uim2-2*uim1);
uim1 = tnm;
uim2 = tnm23;
end
% ------------------------------------------------------------------------%
%
%
end
% ------------------------------------------------------------------------%
% Plot results %
% ------------------------------------------------------------------------%
figure(kk);
if mod(it-1,iplot) == 0
plot(x(1:length(x)-1),u_sol(1:nx(kk)),'b-',...
x_Ex(1:length(x_Ex)-1),u_Ex(1:nx(kk)),'r-',...
'LineWidth',3,'MarkerSize',6)
if my_scheme == 1
legend('FTBS','Exact','location','northwest')
elseif my_scheme == 2
legend('FTCS','Exact','location','northwest')
elseif my_scheme == 3
legend('Lax-Friedrichs','Exact','location','northwest')
elseif my_scheme == 4
legend('Lax-Wendroff','Exact','location','northwest')
elseif my_scheme == 5
legend('Beam-Warming','Exact','location','northwest')
end
axis([xmin xmax ymin ymax]);
set(gca,'FontSize',24,'linewidth',2)
xlabel('x','FontSize',24)
ylabel('u','FontSize',24)
drawnow;
% pause(1);
% ginput(1);
end
end
i_tmp=round(x_Ex(1)*(nx(kk)/L));
% i_tmp=10;
u_tmp=u_Ex;
u_Ex(1:i_tmp)=u_tmp(nx(kk)-(i_tmp-1):nx(kk));
u_Ex(i_tmp+1:nx(kk))=u_tmp(1:(nx(kk)-i_tmp));
error(kk) = (L/nx(kk))*sqrt(sum((u_sol-u_Ex).^2));
end
x1=[.02 .2];
y1=[.02 .2].^2;

Plot

Das könnte Ihnen auch gefallen