# Help me to run the code

조회 수: 2 (최근 30일)
MINATI PATRA 2021년 10월 29일
댓글: MINATI PATRA 2021년 11월 13일
% file modified by sujogya %dated 9.1.2021
% min z=x1-3x2+2x3 % max z'=-x1+3x2-2x3 % s.t. %3x1-x2+2x3<=7, %-2x1+4x2+0x3<=12, %-4x1+3x2+8x3<=10
NVar = 3; C = [-1 3 2]; CMat = [3 -1 2;-2 4 0;-4 3 8]; b = [7; 12; 10;];
s = eye(size(CMat,1)); A = [CMat s b]; cost = zeros(1,size(A,2)); cost(1:NVar) = C;
%% Constraint
BV = NVar+1:1:size(A,2)-1; zjcj = cost(BV)*A - cost;
%% print the Table
zcj = [zjcj;A]; simpletable = array2table(zcj);
simpletable.Properties.VariableNames(1:size(zcj,2)) = {'x_1','x_2','x_3','s_1','s_2','s_3','sol'}
if any(zjcj<0);
fprintf('the current solution is not optimal');
fprintf('\n--------------next iteration----------')
disp('old basic variable(BV)=')
else('optimal solution reached');
end
disp(BV);
%% find entering variable
zc = zjcj(1:end-1); [entercol,pvt_col] = min(zc);
fprintf('the most negative element in zj-cj row is %d corresponding to column%d\n',entercol,pvt_col)
fprintf('entering variable is %d\n',pvt_col);
%% finding leaving variable
sol = A(:,end)
column = A(:,pvt_col);
if all(column <= 0)
printf(' solution is unbdd');
for i = 1:size(column,1)
if column(i)>0
ratio(i) = sol(i)./column(i);
else
ratio(i) = inf
end
end
[MinRatio, pvt_row] = min(ratio);
fprintf('min ratio correspond to pivot row is %d\n',pvt_row);
fprintf('leaving variable %d\n',BV(pvt_row));
end
%% Finding the minimum
BV(pvt_row) = pvt_col;
disp('New basic variables(BV)=');
disp(BV);
%% Pivot key
pivot_key = A(pvt_row,pvt_col);
%%% update the table
A(pvt_row,:) = A(pvt_row,:)./pvt_key;
for i = 1:size(A,1)
if i ~= pvt_row
A(i,:) = A(i,:) - A(i,pvt_col).*A(pvt_row,:);
end
zjcj = zjcj-zjcj(pvt_col).*A(pvt_row,:);
end
%%% for printing purpose
zcj = [zjcj;A]; simptable = array2table(zcj);
simptable.properties.variable(1:size(zcj,2)) = {'x_1','x_2','x_3','s_1','s_2','s_3','sol'};
%%%% simplex method start
run = true
while run
if any(zjcj < 0);
fprintf(' the current solution is not optimal\n')
fprintf(' ----next iteration start----\n')
disp('the old basic variable (BV)=');
disp(BV);
%% print the optimal solution
BFS = zeros(1,size(A,2)); BFS(BV) = A(:,end); BFS(end) = sum(BFS.*cost);
current_BFS = array2table(BFS);
current_BFS.properties.variablenames(1:size(current_BFS,2)) = {'x_1','x_2','x_3','s_1','s_2','s_3','sol'};
else
run = false
fprintf('-------**********--------\n')
fprintf('the solution is optimal\n')
disp('optimal solution')
end
end

댓글을 달려면 로그인하십시오.

### 채택된 답변

Walter Roberson 2021년 10월 30일
if all(column <= 0)
if that is false, then
[MinRatio, pvt_row] = min(ratio);
that is not executed, and then
BV(pvt_row) = pvt_col;
pvt_row is not defined -- because you only assign to it if all(column <= 0)
##### 댓글 수: 3이전 댓글 1개 표시이전 댓글 1개 숨기기
Walter Roberson 2021년 11월 11일
Maybe V(pvt_row) = pvt_col; should be inside the if
MINATI PATRA 2021년 11월 13일
@ Thank you Walter, It is done.

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Sulaymon Eshkabilov 2021년 10월 30일
Some of the assigned variables don't get their values from the second [if .. end] and [for end] operations, e.g.: pvt_row
Thus, its default value has to be assigned. Moreover, one variable name is misspelled: pivot_key vs. pivt_key
There is no need to rename simptable variable names, since it is already done.
In addition, to rename table variable names (e.g. current_BFS), it is better to use renamevars().
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
MINATI PATRA 2021년 11월 11일
@ Sulaymon
Still not working

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by