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

Programmieraufgabe 3

parent a093a983
%% Finite Volume method
function [u] = FVM(u0, flux, f, dt, dx, tspan)
xsize = length(u0);
tsize = (tspan(2) - tspan(1)) / dt;
u = zeros(xsize, tsize);
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));
end
end
end
\ No newline at end of file
%% Mathe V - Progammieraufgabe 03
% Stefan Basermann, Adrien Bordes & Gidon Bauer
%% Clear the workspace
clear; clc; close all;
%% Fluxfunctions
F1 = @(ul, ur, f, ~, ~) (1/2)*(f(ul) + f(ur));
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;
%% Setup
dt = 0.01;
dx = 0.02;
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);
mesh(t,x,u);
\ No newline at end of file
%% Periodic initial conditions
function [y] = initial_cond(x)
y = ones(size(x));
for i = 1:length(x)
if (x(i) >= -0.5 && x(i) < 0.5)
y(i) = 0;
end
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