-
Notifications
You must be signed in to change notification settings - Fork 2
/
convergenceTable.m
127 lines (99 loc) · 2.95 KB
/
convergenceTable.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
function convergenceTable(caption, orders, m, e, q, tableType)
default_arg('tableType','plaintext')
switch tableType
case {'plaintext','text','plain'}
plainTextTable(caption, orders, m, e, q);
case {'tex', 'latex'}
latexTable(caption, orders, m, e, q);
end
end
function plainTextTable(caption, orders, m, e, q)
eW = 0;
qW = 0;
for i = 1:length(orders)
log_e{i} = log10(e{i});
eW = max(eW, findFieldWidth('%.2f',log_e{i}));
qW = max(qW, findFieldWidth('%.2f',q{i}));
end
mW = findFieldWidth('%d',m);
orderHeaderWidth = eW + qW + 1;
fprintf('%s\n',caption);
% Print order headers
fprintf(' %*s |',mW,'')
for i = 1:length(orders)
fprintf(' %-*s |', orderHeaderWidth, sprintf('Order %d', orders{i}));
end
fprintf('\n');
% Print eq headers
fprintf(' %*s |',mW,'m');
for i = 1:length(orders)
fprintf(' %*s %*s |', eW, 'e', qW, 'q');
end
fprintf('\n');
% Print devider
m_dev = repmat('-',1,mW);
column_dev = repmat('-',1,orderHeaderWidth);
fprintf('-%s-+',m_dev);
for i = 1:length(orders)
fprintf('-%s-+', column_dev);
end
fprintf('\n');
% Print each row
for i = 1:length(m)
fprintf(' %*d |',mW,m(i));
for j = 1:length(orders)
if i == 1
fprintf(' %*.2f %*s |', eW, log_e{j}(i), qW, '');
else
fprintf(' %*.2f %*.2f |', eW, log_e{j}(i), qW, q{j}(i-1));
end
end
fprintf('\n');
end
fprintf('\n');
end
function latexTable(caption, orders, m, e, q)
nOrders = length(orders);
header = {
'\begin{table}[H]'
'\centering'
['\begin{tabular}{c' repmat('|cc',1,nOrders) '} &']
orderheaders(orders)
'\hline'
['$N$' repmat('& $log_{10}(l_2)$ & $q$',1,nOrders) ' \\']
'\hline'
};
footer = {
'\end{tabular}'
['\caption{' caption '}']
'\label{table:LABEL}'
'\end{table}'
};
data = cell(1,length(m));
data{1} = num2str(m(1));
for j = 1:nOrders
data{1} = [data{1} ' & ' sprintf('%8.2f',log10(e{j}(1))) ' & ' ];
end
data{1} = [data{1} '\\'];
for i = 2:length(m)
data{i} = [data{i} num2str(m(i)) ];
for j = 1:nOrders
data{i} = [data{i} ' & ' sprintf('%8.2f',log10(e{j}(i))) ' & ' sprintf('%8.2f',(q{j}(i-1))) ];
end
data{i} = [data{i} '\\'];
end
nlc = sprintf('\n');
header = strjoin(header', nlc);
data = strjoin(data, nlc);
footer = strjoin(footer', nlc);
table = strjoin({header, data, footer}, nlc);
fprintf('%s\n', table);
end
function s = orderheaders(orders)
s= sprintf('\\multicolumn{2}{|c}{%dth order}',orders{1});
nOrders = length(orders);
for i = 2:nOrders
s = [s sprintf('& \\multicolumn{2}{|c}{%dth order}',orders{i})];
end
s = [s ' \\'];
end