optimization expression includes an integration
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sukvasant Tantikovt
el 25 de Jul. de 2024
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 comentarios
Respuesta aceptada
Torsten
el 25 de Jul. de 2024
Editada: Torsten
el 25 de Jul. de 2024
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 comentarios
Torsten
el 29 de Jul. de 2024
Editada: Torsten
el 29 de Jul. de 2024
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Get Started with Optimization Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!