optimization problem in matlab
이전 댓글 표시
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
Cris LaPierre
2023년 2월 22일
Please share the full error message (copy/paste all the red text).
What is the value of alpha?
david
2023년 2월 22일
John D'Errico
2023년 2월 22일
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
2023년 2월 22일
답변 (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
카테고리
도움말 센터 및 File Exchange에서 Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
