Upload files to "Sheet4"

MatLab file, Exercise 3
This commit is contained in:
Lisa Pizzo 2025-11-24 10:13:00 +01:00
commit cde584f721

38
Sheet4/Ex3.m Normal file
View file

@ -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');