Solving Constrained Optimization for 4 variables (or 3?)

I am trying to solve the question from the picture below.
I dont know how to modify the original code from solving 2 variables into solving 4 variables. My friend said it was supposedly only 3 variables (x4 supposedly was x3), maybe because the typo.
There are my Matlab code

回答 (1 件)

Torsten
Torsten 2022 年 4 月 8 日
編集済み: Torsten 2022 年 4 月 8 日

1 投票

It's a simple linear optimization problem. If you are allowed to, use "linprog" to solve.
f = [1; 1; 1; 0];
Aeq = [3 1 0 -1;1 -2 1 0];
beq = [-5;1];
lb = [-Inf; 0 ;0 ;0];
ub = [Inf ;Inf; Inf ;Inf];
sol = linprog(f,[],[],Aeq,beq,lb,ub)

7 件のコメント

Tristofani Agasta
Tristofani Agasta 2022 年 4 月 8 日
編集済み: Tristofani Agasta 2022 年 4 月 8 日
I tried with another code from forum, but still not giving the same solution like yours
clc
clear
close all
syms l b h k lambda
A = l + b + h + 0;
V = 3*l + b - k == 0;
G = l - 2*b + h == 0;
L = A + lambda * lhs(V) + lambda * lhs(G);
dL_dl = diff(L,l) == 0;
dL_db = diff(L,b) == 0;
dL_dh = diff(L,h) == 0;
dL_dk = diff(L,k) == 0;
dL_dlambda = diff(L,lambda) == 0;
system = [dL_dl; dL_db; dL_dh; dL_dk; dL_dlambda];
[l_val, b_val, h_val, k_val, lambda_val] = solve(system, [l b h k lambda], 'Real', true)
results_numeric = double([l_val, b_val, h_val,k_val, lambda_val])
V_res = l_val + b_val + h_val + 0
V = 3*l_val + b_val - k_val;
G = l_val - 2*b_val + h_val;
Torsten
Torsten 2022 年 4 月 8 日
編集済み: Torsten 2022 年 4 月 8 日
This code is absolutely inadequate for the problem. Don't stick to it.
And I also don't see how to implement the non-negative constraints for x2 - x4 in the Lagrange approach.
Tristofani Agasta
Tristofani Agasta 2022 年 4 月 8 日
Thank you for your response, perhaps I must find a book which tell me how to do with negative constraints?
Torsten
Torsten 2022 年 4 月 8 日
Is there any reason not to use linprog ?
Tristofani Agasta
Tristofani Agasta 2022 年 4 月 8 日
編集済み: Tristofani Agasta 2022 年 4 月 8 日
Yes, because my lecturer demanding to use basic programming with steps and can be explained mathematically. Just like Gauss Elimination with some iterations without using library or toolbox
Torsten
Torsten 2022 年 4 月 8 日
編集済み: Torsten 2022 年 4 月 8 日
Then look up "simplex algorithm".
Programming it, everything is hand-made.
Tristofani Agasta
Tristofani Agasta 2022 年 4 月 8 日
Thank you for your reply

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

カテゴリ

ヘルプ センター および File ExchangeLinear Programming and Mixed-Integer Linear Programming についてさらに検索

製品

リリース

R2021a

質問済み:

2022 年 4 月 8 日

コメント済み:

2022 年 4 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by