-
Notifications
You must be signed in to change notification settings - Fork 0
/
FVSolverTest.m
142 lines (125 loc) · 3.28 KB
/
FVSolverTest.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
% This script tests the class FVSolver, comparing graphically its solutions
% with the correct ones.
%%
clc
d = 1;
m = 256;
k = @(x) sqrt(x)+1;
solver = FVSolver(d,@(pointsSet,m) kWrapper(1,k,pointsSet,m),m);
x_plot = linspace(0,1,1024);
y_plot = solver.getSolutionValue(x_plot);
f_true = @(x) (sqrt(x)-log(sqrt(x)+1)-1+log(2))/(log(2)-1);
y_true = f_true(x_plot);
plot(x_plot, y_plot,x_plot, y_true)
%%
clc
d = 1;
m = 256;
k = @(x) cosh(x);
solver = FVSolver(d,@(pointsSet,m) kWrapper(1,k,pointsSet,m),m);
x_plot = linspace(0,1,1024);
y_plot = solver.getSolutionValue(x_plot);
f_true = @(x) 1-(atan(tanh(x/2)))/(atan(tanh(1/2)));
y_true = f_true(x_plot);
plot(x_plot, y_plot,x_plot, y_true)
%%
clc
d = 1;
m = 256;
k = @(x) exp(x);
solver = FVSolver(d,@(pointsSet,m) kWrapper(1,k,pointsSet,m),m);
x_plot = linspace(0,1,1024);
y_plot = solver.getSolutionValue(x_plot);
f_true = @(x) exp(-x).*(exp(1)-exp(x))/(exp(1)-1);
y_true = f_true(x_plot);
plot(x_plot, y_plot,x_plot, y_true)
%%
clc
d =1;
m = 256;
k = @(x) 1+x.*0;
solver = FVSolver(d,@(pointsSet,m) kWrapper(1,k,pointsSet,m),m);
x_plot = linspace(0,1,1024);
y_plot = solver.getSolutionValue(x_plot);
f_true = @(x) 1-x;
y_true = f_true(x_plot);
plot(x_plot, y_plot,x_plot,y_true)
%%
clc
d = 1;
m = 256;
k = @(x) x.^2+x+2;
solver = FVSolver(d,@(pointsSet,m) kWrapper(1,k,pointsSet,m),m);
x_plot = linspace(0,1,1024);
y_plot = solver.getSolutionValue(x_plot);
f_true = @(x) (atan((2*x+1)/sqrt(7))-atan(3/sqrt(7)))./ ...
(atan(1/sqrt(7))-atan(3/sqrt(7)));
y_true = f_true(x_plot);
plot(x_plot, y_plot,x_plot, y_true)
%%
clc
d = 1;
m = 256;
k = @(x) 1./(1+exp(x));
solver = FVSolver(d,@(pointsSet,m) kWrapper(1,k,pointsSet,m),m);
x_plot = linspace(0,1,1024);
y_plot = solver.getSolutionValue(x_plot);
f_true = @(x) (-x-exp(x)+exp(1)+1)/exp(1);
y_true = f_true(x_plot);
plot(x_plot, y_plot,x_plot, y_true)
%%
clc
d = 2;
m = 16;
k = @(x,y) sinh(x)+exp(-15.*(1-y));
solver = FVSolver(d,@(pointsSet,m) kWrapper(2,k,pointsSet,m),m);
[x_plot,y_plot] = meshgrid(linspace(0,1,1024));
z_plot = solver.getSolutionValue(x_plot,y_plot);
surf(x_plot,y_plot,z_plot)
%%
clc
d = 2;
m = 32;
k = @(x,y) sinh(0.5*(x+1)./(y+1));
solver = FVSolver(d,@(pointsSet,m) kWrapper(2,k,pointsSet,m),m);
[x_plot,y_plot] = meshgrid(linspace(0,1,1024));
z_plot = solver.getSolutionValue(x_plot,y_plot);
surf(x_plot,y_plot,z_plot)
%%
% This function creates a wrapper for the functions in input, so that they
% are compatible with FVSolver.
function value = kWrapper(d, f, varargin)
pointsSet = varargin{1};
m = varargin{2};
if d==1
switch pointsSet
case "cen"
xpoints = linspace(1/m,1-1/m,m-1)';
case "ext"
xpoints = [0,1]';
end
value = f(xpoints);
else
switch pointsSet
case "cpx"
[xpoints, ypoints] =...
meshgrid(linspace(1/(2*m),1-1/(2*m),m), ...
linspace(1/(m),1-1/(m),m-1));
xpoints = reshape(xpoints,[],1);
ypoints = reshape(ypoints,[],1);
case "cpy"
[xpoints, ypoints] =...
meshgrid(linspace(1/(m),1-1/(m),m-1), ...
linspace(1/(2*m),1-1/(2*m),m));
xpoints = reshape(xpoints,[],1);
ypoints = reshape(ypoints,[],1);
case "bl"
xpoints =repelem(0,m)';
ypoints = linspace(1/(2*m),1-1/(2*m),m)';
case "br"
xpoints =repelem(1,m)';
ypoints = linspace(1/(2*m),1-1/(2*m),m)';
end
value = f(xpoints,ypoints);
end
end