optimization problem in matlab
Mostrar comentarios más antiguos
so i want to numerically minimize an expression in the maximims norm.
i have the following code:
function [c1, c2] = minimize_expression(alpha)
fun = @(c) expression_to_minimize(c, alpha);
% Define the initial guess for the optimization
x0 = [0.5, 1];
% Define the lower and upper bounds for the optimization
lb = [0, 0];
ub = [1, Inf];
% Set options for fmincon
options = optimoptions('fmincon', 'Display', 'off', 'Algorithm', 'interior-point', ...
'OptimalityTolerance', 1e-12, 'ConstraintTolerance', 1e-12, ...
'StepTolerance', 1e-12, 'MaxFunctionEvaluations', 1e5, ...
'FunctionTolerance', 1e-12, 'MaxIterations', 1e5, ...
'SpecifyObjectiveGradient', false, 'SpecifyConstraintGradient', false, ...
'HonorBounds', true, 'SubproblemAlgorithm', 'cg', 'CheckGradients', false, ...
'Diagnostics', 'off', 'FiniteDifferenceStepSize', [], 'TypicalX', []);
[c, fval] = fmincon(fun, x0, [], [], [], [], lb, ub, [], options);
% Return the values of c1 and c2
c1 = c(1);
c2 = c(2);
end
function val = expression_to_minimize(c, alpha)
% Define the function whose infinity norm we want to minimize
x = linspace(0, 50, 1001);
f = c(1) * integral(@(t) t^(alpha-1)./cosh(t).*(x.^2.*cos(x))./(x.^2+t.^2), 0, Inf) ...
+ (1-c(1)) * integral(@(t) t^alpha./sinh(t).*(x.*sin(x))./(x.^2+t.^2), 0, Inf) - c(2) * sin(x);
val = norm(f, Inf);
end
however it always produces an error. Any idea whats wrong? Sorry for the format i dont know how to paste code.
4 comentarios
Cris LaPierre
el 22 de Feb. de 2023
Please share the full error message (copy/paste all the red text).
What is the value of alpha?
david
el 22 de Feb. de 2023
John D'Errico
el 22 de Feb. de 2023
Remember that infinite domains of integration are often highly problematic. Before you do anything at all, verify that the integrals you are computing do yield meaningful results, for a variety of values for alpha.
Next, when you say that something always return an error, tell us EXACTLY what the error was! So everything in red. Otherwise we cannot know if you are even properly running the code.
david
el 22 de Feb. de 2023
Respuestas (1)
Why using 'FiniteDifferenceStepSize'and'TypicalX'in your options if you don't want to set values for them ?
[c1 c2] = minimize_expression(2)
function [c1, c2] = minimize_expression(alpha)
fun = @(c) expression_to_minimize(c, alpha);
% Define the initial guess for the optimization
x0 = [0.5, 1];
% Define the lower and upper bounds for the optimization
lb = [0, 0];
ub = [1, Inf];
% Set options for fmincon
options = optimoptions('fmincon', 'Display', 'off', 'Algorithm', 'interior-point', ...
'OptimalityTolerance', 1e-12, 'ConstraintTolerance', 1e-12, ...
'StepTolerance', 1e-12, 'MaxFunctionEvaluations', 1e5, ...
'FunctionTolerance', 1e-12, 'MaxIterations', 1e5, ...
'SpecifyObjectiveGradient', false, 'SpecifyConstraintGradient', false, ...
'HonorBounds', true, 'SubproblemAlgorithm', 'cg', 'CheckGradients', false, ...
'Diagnostics', 'off');%, 'FiniteDifferenceStepSize', [], 'TypicalX', []);
[c, fval] = fmincon(fun, x0, [], [], [], [], lb, ub, [], options);
x = linspace(0, 50, 1001);
f1 = c(1)/c(2) * integral(@(t) t.^(alpha-1)./cosh(t).*(x.^2.*cos(x))./(x.^2+t.^2), 0, Inf,'ArrayValued',1) ...
+ (1-c(1))/c(2) * integral(@(t) t.^alpha./sinh(t).*(x.*sin(x))./(x.^2+t.^2), 0, Inf,'ArrayValued',1);
f2 = sin(x);
plot(x,[f1; f2])
% Return the values of c1 and c2
c1 = c(1);
c2 = c(2);
end
function val = expression_to_minimize(c, alpha)
% Define the function whose infinity norm we want to minimize
x = linspace(0, 50, 1001);
f = c(1) * integral(@(t) t.^(alpha-1)./cosh(t).*(x.^2.*cos(x))./(x.^2+t.^2), 0, Inf,'ArrayValued',1) ...
+ (1-c(1)) * integral(@(t) t.^alpha./sinh(t).*(x.*sin(x))./(x.^2+t.^2), 0, Inf,'ArrayValued',1) - c(2) * sin(x);
val = norm(f, Inf);
end
Categorías
Más información sobre Optimization en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
