-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathIntEqSolver1d.m
More file actions
298 lines (250 loc) · 8.77 KB
/
IntEqSolver1d.m
File metadata and controls
298 lines (250 loc) · 8.77 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
function [lambda,phi] = IntEqSolver1d(a,b,n,Cd,neig,method,p,ToPlot)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% returns solution of the Fredholm integral equation of the second kind
% [a,b] - interval of integration
% n - grid size = total number of nodes including endpoints
% neig - number of eigenpairs required
%
% method: 'collocation' or 'galerkin'
% p - degree of the polynomials in Lagrange basis: either 1 or 2
%
% varargin - optional list of arguments
% contains parameters for the kernels that need it: eta, sigma
%
% examples of run:
% [lambda,phi] = IntEqSolver1d(0,1,50,10,'exponential','collocation',2,1,1/10,1)
% Written by Veronika Vasylkivska <vasylkiv@math.oregonstate.edu>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
warning off all
h = (b-a)/(n-1);
if p == 1 %% piecewise linear functions are used for basis
nodes = a:h:b; %% grid nodes
nnodes = n;
elseif p == 2 %% piecewise quadratic functions are used for basis
nodes = a:h/2:b; %% grid nodes
nnodes = 2*n-1;
else
error('Not valid degree of polynomials: should be 1 or 2');
end
% find matrices for the generalized eigenvalue problem
% using one of the methods: collocation or Galerkin
D = zeros(nnodes,nnodes);
switch lower(method)
case 'collocation'
%% set up number of integration points nw, nodes xw, and weights w
nw = 11;
[xw,w] = GaussQuad(nw,'legendre');
%% for the first element the weights and roots are different
xw1 = 0.5*(xw+1); w1 = 0.5*w;
C = StatCov1d(nodes,h*xw1+nodes(1),nodes,Cd,0);
psi = shapefun(xw1,p,1); %% calculations on ref.element
D(:,1) = C*(w1.*psi);
%% for the last element the weights and roots are different as well
xw2 = 0.5*(xw-1); w2 = w1;
C = StatCov1d(nodes,h*xw2+nodes(nnodes),nodes,Cd,0);
psi = shapefun(xw2,p,1); %% calculations on ref.element
D(:,nnodes) = C*(w2.*psi);
psi = shapefun(xw,p,1); %% calculations on ref.element
if p == 2; psi2 = shapefun(xw,p,0); end
for j = 2:nnodes-1
C = StatCov1d(nodes,h*xw+nodes(j),nodes,Cd,0);
if p == 1
D(:,j) = C*(w.*psi);
else
if mod(j,2)==0
D(:,j) = C*(w.*psi2);
else
D(:,j) = C*(w.*psi);
end
end
end
D = 2*h*D;
L = eye(nnodes,nnodes);
case 'galerkin'
nw = 11;
[xw,w] = GaussQuad(nw,'legendre');
xw1 = 0.5*(xw+1); w1 = 0.5*w; %% weights and roots for the first element
xw2 = 0.5*(xw-1); w2 = w1; %% weights and roots for the last element
psi1 = shapefun(xw1,p,1); psi2 = shapefun(xw2,p,1); %% shape function on the 1st and last element
%% shape functions on other elements
if p == 1
psi(:,1) = shapefun(xw,p,1);
psi(:,2) = psi(:,1);
elseif p == 2
psi(:,1) = shapefun(xw,p,0); psi(:,2) = shapefun(xw,p,1);
end
%% calculations of the matrix D entries
W = w1*w1';
C = StatCov1d(h*xw1+nodes(1),h*xw1+nodes(1),nodes,Cd,0);
D(1,1) = sum(sum(C.*W.*(psi1*psi1')));
W = w1*w2';
C = StatCov1d(h*xw1+nodes(1),h*xw2+nodes(nnodes),nodes,Cd,0);
D(1,nnodes) = sum(sum(C.*W.*(psi1*psi2')));
D(nnodes,1) = D(1,nnodes);
W = w1*w';
for j = 2:nnodes-1
C = StatCov1d(h*xw1+nodes(1),h*xw+nodes(j),nodes,Cd,0);
D(1,j) = sum(sum(C.*W.*(psi1*psi(:,mod(j,2)+1)')));
D(j,1) = D(1,j);
end
W = w2*w2';
C = StatCov1d(h*xw2+nodes(nnodes),h*xw2+nodes(nnodes),nodes,Cd,0);
D(nnodes,nnodes) = sum(sum(C.*W.*(psi2*psi2')));
W = w2*w';
for j = 2:nnodes-1
C = StatCov1d(h*xw2+nodes(nnodes),h*xw+nodes(j),nodes,Cd,0);
D(nnodes,j) = sum(sum(C.*W.*(psi2*psi(:,mod(j,2)+1)')));
D(j,nnodes) = D(nnodes,j);
end
W = w*w';
for j = 2:nnodes-1
for k = j:nnodes-1
C = StatCov1d(h*xw+nodes(j),h*xw+nodes(k),nodes,Cd,0);
D(j,k) = sum(sum(C.*W.*(psi(:,mod(j,2)+1)*psi(:,mod(k,2)+1)')));
D(k,j) = D(j,k);
end
end
D = 4*h*h*D;
L = GalerkinL(n,h,p);
end
% get eigenpairs
[phi,lambda] = eigs(D,L,neig);
% improve approximation of the eigenfunctions
res = -(D*phi-L*phi*lambda); %% find residual first
lambda = diag(lambda,0);
% for j = 1:neig
% M = D - lambda(j)*L;
% phi(:,j) = phi(:,j) + M\res(:,j);
% end
% get rid of 0 complex part (needed for gaussian kernels)
lambda = abs(real(lambda));
% sort found eigenvalues in descending order
[lambda,ix] = sort(lambda,'descend');
% rearrange the eigenfunctions in the corresponding order
Nphi = phi;
for j = 1:neig
phi(:,ix(j)) = Nphi(:,j);
end
phi = signchange(phi);
% normalize found eigenfunctions
if p == 1
for j = 1:neig
v = phi(:,j);
vnorm = sqrt(h/3*(v(1)^2+v(n)^2+2*sum(v(2:n-1).*v(2:n-1))+sum(v(1:n-1).*v(2:n))));
phi(:,j) = phi(:,j)/vnorm;
end
else
for j = 1:neig
v = phi(:,j);
vnorm = sqrt(h/15*(2*v(1)^2+2*v(nnodes)^2+8*sum(v(2:2:nnodes-1).^2)+4*sum(v(3:2:nnodes-1).^2)+...
2*sum(v(1:nnodes-1).*v(2:nnodes))-sum(v(1:2:nnodes-2).*v(3:2:nnodes))));
phi(:,j) = phi(:,j)/vnorm;
end
end
% make sure to get rid of complex zero part for some kernels
for j = 1:neig
phi(:,j) = real(phi(:,j));
end
% plot eigenvalues if needed
if ToPlot
plot(lambda,'-sk','LineWidth',1.5);
end
end
%%%%%%%%%%%%%%%%%%%% SUBFUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function f = shapefun(x,p,mode) %%%%% shape function for reference element
lenx = length(x);
f = zeros(lenx,1);
if p == 1 %% piecewise linear
for j = 1:lenx
if (x(j)<0)&&(x(j)>=-1)
f(j) = 1+x(j);
elseif (x(j)>=0)&&(x(j)<=1)
f(j) = 1-x(j);
else
f(j) = 0;
end
end
elseif p == 2 %% piecewise quadratic
switch mode
case 1 %% mesh vertices
for j = 1:lenx
if (x(j)<0)&&(x(j)>=-1)
f(j) = (1+x(j)).*(1+2*x(j));
elseif (x(j)>=0)&&(x(j)<=1)
f(j) = (1-x(j)).*(1-2*x(j));
else
f(j) = 0;
end
end
case 0 %% midpoints
for j = 1:lenx
if abs(x(j)) < 0.5
f(j) = 1-4*x(j).*x(j);
else
f(j) = 0;
end
end
end
end
end
function ResF = signchange(F)
ResF = F;
n = size(F,2);
for j = 1:n
if F(1,j) < 0
ResF(:,j) = -F(:,j);
end
end
end
function L = GalerkinL(n,h,p)
switch p
case 1
L = zeros(n,n);
L(1,1) = 1/3;
L(n,n) = 1/3;
for k = 2:n-1
L(k,k) = 2/3;
end
for k = 1:n-1
L(k,k+1) = 1/6;
L(k+1,k) = 1/6;
end
L = h*L;
% A = h/6*[2 1; 1 2];
% for k = 1:n-1
% for i = 1:2
% for j = 1:2
% ig = k+i-1;
% jg = k+j-1;
% L(ig,jg) = L(ig,jg) + A(i,j);
% end
% end
% end
case 2
L = zeros(2*n-1,2*n-1);
A = h/30*[4 2 -1; 2 16 2; -1 2 4];
for k = 1:n-1
for i = 1:3
for j = 1:3
ig = 2*k+i-2;
jg = 2*k+j-2;
L(ig,jg) = L(ig,jg) + A(i,j);
end
end
end
end
end
function M = StatCov1d(x,y,xdata,C,ToPlot)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% returns the covariance function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[X,Y] = meshgrid(xdata);
M = interp2(X,Y,C,x,y);
M = M';
% plot the covariance if needed
if ToPlot
figure;
surf(x,y,M);
title(strcat(kernel,' model'),'fontsize',20);
end
end