-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJune202019.m
93 lines (66 loc) · 1.41 KB
/
June202019.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
%
n=10;
d=[1:n];
v=ones(n-1,1);
%1,0.5,1,05
for i=1:2:n-1
v(i)=1;
end
for i=2:2:n-2
v(i)=0.5;
end
A=diag(d)+diag(v,+1)+diag(v,-1);
D=diag(diag(A));
E=-tril(A,-1);
Bj=D\(D-A);
Bgs=(D-E)\(D-E-A);
max(abs(eig(Bj)))<1;
max(abs(eig(Bgs)))<1;
xex=ones(n,1);
b=A*b;
tol=1e-6;
maxit=500;
x0=zeros(n,1);
gj=D\b;
[x, iter, incr] =stationary_method(Bj,gj,x0,tol,maxit);
numel(iter)
ggs=(D-E)\b;
[x, iter, incr] =stationary_method(Bgs,ggs,x0,tol,maxit);
numel(iter)
P=diag(diag(A));
alpha=2/(max(abs(eig(inv(P)*A)))+min(abs(eig(inv(P)*A))));
[x, iter, incr] =prec_rich_method(A,b,P,alpha,x0,tol,maxit);
numel(iter)
%all three methods converge in just one iteration, meaning that the values
%of the preconditioned matrix P and alpha were properly chosen for the
%problem at hand: they yield good convergence properties
%
f1 =@(x) (x - 5).^2;
f2 =@(x) f1(x).^2;
a=0;
b=1;
x=linspace(a,b,1000);
y1=f1(x);
y2=f2(x);
figure;
plot(x,y1)
hold on;
plot(x,y2)
xlabel('x');
ylabel('f(x)');
grid on;
m=20;
composite_trapezoidal(f1,a,b,m)
61/3
composite_trapezoidal(f2,a,b,m)
2101/5
%both rules are quite accurate, in case of f1 the precision is higher since
%we are dealing with a polynomial of degree 2
%Since f1,f2 are at least C^2 we guarantee that both rules converge with an
%order of accuracy equal to 2
for i=1:5
integer_m(i)=composite_trapezoidal(f2,a,b,2.^i)
end
err=abs(2101/5-integer_m);
p_M=-diff(log(err))/log(2);
p_M