-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspline1.m
174 lines (133 loc) · 3.36 KB
/
spline1.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
clear('ivec', 'jvec', 'Mvec', 'Svec')
%% FEM with b-splines order 2
% use splines of degree 2 for C1 regularity
% basis
ref_spl = bspline([0 1 2]);
phi0 = ref_spl.coefs(2,:);
phi1 = ref_spl.coefs(1,:);
Dphi0 = polyder(phi0);
Dphi1 = polyder(phi1);
%% Calculations on the reference element
% mass matrix
ptemp = polyint(conv(phi0, phi0));
M00 = polyval(ptemp, 1) - polyval(ptemp, 0);
ptemp = polyint(conv(phi1, phi0));
M01 = polyval(ptemp, 1) - polyval(ptemp, 0);
ptemp = polyint(conv(phi1, phi1));
M11 = polyval(ptemp, 1) - polyval(ptemp, 0);
% stiffness matrix
ptemp = polyint(conv(Dphi0, Dphi0));
S00 = polyval(ptemp, 1) - polyval(ptemp, 0);
ptemp = polyint(conv(Dphi1, Dphi0));
S01 = polyval(ptemp, 1) - polyval(ptemp, 0);
ptemp = polyint(conv(Dphi1, Dphi1));
S11 = polyval(ptemp, 1) - polyval(ptemp, 0);
%% preparation for the assembly
% DOFs
N = M-1; % M+p-2=M+1-2; p-1 regularity
% cell volumes
C = x(2:end) - x(1:end-1);
% save some computations
Cinv = 1./C;
% nodes; only inner ones
nodes = x(2:end-1); % size(nodes) = 1, N
%% assembly of matrix
nnz = 1;
% left boundary
i=1;
ivec(nnz) = i;
jvec(nnz) = i;
Mvec(nnz) = C(i)*M00 + C(i+1)*M11;
Svec(nnz) = Cinv(i)*S00 + Cinv(i+1)*S11;
nnz = nnz+1;
ivec(nnz) = i;
jvec(nnz) = i+1;
Mvec(nnz) = C(i+1)*M01;
Svec(nnz) = Cinv(i+1)*S01;
nnz = nnz+1;
% inner nodes
for i=2:N-1
ivec(nnz) = i;
jvec(nnz) = i-1;
Mvec(nnz) = C(i)*M01;
Svec(nnz) = Cinv(i)*S01;
nnz = nnz+1;
ivec(nnz) = i;
jvec(nnz) = i;
Mvec(nnz) = C(i)*M00 + C(i+1)*M11;
Svec(nnz) = Cinv(i)*S00 + Cinv(i+1)*S11;
nnz = nnz+1;
ivec(nnz) = i;
jvec(nnz) = i+1;
Mvec(nnz) = C(i+1)*M01;
Svec(nnz) = Cinv(i+1)*S01;
nnz = nnz+1;
end
% right boundary
i = N;
ivec(nnz) = i;
jvec(nnz) = i-1;
Mvec(nnz) = C(i)*M01;
Svec(nnz) = Cinv(i)*S01;
nnz = nnz+1;
ivec(nnz) = i;
jvec(nnz) = i;
Mvec(nnz) = C(i)*M00 + C(i+1)*M11;
Svec(nnz) = Cinv(i)*S00 + Cinv(i+1)*S11;
nnz = nnz+1;
% deallocate memory
clear Cinv
%% assembly
A = sparse(ivec, jvec, Mvec+Svec);
% display system matrix
figure
spy(A);
title('System matrix for spl1');
%% compute right-hand side
rhovec = zeros(1,N);
switch rhs_calculation
case 'exact'
syms xi
% no need for distinction
for i=1:N
clear z
% first part of the spline
z = x(i) + xi*C(i); % transform
igrand = eval(rho)*xi;
handle = matlabFunction(igrand);
int_left = C(i)*integral(handle, 0, 1);
% second part of the spline
z = x(i+1) + xi*C(i+1);
igrand = eval(rho)*(1-xi);
handle = matlabFunction(igrand);
int_right = C(i)*integral(handle, 0, 1);
rhovec(i) = int_left + int_right;
end
case 'basis'
z = nodes;
Mass = sparse(ivec, jvec, Mvec);
rhovec = (Mass * eval(rho)')';
otherwise
error('rhs_calculation must be ''basis'' or ''exact''');
end
%% solve the system
u = A\rhovec';
u = [0 u' 0];
%% prepare output
u_000 = u(1:end-1);
pv0 = polyval(phi0, 0.25);
pv1 = polyval(phi1, 0.25);
u_025 = pv1*u(1:end-1) + pv0*u(2:end);
pv0 = polyval(phi0, 0.50);
pv1 = polyval(phi1, 0.50);
u_050 = pv1*u(1:end-1) + pv0*u(2:end);
pv0 = polyval(phi0, 0.75);
pv1 = polyval(phi1, 0.75);
u_075 = pv1*u(1:end-1) + pv0*u(2:end);
u_100 = u(2:end);
Du = (u(2:end) - u(1:end-1))./C;
Du_000 = Du;
Du_025 = Du;
Du_050 = Du;
Du_075 = Du;
Du_100 = Du;