フィルターのクリア

Optimization of a script

1 回表示 (過去 30 日間)
Alcide
Alcide 2023 年 8 月 24 日
回答済み: Bruno Luong 2023 年 8 月 24 日
Hi,
I'm working on a script like this:
a=[ 1 1 0 1 1 1 1 0 1]
b=2
c=3.2
v=0
for i=1: 9
f=b*2^(i-1)
if a(i)==0
g=f*c-f
v=v+g
end
end
I would like to maximize the value of v using b and c as variables.
How can i do that?
TY
  2 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 8 月 24 日
Do you mean - you want to find value of b and c for which v will be maximum?
Torsten
Torsten 2023 年 8 月 24 日
b = c = Inf

サインインしてコメントする。

採用された回答

Bruno Luong
Bruno Luong 2023 年 8 月 24 日
The v you compute is simply
132*b*(c-1)
You want to optimiza it? Take b=Inf and c=Inf

その他の回答 (1 件)

Kevin Holly
Kevin Holly 2023 年 8 月 24 日
編集済み: Kevin Holly 2023 年 8 月 24 日
You can use fmincon.
a = [1 1 0 1 1 1 1 0 1];
b0 = 2; % Initial guess for b
c0 = 3.2; % Initial guess for c
% Define the objective function to maximize
objective = @(x) -calculateV(a, x(1), x(2));
% Set the lower and upper bounds for the variables
lb = [0, 0]; % Lower bounds for b and c
ub = [Inf, Inf]; % Upper bounds for b and c
% Perform constrained optimization to maximize v
[x, fval] = fmincon(objective, [b0, c0], [], [], [], [], lb, ub);
Problem appears unbounded. fmincon stopped because the objective function value is less than the value of the objective function limit and constraints are satisfied to within the value of the constraint tolerance.
% Retrieve the optimized values
b_optimized = x(1);
c_optimized = x(2);
v_maximized = -fval;
% Display the optimized values and the corresponding v
disp("Optimized Values:")
Optimized Values:
disp("b = " + b_optimized)
b = 10167158107334.54
disp("c = " + c_optimized)
c = 10639554538105.3
disp("Maximized v = " + v_maximized)
Maximized v = 1.4278972379828e+28
% Function to calculate v for given a, b, and c
function v = calculateV(a, b, c)
v = 0;
for i = 1:numel(a)
f = b * 2^(i-1);
if a(i) == 0
g = f * c - f;
v = v + g;
end
end
end

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by