MatLab file, Exercise 3
This commit is contained in:
parent
a05af527f5
commit
3ec7c4d75b
1 changed files with 38 additions and 0 deletions
38
Sheet4/Ex3.m
Normal file
38
Sheet4/Ex3.m
Normal 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');
|
||||
Loading…
Add table
Add a link
Reference in a new issue