From cde584f7217b2098abb079505d37e86cfad22328 Mon Sep 17 00:00:00 2001 From: "lisa.pizzo" Date: Mon, 24 Nov 2025 10:13:00 +0100 Subject: [PATCH] Upload files to "Sheet4" MatLab file, Exercise 3 --- Sheet4/Ex3.m | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Sheet4/Ex3.m diff --git a/Sheet4/Ex3.m b/Sheet4/Ex3.m new file mode 100644 index 0000000..f2ac85f --- /dev/null +++ b/Sheet4/Ex3.m @@ -0,0 +1,38 @@ +clear; close all; clc; +p = 70; +Nvals = [10 20 30 40 70]; + +figure; hold on; box on; + +for N = Nvals + h = 1/N; + x = linspace(0,1,N+1)'; + A = zeros(N-1); + b = zeros(N-1,1); + + for i = 1:N-1 + A(i,i) = 2/h; + + if i > 1 + A(i,i-1) = -1/h - p/2; + end + + if i < N-1 + A(i,i+1) = -1/h + p/2; + end + end + b(end) = (1/h - p/2); + uh_inner = A\b; + uh = [0; uh_inner; 1]; + + plot(x, uh, 'o-', 'LineWidth', 2, ... + 'DisplayName', ['FEM, N=', num2str(N)]); +end + +x_exact = linspace(0,1,2000)'; +u_exact = (exp(p*x_exact) - 1)/(exp(p) - 1); +plot(x_exact, u_exact, 'k', 'LineWidth', 1, 'DisplayName', 'Exact'); + +title("FEM vs Analytical Solution for -u'' + p u' = 0, p=70"); +xlabel('x'); ylabel('u(x)'); +legend('Location','best');