38 lines
800 B
Matlab
38 lines
800 B
Matlab
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');
|