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 Comments

Please share the full error message (copy/paste all the red text).
What is the value of alpha?
Thx for your help.The error message is: Invalid value for OPTIONS parameter FiniteDifferenceStepSize: must be a non-empty double vector in the range 0 to Inf.
well i will call the function with different values of alpha
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.
Well i just cited the error message. I just think that the code is not entirely correct but i cannot find out what the problem is...The integrals definitely yield meaningful results. i checked it.

Sign in to comment.

Answers (1)

Torsten
Torsten on 22 Feb 2023
Edited: Torsten on 22 Feb 2023
Why using 'FiniteDifferenceStepSize'and'TypicalX'in your options if you don't want to set values for them ?
[c1 c2] = minimize_expression(2)
c1 = 0.1068
c2 = 0.4255
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

2 Comments

thank you very much! if someone else has a better idea on how to minimize this expression better or more efficient please let me know
Since we don't know the background of your question (i.e. approximating sin(x) in the Inf-norm by this complicated integral function), we cannot comment on this.

Sign in to comment.

Categories

Find more on Optimization in Help Center and File Exchange

Asked:

on 22 Feb 2023

Commented:

on 22 Feb 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!