set(0, "defaultlinelinewidth", 5);
set (0, "defaulttextfontname", "TimesNewRoman")
set (0, "defaulttextfontsize", 20)
set (0, "DefaultAxesFontName", "TimesNewRoman")
set(0, 'DefaultAxesFontSize', 20)
xe = linspace(0,2, 6);
A = 1; B = -sinh(2)/cosh(2);
ye = A*cosh(xe) + B*sinh(xe);
plot(xe, ye); legend("Exact soln");
N = 10;
x = linspace(0,2,N); dx = x(2) - x(1);
A1 = zeros(N,N); b = zeros(N,1);
A2 = zeros(N,N);
alpha = -2 - dx.^2;
dv = alpha*ones(N,1);
dv1 = ones(N-1,1);
A1 = diag(dv) + diag(dv1,1) + diag(dv1,-1);
A1(1,:) = 0; A1(N,:) = 0;
A2 = diag(dv) + diag(dv1,1) + diag(dv1,-1);
A2(1,:) = 0; A2(N,:) = 0;
A1(1,1) = 1; A1(N,N-1) = -1; A1(N,N) = 1;
A2(N,N) = alpha; A2(N,N-1) = 2; A2(1,1) = 1;
b(1) = 1;
f = (inv(A1)*b)';
g = (inv(A2)*b)';
plot(x, f, x, g, xe, ye,'ok');
xlabel("x"); ylabel("f(x)");
h=legend ("First order BC", "Second Order", "Exact Soln");
N = 10;
x = linspace(0,2,N); dx = x(2) - x(1);
A1 = zeros(N, N); b = zeros(N,1);
alpha = -2 - dx^2;
dv = alpha*ones(N,1);
dv1 = ones(N-1,1);
A1 = diag(dv) + diag(dv1,1) + diag(dv1,-1);
A1(1,:) = 0; A1(N,:) = 0;
A1(1,1) = 1; A2(1,1) = 1;
A1(end,end-1) = -1; A1(end,end) = 1;
b(1) = 1;
As = sparse(A1);
f =As*b
f = 1 1 0 0 0 0 0 0 0 0
As = sparse(A1)
f = As\b
As = Compressed Column Sparse (rows = 10, cols = 10, nnz = 27 [27%]) (1, 1) -> 1 (2, 1) -> 1 (2, 2) -> -2.0494 (3, 2) -> 1 (2, 3) -> 1 (3, 3) -> -2.0494 (4, 3) -> 1 (3, 4) -> 1 (4, 4) -> -2.0494 (5, 4) -> 1 (4, 5) -> 1 (5, 5) -> -2.0494 (6, 5) -> 1 (5, 6) -> 1 (6, 6) -> -2.0494 (7, 6) -> 1 (6, 7) -> 1 (7, 7) -> -2.0494 (8, 7) -> 1 (7, 8) -> 1 (8, 8) -> -2.0494 (9, 8) -> 1 (8, 9) -> 1 (9, 9) -> -2.0494 (10, 9) -> -1 (9, 10) -> 1 (10, 10) -> 1 f = 1.00000 0.81118 0.66241 0.54636 0.45729 0.39080 0.34361 0.31338 0.29864 0.29864
plot(x, f, "-;First order BC;");
hold on;
plot(xe, ye, "-;Exact;",'Marker',"o","linestyle","none");
xlabel("x"); ylabel("f(x)");
legend;
hold off;