Solving a quadratic optimization problem subjected to linear constraints

2 vues (au cours des 30 derniers jours)
arianna
arianna le 28 Nov 2023
Commenté : Torsten le 30 Nov 2023
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];
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

Réponse acceptée

Matt J
Matt J le 28 Nov 2023
Modifié(e) : Matt J le 28 Nov 2023
You can use lsqnonneg,
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)
lambda = 7×1
1.8083 0 0 0 0.6845 0 0.8566
fval = 0.4904
  2 commentaires
arianna
arianna le 30 Nov 2023
It's better than before, but I still don't get the same interpolation of the picture. Now I get this curve.
I don't understand what I'm missing.
Torsten
Torsten le 30 Nov 2023
Your code uses a different function for phi than in your mathematical description.
In your code:
phi(r)=max(0, 1 - r.^4).*(4*r + 1)
In your mathematical description:
phi(r)=(max(0, 1 - r)).^4.*(4*r + 1)
@Matt J used your mathematical description.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Quadratic Programming and Cone Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by