Commit e542662f authored by Gidon Lucian Bauer's avatar Gidon Lucian Bauer 🏳
Browse files

Teil 1 fertig

parent b9cf8e1e
%% Finite Volume method
function [u] = FVM(u0, flux, f, dt, dx, tspan)
function [u, t] = FVM(u0, flux, f, dx, tspan)
xsize = length(u0);
tsize = (tspan(2) - tspan(1)) / dt;
u = zeros(xsize, tsize);
u = zeros(xsize, 1);
u(:, 1) = u0;
for n = 1:tsize
for j = 2:xsize-1
u(j,n+1) = u(j,n) + (dt/dx) * (flux(u(j-1,n), u(j,n), f, dt, dx) - flux(u(j,n), u(j+1,n), f, dt, dx));
t = [];
t(1) = tspan(1);
n = 1;
while t(end) < tspan(2)
%% Calc dt
dt = dx/max_der(f,u(:,n), 100);
t(n+1) = t(n) + dt;
for j = 1:xsize
%% Calc next value
if (j == 1)
u(j,n+1) = u(j,n) + (dt/dx) * (flux(u(end,n), u(j,n), f, dt, dx) - flux(u(j,n), u(j+1,n), f, dt, dx));
elseif (j == xsize)
u(j,n+1) = u(j,n) + (dt/dx) * (flux(u(j-1,n), u(j,n), f, dt, dx) - flux(u(j,n), u(1,n), f, dt, dx));
else
u(j,n+1) = u(j,n) + (dt/dx) * (flux(u(j-1,n), u(j,n), f, dt, dx) - flux(u(j,n), u(j+1,n), f, dt, dx));
end
end
n = n + 1;
end
end
\ No newline at end of file
......@@ -4,26 +4,83 @@
%% Clear the workspace
clear; clc; close all;
%% Functions for conservation law
% Advection
fa = @(u) 2 * u;
% Burgers
fb = @(u) (1/2) * u.^2;
%% Fluxfunctions
% Naive
F1 = @(ul, ur, f, ~, ~) (1/2)*(f(ul) + f(ur));
% Lax-Friedrichs
F2 = @(ul, ur, f, dt, dx) (1/2)*(f(ul) + f(ur)) - (dx/(2*dt)) * (ur - ul);
F3 = @(ul, ur, f, dt, dx) (1/2)*(f(ul) + f(ur)) - (dt/(2*dx)) * (ur - ul) * ((f(ul) - f(ur))/(ul - ur)).^2;
%% Function for conservation law
fa = @(u) 2 * u;
fb = @(u) (1/2) * u.^2;
% Lax-Wendroff
F3 = @lax_wendroff_flux;
%% Setup
dt = 0.01;
dx = 0.02;
dx = 0.01;
tspan = [0; 1];
t = (tspan(1):dt:tspan(2))';
x = (-1:dx:1)';
u0 = initial_cond(x);
u = FVM(u0, F2, fa, dt, dx, tspan);
%% Solving the advection equation
[u1a, t1a] = FVM(u0, F1, fa, dx, tspan);
[u2a, t2a] = FVM(u0, F2, fa, dx, tspan);
[u3a, t3a] = FVM(u0, F3, fa, dx, tspan);
figure(1);
tiledlayout(2,2);
nexttile;
pcolor(t1a,x,u1a);
xlabel('t');
ylabel('x');
title('Naive');
nexttile;
pcolor(t2a,x,u2a);
xlabel('t');
ylabel('x');
title('Lax-Friedrichs');
nexttile;
pcolor(t3a,x,u3a);
xlabel('t');
ylabel('x');
title('Lax-Wendroff');
%% Solving the burges equation
[u1b, t1b] = FVM(u0, F1, fb, dx, tspan);
[u2b, t2b] = FVM(u0, F2, fb, dx, tspan);
[u3b, t3b] = FVM(u0, F3, fb, dx, tspan);
figure(2);
tiledlayout(2,2);
nexttile;
pcolor(t1b,x,u1b);
xlabel('t');
ylabel('x');
title('Naive');
nexttile;
pcolor(t2b,x,u2b);
xlabel('t');
ylabel('x');
title('Lax-Friedrichs');
nexttile;
pcolor(t3b,x,u3b);
xlabel('t');
ylabel('x');
title('Lax-Wendroff');
mesh(t,x,u);
\ No newline at end of file
%% Lax-Wendroff Flux function
function [y] = lax_wendroff_flux(ul, ur, f, dt, dx)
if ((ul - ur) ~= 0)
a = ((f(ul) - f(ur))/(ul - ur)).^2;
else
a = 0;
end
y = (1/2)*(f(ul) + f(ur)) - (dt/(2*dx)) * (ur - ul) * a;
end
\ No newline at end of file
%% Calculate the max. absolut derivative
function [m] = max_der(func, u, N)
umax = max(u);
umin = min(u);
eps = (umax - umin)/N;
uline = umin:eps:umax;
f_der = abs(1/eps * diff(func(uline)));
m_ = max(f_der);
if m_ > 1e5
m = 1e5;
else
m = m_;
end
end
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment