Solving linear system with constraints

4 ビュー (過去 30 日間)
Matthew
Matthew 2022 年 12 月 5 日
回答済み: Torsten 2022 年 12 月 5 日
You only need to focus on lines '12' and below. For reference, the above vectors are part of a math problem I wanted to see how to "automate."
I would like to assign '35' a variable that finds the maximum value such that the largest value of 'ans' is equal to 105. I am new to MatLab and require logic associated with each step if possible. Thank you in advance.

回答 (2 件)

Askic V
Askic V 2022 年 12 月 5 日
編集済み: Askic V 2022 年 12 月 5 日
In order people to help you, insert your code instead of screenshot. Please use code tags shown here:
Regarding the question you asked, please have a look at this example:
rng('default')
v = randi(100, 1, 15)
v = 1×15
82 91 13 92 64 10 28 55 96 97 16 98 96 49 81
% returns max element in v and its index
[m,ind] = max(v)
m = 98
ind = 12

Torsten
Torsten 2022 年 12 月 5 日
x0 = 2;
sol = fmincon(@obj,x0,[],[],[],[],[],[],@nonlcon)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
sol = 35.0000
function value = obj(x)
value = -x;
end
function [c, ceq] = nonlcon(x)
B = [0 1500 0];
BC = [0 1 0];
D = [-200 0 500];
E = [500 0 500];
BD = (D-B)/norm(D-B);
BE = (E-B)/norm(E-B);
matrix = [BD ;BE ;BC].';
matrix2 = [0; 0; x];
ans = matrix\matrix2;
c = max(ans) - 105;
ceq = [];
end

カテゴリ

Help Center および File ExchangeSystems of Nonlinear Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by