forked from LeoZhangCh6/Optical-Vortex-Soliton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleValue.m
154 lines (128 loc) · 3.93 KB
/
SingleValue.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
clearvars, clearvars –global
clear all, clear global
% ======= variables ========
global dim R m P_0 basis;
dim = 25;
R = 10;
m = 1;
P_0 = 339.3 ;
basis = cell(dim, 3);
for n = 1:1:dim
basis{n, 1} = chebfun(@(r) sin(r * n * pi ./ R ), [0, R]);
gram_schmidt(n);
basis{n, 2} = diff(basis{n, 1});
basis{n, 3} = diff(basis{n, 2});
end
t = cputime;
disp(['dim: ',num2str(dim), ...
' R: ',num2str(R), ...
' m: ',num2str(m), ...
' P_0: ',num2str(P_0) ...
])
% ============ problem setup ================
c = optimvar('c', dim);
obj = fcn2optimexpr(@I, c);
prob = optimproblem('Objective', obj);
cons1 = sum(c .* c) == P_0; % beam power constraints
prob.Constraints.cons1 = cons1;
options = optimoptions(@fmincon,...
'Algorithm','active-set',...
'MaxFunEvals', 1e+05, ...
'MaxIter', 1000,...
'TolCon',1e-8,...
'TolFun',1e-8);
x0.c = zeros(1, dim);
x0.c(1) = 1;
try
[sol,fval,exitflag,output] = solve(prob,x0,'options',options);
% display success/fail
disp(exitflag > 0)
if exitflag > 0 % if successful optimization
% plotting
u = lin_comb(sol.c);
cache = sol.c;
% compute eigenvalue
eigen = compute_eigen(sol.c, P_0);
disp(['Yields a good eigenvalue:', num2str(eigen)])
% compute error
error = compute_error(sol.c, eigen);
disp(['error:', num2str(error)])
end
catch
disp('some error')
end
disp(['Time it took:', num2str(cputime-t)])
% ================== functions ===================
% Linear Combination of variational vector and basis (global)
function u = lin_comb(vec)
global R dim basis;
u = chebfun(0, [0 R]);
for n = 1:dim
u = u + vec(n) * basis{n, 1};
end
end
function u_r = lin_comb_r(vec)
global R dim basis;
u_r = chebfun(0, [0 R]);
for n = 1:dim
u_r = u_r + vec(n) * basis{n, 2};
end
end
function u_rr = lin_comb_rr(vec)
global R dim basis;
u_rr = chebfun(0, [0 R]);
for n = 1:dim
u_rr = u_rr + vec(n) * basis{n, 3};
end
end
% inner product defined as: 2 pi * int_0^R r*f(r)*g(r) dr
function prod = inner_product(func1, func2)
global R;
prod = func1 * func2 * chebfun(@(r) r, [0, R]);
prod = 2 * pi * sum(prod);
end
% gram_schmidt process on n th basis
function gram_schmidt(n)
global basis;
basis{n, 1} = basis{n, 1} / sqrt(inner_product(basis{n, 1}, basis{n, 1}));
for i = 1 : n-1
basis{n, 1} = basis{n, 1} - projection(basis{i, 1}, basis{n, 1});
end
basis{n, 1} = basis{n, 1} / sqrt(inner_product(basis{n, 1}, basis{n, 1}));
function proj = projection(fun_i, fun_n)
proj = inner_product(fun_i,fun_n) * basis{i, 1} / inner_product(fun_i,fun_i);
end
end
% I: action functional
function integral = I(vec)
global R m;
u = lin_comb(vec);
u_r = lin_comb_r(vec);
r = chebfun(@(r) r, [0, R]);
integrand = r * u_r^2;
integrand = integrand + m^2 * u^2 / r;
integrand = integrand - u * u_r * r^2 / (1 + u^2);
integral = sum(integrand) / 2;
end
function eigen = compute_eigen(vec, p_0)
global R m;
u = lin_comb(vec);
u_r = lin_comb_r(vec);
r = chebfun(@(r) r, [0, R]);
ut = r * u_r^2;
ut = ut + m^2 * u^2 / r;
ut = ut + r*u^2 / (1 + u^2);
eigen = -2 * pi * sum(ut)/ p_0;
end
% compute error intergrating (1)
function error = compute_error(vec, b)
global R m;
u = lin_comb(vec);
u_r = lin_comb_r(vec);
r = chebfun(@(r) r, [0, R]);
ut = b * r * u;
ut = ut - u_r - r * lin_comb_rr(vec);
ut = ut + m^2 * u / r ;
ut = ut + r * u / (1 + u^2);
error = sum(ut^2);
end