Borrar filtros
Borrar filtros

I need to Fix an Error For Lines 34 & 126

5 visualizaciones (últimos 30 días)
Cody Crouse
Cody Crouse el 17 de Abr. de 2024
Respondida: Steven Lord el 17 de Abr. de 2024
Context of the Assignment I.E the Reason for the Code
First off, I want to state that this is a programming assginment for one of my engineering classes, that class being which deals with numerical methods for engineers. In particular, we are provided with a template code to fill in the blanks, for the purposes of solving an oil company heat rate problem, using the simplex (tableau) method.
Issues That I want Sorted Out
It is giving me an error for positive integers or logical values in the command window, hence why I am not able to run the code on MATLAB. The lines in question are line 34 & line 126, where the arrays are said not not to not give me positive or logical values.
A Warning About This Assignment
Note that I am not allowed to use advanced MATLAB functions such as sum(), rref(), symbolab or any other advanced functions.
%% TEMPLATE FOR Programming Assignment II Solver
clc;
clear all;
format shortE;
% Index for temporal variables to save
temp_index1 = 0;
temp_index2 = 0;
% Set up the standard form of the simplex method
% A includes coefficients of q1, q2 + q3, q4, q5 (slack variables)
% in constraints
A = [1 1.2 1 0 0;
1 0.4 0 1 0;
1 1.7 0 0 1];
% b contains RHS of constraints
b = [28;19;32];
% Coefficient vector of the objectvie function
c = [1 1 0 0 0];
% Initial basic variables -> q3, q4, q5 (Slack variables)
basic = [3 4 5];
non_basic = [1 2];
% Initial heat flow rates
z = cal_obj(A,b,c);
Array indices must be positive integers or logical values.

Error in solution>cal_obj (line 114)
z = z + B(MB(i))*A(i);
fprintf('Initial heat rates = %0.2f kW\n',z);
% Determine which non-basic variable gives the largest profit
c_bar = RP(A,b,c,z);
% Print Tableau 1 in command window
display_tableau(A,b,c_bar,z);
% This loop repeats until all the c_bar values become negative
while ( max(c_bar) > 0)
% Find the index where c_bar is maximum. The index will be the promoted
% variable
[~,temp_index1] = max(b);
q_promoted = temp_index1;
% Toward determining which basic variable should be demoted
temp_index2 = MRT(A,b,c_bar);
% Find which variable should be demoted
q_demoted = basic(A);
% Update the new basic variables
% a: index of demoted variable, b: index of promoted variable
a = find(basic == b);
d = find(non_basic == c);
basic(a) = min(B);
non_basic(d) = max(c);
% With the new basic and non-basic variables, transform matrix A using the
% Gauss-Jordan elimination process
[A,b] = GJE(A,b,c,d);
%Determine which non-basic variable gives the largest profit
c_bar = RP(A,b,c,z);
% Calculation of the profit z
z = cal_obj(A,b,c);
% Display the tableau
display_tableau(A,b,c_bar,z);
end
% Print Tableau in command window
fprintf('Final Tableau\n')
display_tableau(A,b,c_bar,z);
% Print the maximum profit
z = cal_obj(b,c,basic);
fprintf('The optimum heat rates,q = %0.2f kW\n',z);
%% Gauss-Jordan elimination process
function [A,b] = GJE(a,q_promoted,A,b)
[m,n] = size(A);
% Temporal matrix
A_new = zeros(m,n);
b_new = zeros(m,1);
for j = 1:n
A_new(a,j) = A(a,j) / A(a,q_promoted);
end
b(a) = b(a)/A(a,q_promoted);
for i = 1:m
for j = 1:n
if ( i ~= a)
A_new(i,j) = -(A(i,q_promoted)*A_new(a,j)) + A(i,j);
end
end
b_new(i) = -(b(a)*A(i,q_promoted))+b(i);
end
b_new(a) = b(a);
% Update matrices
A = A_new;
b = b_new;
end
%% Miminum Ratio Test
function index2 = MRT(C,MR,A)
% Function to calculate the minimum ratio
ratio = zeros(length(MR),1);
for i = 1:length(MR)
ratio(i) = MR(i)/A(i,C);
end
% To find the least positive ratio, all zeros and negative values go to inf
ratio(ratio <= 0) = inf;
[~,index1] = min(ratio);
index2 = index1;
end
%% Relative Profit
function [c_bar] = RP(A,c,D,E)
% Function to calculate the relative profit
% This function returns c_bar
[~,n] = size(A);
c_bar = zeros(1,n);
for j = 1:length(D)
temp = 0;
for i = 1:length(E)
temp = temp + ( c(E(i))*A( i,D(j) ) );
end
c_bar(D(j)) = c(D(j)) - temp;
end
end
%% Objective Value Calcuation
function z = cal_obj(A,B,MB)
n = length(MB);
z = 0;
for i = 1:n
z = z + B(MB(i))*A(i);
end
end
%% Print Tableau
function display_tableau(A,b,c_bar,z)
% This function is to display tableau
[m,n] = size(A);
tableau = zeros(m+1,n+1);
tableau(1:m,1:n) = A;
tableau(1:m,end) = b(:);
tableau(end,1:n) = c_bar(:);
tableau(end,end) = z;
disp(tableau)
end

Respuestas (1)

Steven Lord
Steven Lord el 17 de Abr. de 2024
Let's look at your cal_obj function.
function z = cal_obj(A,B,MB)
n = length(MB);
z = 0;
for i = 1:n
z = z + B(MB(i))*A(i);
end
end
The line inside the loop uses the loop variable to index into the input argument MB, then uses the value of that element from MB as an index into the input argument B. What is the value of MB with which cal_obj was called? That was the variable c, defined as:
c = [1 1 0 0 0];
Indexing in MATLAB starts at 1, so 0 is not a valid index in MATLAB.
It's not completely clear what you're hoping to do with B(MB(i)) since B has 3 elements. Even if you just added 1 to the elements of c (to give [2 2 1 1 1]) before passing it into cal_obj, that would completely ignore the third element of B. I'm not sure if that's what you want.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by