Solving a quadratic optimization problem subjected to linear constraints
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm trying to recreate the code behind this picture. I have a function
where
. I need to solve the following quadratic optimization problem subject to linear constraint:
subject to: 

the input data are:
= [0, 0.25, 0.5, 1, 1.2, 1.8, 2];
= [2, 0.8, 0.5, 0.1, 1, 0.5, 1];
= [0, 0.25, 0.5, 1, 1.2, 1.8, 2]; And I need the following result: 

I tried to use the function fmincon but it gives me always the same value for lambda. Can you help me find the error or explain to me what kind of function I need to use instead?
clc;
clear;
close all;
x_i = [0, 0.25, 0.5, 1, 1.2, 1.8, 2];
f_i = [2, 0.8, 0.5, 0.1, 1, 0.5, 1];
ottimizzazione_quadratica(x_i,f_i);
function risultato = ottimizzazione_quadratica(x_i, f_i)
x0 = zeros(size(x_i));
A = [];
b = [];
Aeq = [];
beq = [];
lb = zeros(size(x_i)); % lambda_i >= 0
ub = [];
lambda_ottimale = fmincon(@(lambda) funzione_obiettivo(lambda, x_i, f_i), x0, A, b, Aeq, beq, lb, ub);
risultato = F(lambda_ottimale, x_i);
% Plot F(x) and points (xi, fi)
x_vals = linspace(min(x_i), max(x_i), 1000);
F_vals = arrayfun(@(x) F(lambda_ottimale, x), x_vals);
figure;
plot(x_i, f_i, 'ro', 'MarkerSize', 10, 'MarkerFaceColor', 'r'); % Punti dati
hold on;
plot(x_vals, F_vals, 'b-', 'LineWidth', 2); % Funzione F(x)
xlabel('x');
ylabel('F(x)');
grid on;
hold off;
end
function risultato = funzione_obiettivo(lambda, x_i, f_i)
risultato = norm(F(lambda, x_i) - f_i)^2;
end
% F(x)
function risultato = F(lambda, x_i)
risultato = sum(lambda .* phi(x_i));
end
% phi
function risultato = phi(x_i)
% max(0, 1 - norm(x - xi)^4)*(4*norm(x-xi)+1)
risultato = arrayfun(@(x) max(0, 1 - norm(x - x_i)^4)*(4*norm(x - x_i) + 1), x_i);
end
0 Kommentare
Akzeptierte Antwort
Matt J
am 28 Nov. 2023
Bearbeitet: Matt J
am 28 Nov. 2023
xi=[0, 0.25, 0.5, 1, 1.2, 1.8, 2]';
fi= [2, 0.8, 0.5, 0.1, 1, 0.5, 1]';
phi=@(r) max(0, 1 - r).^4.*(4*r + 1);
C=phi(abs(xi-xi'));
[lambda,fval]=lsqnonneg(C,fi)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Quadratic Programming and Cone Programming finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
