optimization expression includes an integration
3 次查看(过去 30 天)
显示 更早的评论
I am trying an optimization problem in which the expression of the objective function includes an integral.
It is obvious that the \sigma equal to one results in the optimium solution. I want to use the optimization toolbox to get this result with an initial \sigma equal to, say, 10.
I wrote the following code.
g1 = @(x,c) (exp(-(0.5*(x./c).^2))./sqrt(2*pi*c^2));
c = optimvar("c",1,1,'Type','continuous','LowerBound',0.1,'UpperBound',10);
prob = optimproblem('Objective', (0.5 - integral(@(x)g1(x,c),0, 10)).^2);
[solf,fvalf,eflagf,outputf] = solve(prob)
The following error is generated.
Error using integralCalc>finalInputChecks (line 544)
Input function must return 'double' or 'single' values. Found
'optim.problemdef.OptimizationExpression'.
I have two questions:
1, Am I coding the problem properly/correctly?
2, If the code is basically correct, how can I solve the error?
Thank you.
0 个评论
采纳的回答
Torsten
2024-7-25
编辑:Torsten
2024-7-25
c = optimvar("c",1,1,'Type','continuous','LowerBound',0.1,'UpperBound',10);
g1 = @(x,c) exp(-0.5*(x./c).^2)./sqrt(2*pi*c^2);
obj = fcn2optimexpr(@(c)abs(0.5-integral(@(x)g1(x,c),0,10)).^0.5,c);
prob = optimproblem('Objective', obj);
show(prob)
x0.c = 10;
options = optimoptions("fmincon",OptimalityTolerance=1e-18);
[solf,fvalf,eflagf,outputf] = solve(prob,x0,Options=options)
%The function showing the error is very flat - thus c = 1 is unlikely as
%result.
y = 0.01:0.01:3;
G1 = arrayfun(@(y)abs(0.5-integral(@(x)g1(x,y),0,10)).^0.5,y);
plot(y,G1)
grid on
2 个评论
Torsten
2024-7-29
编辑:Torsten
2024-7-29
I plotted the error function for your problem. It's almost 0 for c in the interval [0 3] and too flat to get good convergence. Most probably, the integral cannot be computed with sufficient accuracy. Using MATLAB's "erf" function (after a coordinate transformation) or a symbolic computation might help.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!